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

Configure Feed

Select the types of activity you want to include in your feed.

:recycle: inline the `read_cookie_token` function

Kacaii 5c2bd390 54a1d97a

+24 -32
+2 -1
dev/app_dev.gleam
··· 2 2 import app/web/context.{type Context, Context} 3 3 import argv 4 4 import dummy 5 + import envoy 5 6 import gleam/erlang/process 6 7 import gleam/int 7 8 import gleam/io ··· 85 86 let registry_name = process.new_name("registry") 86 87 87 88 let assert Ok(config) = app.read_connection_uri(db_process_name) 88 - let assert Ok(secret_key_base) = app.read_cookie_token() 89 + let assert Ok(secret_key_base) = envoy.get("COOKIE_TOKEN") 89 90 90 91 let db = pog.named_connection(db_process_name) 91 92 let assert Ok(_) = pog.start(config)
+15 -27
src/app.gleam
··· 16 16 import app/web/context.{type Context, Context} 17 17 import app/web/socket 18 18 import envoy 19 + import gleam/bool 19 20 import gleam/erlang/process 20 21 import gleam/http 21 22 import gleam/io ··· 37 38 //  Setup the connection process name 38 39 let db_process_name = process.new_name("db_conn") 39 40 40 - // DATABASE POOL ------------------------------------------------------------- 41 - //  Database connection 42 - let db = pog.named_connection(db_process_name) 43 - 44 41 // SECRET KEYS --------------------------------------------------------------- 45 42 // Used for signing and encryption 46 - let assert Ok(secret_key_base) = read_cookie_token() 47 - as " Failed to read the cookie secret key" 43 + let assert Ok(secret_key_base) = envoy.get("COOKIE_TOKEN") 44 + as " Cookie secret key is available" 48 45 49 46 // Postgresql connection URI 50 47 let assert Ok(pog_config) = read_connection_uri(db_process_name) 51 - as " Failed to read DataBase connection URI" 48 + as " DataBase connection URI is available" 49 + 52 50 // Pass the application context to the router 53 51 let ctx = 54 52 Context( 55 53 static_directory: static_directory(), 56 - db:, 54 + db: pog.named_connection(db_process_name), 57 55 registry_name:, 58 56 secret_key_base:, 59 57 ) ··· 70 68 secret_key_base:, 71 69 registry_name:, 72 70 ) 73 - as "󰪋 Failed to start the application supervisor" 71 + as "󰪋 Start the application supervision tree" 74 72 75 - // ⏾ 󰒲 76 73 process.sleep_forever() 77 74 } 78 75 79 - ///  Read the `COOKIE_TOKEN` enviroment variable 80 - pub fn read_cookie_token() -> Result(String, Nil) { 81 - use cookie_token <- result.try(envoy.get("COOKIE_TOKEN")) 82 - Ok(cookie_token) 83 - } 84 - 85 76 ///  Read the `DATABASE_URL` environment variable and then 86 77 /// build the `pog.Config` from that database URI. 87 78 pub fn read_connection_uri( ··· 103 94 /// Access to static files 104 95 pub fn static_directory() -> String { 105 96 let assert Ok(priv_directory) = wisp.priv_directory("app") 106 - as "Failed to access priv directory" 97 + as "Erlang's priv directory is generated" 107 98 108 99 priv_directory <> "/static" 109 100 } ··· 111 102 /// Generate a default admin account if the user_account table is empty 112 103 pub fn setup_admin(ctx: Context) { 113 104 let assert Ok(returned) = admin_sql.count_total_users(ctx.db) 105 + as "Check if the users table is empty" 114 106 let assert Ok(row) = list.first(returned.rows) 107 + use <- bool.guard(when: row.total != 0, return: Nil) 115 108 116 - case row.total == 0 { 117 - False -> Nil 118 - True -> { 119 - let setup_admin_req = 120 - simulate.browser_request(http.Post, "/admin/setup") 121 - |> simulate.json_body(json.object([#("key", json.string("admin"))])) 109 + // Generate a default admin user 110 + simulate.browser_request(http.Post, "/admin/setup") 111 + |> simulate.json_body(json.object([#("key", json.string("admin"))])) 112 + |> http_router.handle_request(request: _, ctx:) 122 113 123 - http_router.handle_request(setup_admin_req, ctx) 124 - io.println(" Administrador cadastrado com sucesso!") 125 - } 126 - } 114 + io.println(" Administrador cadastrado com sucesso!") 127 115 }
+5 -3
src/app/http_router.gleam
··· 38 38 39 39 /// 󱂇 Main request router - matches HTTP methods and paths to appropriate handlers 40 40 /// All routes pass through middleware first for common processing 41 - pub fn handle_request(request: wisp.Request, ctx: Context) -> wisp.Response { 42 - use request <- web.middleware(request: request, context: ctx) 43 - 41 + pub fn handle_request( 42 + request request: wisp.Request, 43 + ctx ctx: Context, 44 + ) -> wisp.Response { 45 + use request <- web.middleware(request:, context: ctx) 44 46 case request.method, wisp.path_segments(request) { 45 47 //  Security routes ------------------------------------------------- 46 48 http.Post, ["user", "login"] -> login.handle_request(request:, ctx:)
+2 -1
test/app_test.gleam
··· 1 1 import app 2 2 import app/http_router 3 3 import app/web/context.{type Context, Context} 4 + import envoy 4 5 import gleam/erlang/process 5 6 import gleam/http 6 7 import gleeunit ··· 22 23 let registry_name = process.new_name("registry") 23 24 24 25 let assert Ok(config) = app.read_connection_uri(db_process_name) 25 - let assert Ok(secret_key_base) = app.read_cookie_token() 26 + let assert Ok(secret_key_base) = envoy.get("COOKIE_TOKEN") 26 27 27 28 let db = pog.named_connection(db_process_name) 28 29 let assert Ok(_) = pog.start(config)