Skip to content

Commit

Permalink
able to connect to database
Browse files Browse the repository at this point in the history
  • Loading branch information
baraichaman committed Aug 2, 2024
1 parent e640732 commit 054addc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ glenvy = ">= 1.0.1 and < 2.0.0"
mist = ">= 1.2.0 and < 2.0.0"
gleam_erlang = ">= 0.25.0 and < 1.0.0"
gleam_http = ">= 3.6.0 and < 4.0.0"
gleam_community_ansi = ">= 1.4.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
5 changes: 4 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ packages = [
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
Expand Down Expand Up @@ -33,8 +35,9 @@ packages = [
]

[requirements]
gleam_community_ansi = { version = ">= 1.4.0 and < 2.0.0"}
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
gleam_http = { version = ">= 3.6.0 and < 4.0.0"}
gleam_http = { version = ">= 3.6.0 and < 4.0.0" }
gleam_pgo = { version = ">= 0.13.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
Expand Down
27 changes: 27 additions & 0 deletions src/app/db.gleam
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,
)
}
24 changes: 23 additions & 1 deletion src/app/middleware.gleam
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import gleam/http
import gleam/int
import gleam/io
import gleam/string
import gleam_community/ansi
import wisp.{type Request, type Response}

fn log_request(req: Request, handler: fn() -> Response) -> Response {
let response = handler()

[
ansi.blue("INFO "),
int.to_string(response.status),
" ",
string.uppercase(http.method_to_string(req.method)),
" ",
req.path,
]
|> string.concat
|> io.println

response
}

pub fn apply(request: Request, handler: fn(Request) -> Response) {
// Allows different requests to be processed instead of just (GET, POST)
let request = wisp.method_override(request)

// Log information about the current incomming request
use <- wisp.log_request(request)
use <- log_request(request)

// Automatically response with 500 (ServerInternalError) status code, if the
// the handler function crashes
Expand Down
21 changes: 20 additions & 1 deletion src/app/router.gleam
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])
}
}
10 changes: 7 additions & 3 deletions src/url_shortener.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import app/db
import gleam/erlang/process
import gleam/result.{try}
import glenvy/dotenv
Expand All @@ -15,12 +16,15 @@ pub fn main() {
use port <- try(env.get_int("PORT"))
use secret_key <- try(env.get_string("SECRET_KEY"))

// Iniciating a process to configure INFO level logs for the Wisp logger.
wisp.configure_logger()
// Getting the database connection pool
let assert Ok(db) = db.connect()

// Trying to start the webserver on given PORT
let assert Ok(_) =
wisp.mist_handler(router.handle_request, secret_key)
wisp.mist_handler(
fn(request) { router.handle_request(request, db) },
secret_key,
)
|> mist.new
|> mist.port(port)
|> mist.start_http
Expand Down

0 comments on commit 054addc

Please sign in to comment.