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: add type alias for mist request and response types

Kacaii 608c3ca8 95169e5e

+38 -41
+8 -3
src/app/supervision_tree.gleam
··· 10 10 import wisp 11 11 import wisp/wisp_mist 12 12 13 + type Request = 14 + request.Request(mist.Connection) 15 + 16 + type Response = 17 + response.Response(mist.ResponseData) 18 + 13 19 /// 󰪋 Start the application supervisor 14 20 pub fn start( 15 21 ctx ctx: context.Context, 16 22 pog_config pog_config: pog.Config, 17 23 wisp_handler wisp_handler: fn(wisp.Request) -> wisp.Response, 18 - ws_handler ws_handler: fn(request.Request(mist.Connection)) -> 19 - response.Response(mist.ResponseData), 24 + ws_handler ws_handler: fn(Request) -> Response, 20 25 secret_key_base secret_key_base: String, 21 26 registry_name registry_name: process.Name(_), 22 27 ) -> Result(actor.Started(supervisor.Supervisor), actor.StartError) { 23 28 // Handler for the web server 24 29 let webserver_handler = fn(req) { 25 30 case wisp.path_segments(req) { 26 - ["ws"] | ["ws", _] -> ws_handler(req) 31 + ["ws"] -> ws_handler(req) 27 32 _ -> wisp_mist.handler(wisp_handler, secret_key_base)(req) 28 33 } 29 34 }
+30 -38
src/app/web/socket.gleam
··· 27 27 28 28 pub const ws_topic = "active_users" 29 29 30 + type Request = 31 + request.Request(mist.Connection) 32 + 33 + type Response = 34 + response.Response(mist.ResponseData) 35 + 30 36 pub opaque type WebSocketError { 31 37 /// 󱛪 Session cookie was not found 32 38 MissingCookie ··· 47 53 } 48 54 49 55 /// 󱘖 Stabilishes a websocket connection with the client 50 - pub fn handle_request( 51 - req: request.Request(mist.Connection), 52 - ctx: Context, 53 - ) -> response.Response(mist.ResponseData) { 56 + pub fn handle_request(req: Request, ctx: Context) -> Response { 54 57 let registry = group_registry.get_registry(ctx.registry_name) 55 58 56 59 case extract_uuid(req, ctx) { ··· 87 90 } 88 91 89 92 fn handle_connection( 90 - req: request.Request(mist.Connection), 93 + req: Request, 91 94 ctx: Context, 92 95 user_uuid: uuid.Uuid, 93 96 registry: group_registry.GroupRegistry(msg.Msg), 94 - ) -> response.Response(mist.ResponseData) { 97 + ) -> Response { 95 98 case fetch_user_data(ctx, user_uuid) { 96 99 Error(err) -> handle_error(err) 97 100 Ok(state) -> route_request(req, ctx, registry, state) ··· 99 102 } 100 103 101 104 fn route_request( 102 - req: request.Request(mist.Connection), 105 + req: Request, 103 106 ctx: Context, 104 107 registry: group_registry.GroupRegistry(msg.Msg), 105 108 state: State, 106 - ) -> response.Response(mist.ResponseData) { 109 + ) -> Response { 107 110 case request.path_segments(req) { 108 111 ["ws"] -> 109 112 mist.websocket( ··· 287 290 } 288 291 } 289 292 290 - fn extract_uuid( 291 - req: request.Request(mist.Connection), 292 - ctx: Context, 293 - ) -> Result(uuid.Uuid, WebSocketError) { 293 + fn extract_uuid(req: Request, ctx: Context) -> Result(uuid.Uuid, WebSocketError) { 294 294 let cookies = request.get_cookies(req) 295 295 let salt = <<ctx.secret_key_base:utf8>> 296 296 ··· 317 317 318 318 fn ws_on_init( 319 319 conn _conn: mist.WebsocketConnection, 320 - req _req: request.Request(mist.Connection), 320 + req _req: Request, 321 321 ctx _ctx: Context, 322 322 registry registry: group_registry.GroupRegistry(msg.Msg), 323 323 state state: State, ··· 377 377 |> result.map_error(Database), 378 378 ) 379 379 380 - use row <- list.map(returned.rows) 381 - case row.notification_type { 382 - notif_sql.Emergency -> category.MedicEmergency 383 - notif_sql.Fire -> category.Fire 384 - notif_sql.Other -> category.Other 385 - notif_sql.Traffic -> category.TrafficAccident 386 - } 380 + list.map(returned.rows, fn(row) { 381 + case row.notification_type { 382 + notif_sql.Emergency -> category.MedicEmergency 383 + notif_sql.Fire -> category.Fire 384 + notif_sql.Other -> category.Other 385 + notif_sql.Traffic -> category.TrafficAccident 386 + } 387 + }) 387 388 } 388 389 389 390 // ON CLOSE -------------------------------------------------------------------- ··· 407 408 408 409 // HELPERS --------------------------------------------------------------------- 409 410 410 - fn send_response( 411 - body: String, 412 - status: Int, 413 - ) -> response.Response(mist.ResponseData) { 414 - body 415 - |> bytes_tree.from_string 411 + fn send_response(body body: String, status status: Int) -> Response { 412 + bytes_tree.from_string(body) 416 413 |> mist.Bytes 417 414 |> response.set_body(response.new(status), _) 418 415 } 419 416 420 - fn handle_error(err: WebSocketError) -> response.Response(mist.ResponseData) { 417 + fn handle_error(err: WebSocketError) -> Response { 421 418 case err { 422 419 InvalidUuid(id) -> { 423 420 let body = "Usuário possui Uuid inválido: " <> id ··· 457 454 } 458 455 } 459 456 460 - fn handle_database_error( 461 - err: pog.QueryError, 462 - ) -> response.Response(mist.ResponseData) { 457 + fn handle_database_error(err: pog.QueryError) -> Response { 463 458 case err { 464 459 pog.ConnectionUnavailable -> 465 460 "Conexão com o banco de dados não disponível" ··· 513 508 } 514 509 } 515 510 516 - pub fn handle_decode_error( 517 - decode_errors: List(decode.DecodeError), 518 - ) -> response.Response(mist.ResponseData) { 519 - case list.first(decode_errors) { 511 + pub fn handle_decode_error(failed: List(decode.DecodeError)) -> Response { 512 + case list.first(failed) { 520 513 Error(_) -> send_response("Ok", 200) 521 514 Ok(err) -> 522 - [ 515 + json.object([ 523 516 #("expected", json.string(err.expected)), 524 517 #("found", json.string(err.found)), 525 518 #("path", json.string(string.join(err.path, "/"))), 526 - ] 527 - |> json.object 519 + ]) 528 520 |> json.to_string 529 521 |> send_response(400) 530 522 } 531 523 } 532 524 533 - pub fn read_body(req: request.Request(mist.Connection)) { 525 + pub fn read_body(req: Request) { 534 526 use header <- result.try( 535 527 request.get_header(req, "content-length") 536 528 |> result.replace_error(MissingContentLength),