-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e640732
commit 054addc
Showing
6 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import gleam/io | ||
import gleam/option | ||
import gleam/pgo | ||
import gleam/result | ||
import glenvy/env | ||
|
||
pub fn connect() { | ||
// Reading the DB connection URI from the environment. | ||
use pg_dbname <- result.try(env.get_string("DB_NAME")) | ||
use pg_host <- result.try(env.get_string("DB_HOST")) | ||
use pg_user <- result.try(env.get_string("DB_USER")) | ||
use pg_pass <- result.try(env.get_string("DB_PASS")) | ||
|
||
// Starting a connection pool | ||
Ok( | ||
pgo.Config( | ||
..pgo.default_config(), | ||
port: 47_552, | ||
host: pg_host, | ||
user: pg_user, | ||
pool_size: 25, | ||
database: pg_dbname, | ||
password: option.Some(pg_pass), | ||
) | ||
|> pgo.connect, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import gleam/dynamic | ||
import gleam/http.{Get, Post} | ||
import gleam/io | ||
import gleam/list | ||
import gleam/pgo.{type Connection} | ||
import gleam/string | ||
import wisp.{type Request, type Response} | ||
|
||
import app/middleware | ||
|
||
pub fn handle_request(request: Request) -> Response { | ||
fn landing_page(connection: Connection) -> Response { | ||
let sql = "SELECT * FROM urls" | ||
let response_type = | ||
dynamic.tuple3(dynamic.int, dynamic.string, dynamic.string) | ||
|
||
let assert Ok(results) = pgo.execute(sql, connection, [], response_type) | ||
io.debug(results) | ||
let ids = list.map(results.rows, fn(item) { item.2 }) | ||
|
||
wisp.ok() | ||
|> wisp.string_body(string.concat(ids)) | ||
} | ||
|
||
pub fn handle_request(request: Request, connection: Connection) -> Response { | ||
use request <- middleware.apply(request) | ||
|
||
case request.method, wisp.path_segments(request) { | ||
Get, [] -> landing_page(connection) | ||
_, _ -> wisp.method_not_allowed([Get, Post]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters