🧚 A practical web framework for Gleam

Updating examples to match new API.

authored by Lezenn and committed by Louis Pilfold 50ebbf1f b85a5ba3

Changed files
+34 -34
examples
00-hello-world
src
01-routing
src
02-working-with-form-data
src
05-using-a-database
src
app
06-serving-static-assets
src
08-working-with-cookies
src
09-configuring-default-responses
10-working-with-files
src
+3 -3
examples/00-hello-world/src/app/router.gleam
··· 1 - import wisp.{type Request, type Response} 2 - import gleam/string_builder 3 1 import app/web 2 + import gleam/string_tree 3 + import wisp.{type Request, type Response} 4 4 5 5 /// The HTTP request handler- your application! 6 6 /// ··· 9 9 use _req <- web.middleware(req) 10 10 11 11 // Later we'll use templates, but for now a string will do. 12 - let body = string_builder.from_string("<h1>Hello, Joe!</h1>") 12 + let body = string_tree.from_string("<h1>Hello, Joe!</h1>") 13 13 14 14 // Return a 200 OK response with the body and a HTML content type. 15 15 wisp.html_response(body, 200)
+7 -7
examples/01-routing/src/app/router.gleam
··· 1 - import wisp.{type Request, type Response} 2 - import gleam/string_builder 3 - import gleam/http.{Get, Post} 4 1 import app/web 2 + import gleam/http.{Get, Post} 3 + import gleam/string_tree 4 + import wisp.{type Request, type Response} 5 5 6 6 pub fn handle_request(req: Request) -> Response { 7 7 use req <- web.middleware(req) ··· 31 31 // used to return a 405: Method Not Allowed response for all other methods. 32 32 use <- wisp.require_method(req, Get) 33 33 34 - let html = string_builder.from_string("Hello, Joe!") 34 + let html = string_tree.from_string("Hello, Joe!") 35 35 wisp.ok() 36 36 |> wisp.html_body(html) 37 37 } ··· 48 48 49 49 fn list_comments() -> Response { 50 50 // In a later example we'll show how to read from a database. 51 - let html = string_builder.from_string("Comments!") 51 + let html = string_tree.from_string("Comments!") 52 52 wisp.ok() 53 53 |> wisp.html_body(html) 54 54 } 55 55 56 56 fn create_comment(_req: Request) -> Response { 57 57 // In a later example we'll show how to parse data from the request body. 58 - let html = string_builder.from_string("Created") 58 + let html = string_tree.from_string("Created") 59 59 wisp.created() 60 60 |> wisp.html_body(html) 61 61 } ··· 66 66 // The `id` path parameter has been passed to this function, so we could use 67 67 // it to look up a comment in a database. 68 68 // For now we'll just include in the response body. 69 - let html = string_builder.from_string("Comment with id " <> id) 69 + let html = string_tree.from_string("Comment with id " <> id) 70 70 wisp.ok() 71 71 |> wisp.html_body(html) 72 72 }
+3 -3
examples/02-working-with-form-data/src/app/router.gleam
··· 2 2 import gleam/http.{Get, Post} 3 3 import gleam/list 4 4 import gleam/result 5 - import gleam/string_builder 5 + import gleam/string_tree 6 6 import wisp.{type Request, type Response} 7 7 8 8 pub fn handle_request(req: Request) -> Response { ··· 21 21 // In a larger application a template library or HTML form library might 22 22 // be used here instead of a string literal. 23 23 let html = 24 - string_builder.from_string( 24 + string_tree.from_string( 25 25 "<form method='post'> 26 26 <label>Title: 27 27 <input type='text' name='title'> ··· 60 60 case result { 61 61 Ok(content) -> { 62 62 wisp.ok() 63 - |> wisp.html_body(string_builder.from_string(content)) 63 + |> wisp.html_body(string_tree.from_string(content)) 64 64 } 65 65 Error(_) -> { 66 66 wisp.bad_request()
+2 -2
examples/05-using-a-database/src/app/web/people.gleam
··· 1 1 import app/web.{type Context} 2 + import gleam/dict 2 3 import gleam/dynamic.{type Dynamic} 3 4 import gleam/http.{Get, Post} 4 5 import gleam/json 5 - import gleam/dict 6 6 import gleam/result.{try} 7 7 import tiny_database 8 8 import wisp.{type Request, type Response} ··· 122 122 // In this example we are not going to be reporting specific errors to the 123 123 // user, so we can discard the error and replace it with Nil. 124 124 result 125 - |> result.nil_error 125 + |> result.replace_error(Nil) 126 126 } 127 127 128 128 /// Save a person to the database and return the id of the newly created record.
+3 -3
examples/06-serving-static-assets/src/app/router.gleam
··· 1 - import wisp.{type Request, type Response} 2 - import gleam/string_builder 3 1 import app/web.{type Context} 2 + import gleam/string_tree 3 + import wisp.{type Request, type Response} 4 4 5 5 const html = "<!DOCTYPE html> 6 6 <html lang=\"en\"> ··· 18 18 19 19 pub fn handle_request(req: Request, ctx: Context) -> Response { 20 20 use _req <- web.middleware(req, ctx) 21 - wisp.html_response(string_builder.from_string(html), 200) 21 + wisp.html_response(string_tree.from_string(html), 200) 22 22 }
+3 -3
examples/08-working-with-cookies/src/app/router.gleam
··· 1 1 import app/web 2 2 import gleam/http.{Delete, Get, Post} 3 3 import gleam/list 4 - import gleam/string_builder 4 + import gleam/string_tree 5 5 import wisp.{type Request, type Response} 6 6 7 7 const cookie_name = "id" ··· 25 25 " <button type='submit'>Log out</button>", 26 26 "</form>", 27 27 ] 28 - |> string_builder.from_strings 28 + |> string_tree.from_strings 29 29 |> wisp.html_response(200) 30 30 } 31 31 Error(_) -> { ··· 52 52 <button type='submit'>Log in</button> 53 53 </form> 54 54 " 55 - |> string_builder.from_string 55 + |> string_tree.from_string 56 56 |> wisp.html_response(200) 57 57 } 58 58
+2 -2
examples/09-configuring-default-responses/src/app/router.gleam
··· 1 1 import app/web 2 - import gleam/string_builder 2 + import gleam/string_tree 3 3 import wisp.{type Request, type Response} 4 4 5 5 pub fn handle_request(req: Request) -> Response { ··· 9 9 // This request returns a non-empty body. 10 10 [] -> { 11 11 "<h1>Hello, Joe!</h1>" 12 - |> string_builder.from_string 12 + |> string_tree.from_string 13 13 |> wisp.html_response(200) 14 14 } 15 15
+6 -6
examples/09-configuring-default-responses/src/app/web.gleam
··· 1 - import wisp 2 1 import gleam/bool 3 - import gleam/string_builder 2 + import gleam/string_tree 3 + import wisp 4 4 5 5 pub fn middleware( 6 6 req: wisp.Request, ··· 32 32 case response.status { 33 33 404 | 405 -> 34 34 "<h1>There's nothing here</h1>" 35 - |> string_builder.from_string 35 + |> string_tree.from_string 36 36 |> wisp.html_body(response, _) 37 37 38 38 400 | 422 -> 39 39 "<h1>Bad request</h1>" 40 - |> string_builder.from_string 40 + |> string_tree.from_string 41 41 |> wisp.html_body(response, _) 42 42 43 43 413 -> 44 44 "<h1>Request entity too large</h1>" 45 - |> string_builder.from_string 45 + |> string_tree.from_string 46 46 |> wisp.html_body(response, _) 47 47 48 48 500 -> 49 49 "<h1>Internal server error</h1>" 50 - |> string_builder.from_string 50 + |> string_tree.from_string 51 51 |> wisp.html_body(response, _) 52 52 53 53 // For other status codes redirect to the home page
+5 -5
examples/10-working-with-files/src/app/router.gleam
··· 1 1 import app/web 2 + import gleam/bytes_tree 2 3 import gleam/http.{Get, Post} 3 4 import gleam/list 4 5 import gleam/result 5 - import gleam/string_builder 6 - import gleam/bytes_builder 6 + import gleam/string_tree 7 7 import wisp.{type Request, type Response} 8 8 9 9 pub fn handle_request(req: Request) -> Response { ··· 35 35 fn show_home(req: Request) -> Response { 36 36 use <- wisp.require_method(req, Get) 37 37 html 38 - |> string_builder.from_string 38 + |> string_tree.from_string 39 39 |> wisp.html_response(200) 40 40 } 41 41 ··· 45 45 // In this case we have the file contents in memory as a string. 46 46 // This is good if we have just made the file, but if the file already exists 47 47 // on the disc then the approach in the next function is more efficient. 48 - let file_contents = bytes_builder.from_string("Hello, Joe!") 48 + let file_contents = bytes_tree.from_string("Hello, Joe!") 49 49 50 50 wisp.ok() 51 51 |> wisp.set_header("content-type", "text/plain") ··· 107 107 case result { 108 108 Ok(name) -> { 109 109 { "<p>Thank you for your file!" <> name <> "</p>" <> html } 110 - |> string_builder.from_string 110 + |> string_tree.from_string 111 111 |> wisp.html_response(200) 112 112 } 113 113 Error(_) -> {