🧚 A practical web framework for Gleam

Logging

+126 -5
+1
README.md
··· 62 62 - [Working with other formats](https://github.com/lpil/wisp/tree/main/examples/4-working-with-other-formats) 63 63 - [Using a database](https://github.com/lpil/wisp/tree/main/examples/5-using-a-database) 64 64 - [Serving static assets](https://github.com/lpil/wisp/tree/main/examples/6-serving-static-assets) 65 + - [Logging](https://github.com/lpil/wisp/tree/main/examples/7-logging) 65 66 66 67 API documentation is available on [HexDocs](https://hexdocs.pm/wisp/).
+22
examples/7-logging/README.md
··· 1 + # Wisp Example: Logging 2 + 3 + ```sh 4 + gleam run # Run the server 5 + gleam test # Run the tests 6 + ``` 7 + 8 + This example shows how to route requests to different handlers based on the 9 + request path and method. 10 + 11 + This example is based off of the ["routing" example][routing], so read that 12 + one first. The additions are detailed here and commented in the code. 13 + 14 + [hello]: https://github.com/lpil/wisp/tree/main/examples/1-routing 15 + 16 + ### `app/router` module 17 + 18 + The `handle_request` function now logs messages depending on the request. 19 + 20 + ### Other files 21 + 22 + No changes have been made to the other files.
+10
examples/7-logging/gleam.toml
··· 1 + name = "app" 2 + version = "1.0.0" 3 + description = "A Wisp example" 4 + 5 + [dependencies] 6 + gleam_stdlib = "~> 0.30" 7 + wisp = { path = "../.." } 8 + 9 + [dev-dependencies] 10 + gleeunit = "~> 0.10"
+17
examples/7-logging/src/app.gleam
··· 1 + import gleam/erlang/process 2 + import mist 3 + import wisp 4 + import app/router 5 + 6 + pub fn main() { 7 + wisp.configure_logger() 8 + let secret_key_base = wisp.random_string(64) 9 + 10 + let assert Ok(_) = 11 + wisp.mist_handler(router.handle_request, secret_key_base) 12 + |> mist.new 13 + |> mist.port(8000) 14 + |> mist.start_http 15 + 16 + process.sleep_forever() 17 + }
+42
examples/7-logging/src/app/router.gleam
··· 1 + import wisp.{Request, Response} 2 + import gleam/http.{Get, Post} 3 + import app/web 4 + 5 + // Wisp has functions for logging messages using the BEAM logger. 6 + // 7 + // Messages can be logged at different levels. From most important to least 8 + // important they are: 9 + // - emergency 10 + // - alert 11 + // - critical 12 + // - error 13 + // - warning 14 + // - notice 15 + // - info 16 + // - debug 17 + // 18 + pub fn handle_request(req: Request) -> Response { 19 + use _req <- web.middleware(req) 20 + 21 + case wisp.path_segments(req) { 22 + [] -> { 23 + wisp.log_debug("The home page") 24 + wisp.ok() 25 + } 26 + 27 + ["about"] -> { 28 + wisp.log_info("They're reading about us") 29 + wisp.ok() 30 + } 31 + 32 + ["secret"] -> { 33 + wisp.log_error("The secret page was found!") 34 + wisp.ok() 35 + } 36 + 37 + _ -> { 38 + wisp.log_warning("User requested a route that does not exist") 39 + wisp.not_found() 40 + } 41 + } 42 + }
+13
examples/7-logging/src/app/web.gleam
··· 1 + import wisp 2 + 3 + pub fn middleware( 4 + req: wisp.Request, 5 + handle_request: fn(wisp.Request) -> wisp.Response, 6 + ) -> wisp.Response { 7 + let req = wisp.method_override(req) 8 + use <- wisp.log_request(req) 9 + use <- wisp.rescue_crashes 10 + use req <- wisp.handle_head(req) 11 + 12 + handle_request(req) 13 + }
+16
examples/7-logging/test/app_test.gleam
··· 1 + import gleeunit 2 + import gleeunit/should 3 + import wisp/testing 4 + import app/router 5 + 6 + pub fn main() { 7 + gleeunit.main() 8 + } 9 + 10 + pub fn get_home_page_test() { 11 + let request = testing.get("/", []) 12 + let response = router.handle_request(request) 13 + 14 + response.status 15 + |> should.equal(200) 16 + }
+5 -5
manifest.toml
··· 4 4 packages = [ 5 5 { name = "exception", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "71F00057D38ADB03BBCCD0E3B07AB2C236BD49DBA7E7611A9DADBD1E26C9F53D" }, 6 6 { name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" }, 7 - { name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" }, 7 + { name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" }, 8 8 { name = "gleam_erlang", version = "0.21.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A502A9DE61CEBC94B9D55A1FD41D0EA735A82D74E926E3D874CBA69F3705BDB2" }, 9 9 { name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" }, 10 - { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, 11 - { name = "gleam_otp", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E31B158857E3D2AF946FE6E90E0CB21699AF226F4630E93FBEAC5DB4515F8920" }, 10 + { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, 11 + { name = "gleam_otp", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E31B158857E3D2AF946FE6E90E0CB21699AF226F4630E93FBEAC5DB4515F8920" }, 12 12 { name = "gleam_stdlib", version = "0.30.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "704258528887F95075FFED7AAE1CCF836A9B88E3AADA2F69F9DA15815F94A4F9" }, 13 13 { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, 14 - { name = "glisten", version = "0.8.1", build_tools = ["gleam"], requirements = ["gleam_otp", "gleam_erlang", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "B156D92DD8508D05C7E2D54FA6EB4EC57510E70F1B156F40B0FF1589CE72095D" }, 15 - { name = "mist", version = "0.13.1", build_tools = ["gleam"], requirements = ["glisten", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "mist", source = "hex", outer_checksum = "178EDF5F396570DD53BE2A94C8F9759072093DACB81B62CD47A620B961DB2F2D" }, 14 + { name = "glisten", version = "0.8.1", build_tools = ["gleam"], requirements = ["gleam_otp", "gleam_stdlib", "gleam_erlang"], otp_app = "glisten", source = "hex", outer_checksum = "B156D92DD8508D05C7E2D54FA6EB4EC57510E70F1B156F40B0FF1589CE72095D" }, 15 + { name = "mist", version = "0.13.1", build_tools = ["gleam"], requirements = ["glisten", "gleam_http", "gleam_erlang", "gleam_stdlib"], otp_app = "mist", source = "hex", outer_checksum = "178EDF5F396570DD53BE2A94C8F9759072093DACB81B62CD47A620B961DB2F2D" }, 16 16 { name = "simplifile", version = "0.1.10", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "263B7C7F4B29263555DEA2D30BA918425A27120CDD1E1352744EAB4D56CE01CE" }, 17 17 { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, 18 18 ]