diff --git a/src/app/router.gleam b/src/app/router.gleam index 1b22d21..82973ac 100644 --- a/src/app/router.gleam +++ b/src/app/router.gleam @@ -1,4 +1,5 @@ import app/web +import gleam/http.{Get, Post} import gleam/string_builder.{type StringBuilder} import lustre/element import lustre/element/html.{html} @@ -9,18 +10,39 @@ pub fn handle_request(req: Request) -> Response { // Apply the middleware stack to all requests/responses. use _req <- web.middleware(req) - // Right now, let's generate the same template no matter what - // the user requests. - let body = index() + case wisp.path_segments(req) { + [] -> index(req) + ["greet", name] -> greet(req, name) + _ -> wisp.not_found() + } +} + +fn index(req: Request) -> Response { + // This page can only be accessed via GET request. + // Return a 405 Method Not Allowed for all other methods. + use <- wisp.require_method(req, Get) + + let html = + html([], [ + html.head([], [html.title([], "Greetings!")]), + html.body([], [html.h1([], [element.text("Hello from Wisp & Lustre!")])]), + ]) + |> element.to_document_string_builder - // Return a 200 OK response with the body and a content-type of HTML - wisp.html_response(body, 200) + wisp.ok() + |> wisp.html_body(html) } -fn index() -> StringBuilder { - html([], [ - html.head([], [html.title([], "Greetings!")]), - html.body([], [html.h1([], [element.text("Hello from Wisp & Lustre!")])]), - ]) - |> element.to_document_string_builder +fn greet(req: Request, name: String) -> Response { + use <- wisp.require_method(req, Get) + + let html = + html([], [ + html.head([], [html.title([], "Hi " <> name <> "!")]), + html.body([], [html.h1([], [element.text("Hello " <> name <> "!")])]), + ]) + |> element.to_document_string_builder + + wisp.ok() + |> wisp.html_body(html) }