wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam

:bulb: add descriptions and doc comments about handlers and db connections

+17 -3
+14 -3
src/app.gleam
··· 10 10 import wisp 11 11 import wisp/wisp_mist 12 12 13 + /// Application entry 13 14 pub fn main() -> Nil { 14 15 wisp.configure_logger() 15 16 let secret_key_base = wisp.random_string(64) 16 17 18 + //  Setup the postgres database connection --------------------------------- 17 19 let db_process_name = process.new_name("db_conn") 20 + 18 21 let assert Ok(pog_config) = read_connection_uri(db_process_name) 22 + as "Failed to read connection URI" 19 23 let assert Ok(_) = start_application_supervised(pog_config) 24 + as "Failed to database supervisor" 20 25 21 - // Database connection 26 + //  Database connection 22 27 let conn = pog.named_connection(db_process_name) 23 28 29 + // Pass the application context to the router 24 30 let ctx = Context(static_directory: static_directory(), conn:) 25 31 let handler = router.handle_request(_, ctx) 26 32 ··· 34 40 process.sleep_forever() 35 41 } 36 42 43 + ///  Start the postgres application supervisor 37 44 pub fn start_application_supervised( 38 45 pog_config: pog.Config, 39 46 ) -> Result(actor.Started(supervisor.Supervisor), actor.StartError) { ··· 44 51 |> supervisor.start 45 52 } 46 53 47 - /// Read the DATABASE_URL environment variable and then 48 - /// build the pog.Config from that database URL. 54 + ///  Read the `DATABASE_URL` environment variable and then 55 + /// build the `pog.Config` from that database URI. 49 56 pub fn read_connection_uri( 50 57 name: process.Name(pog.Message), 51 58 ) -> Result(pog.Config, Nil) { 59 + // Remember to set the enviroment variable before running the app 52 60 use postgres_url <- result.try(envoy.get("DATABASE_URL")) 61 + 53 62 pog.url_config(name, postgres_url) 54 63 } 55 64 56 65 /// Access to Erlang's Priv directory 57 66 pub fn static_directory() -> String { 58 67 let assert Ok(priv_directory) = wisp.priv_directory("app") 68 + as "Failed to acces priv directory" 69 + 59 70 priv_directory <> "/static" 60 71 }
+1
src/app/router.gleam
··· 1 1 import app/web.{type Context} 2 2 import wisp 3 3 4 + /// Handle the incoming requests 4 5 pub fn handle_request(req: wisp.Request, ctx: Context) -> wisp.Response { 5 6 use req <- web.middleware(request: req, context: ctx) 6 7
+2
src/app/web.gleam
··· 7 7 Context(static_directory: String, conn: pog.Connection) 8 8 } 9 9 10 + /// Middleware that runs before every request. 11 + /// It sets up the request, and then calls the next handler. 10 12 pub fn middleware( 11 13 request req: wisp.Request, 12 14 context ctx: Context,