Skip to content

Commit

Permalink
Merge pull request #1 from alphabitserial/routing
Browse files Browse the repository at this point in the history
basic path handling
  • Loading branch information
alphabitserial authored Sep 10, 2024
2 parents 857cd31 + 56d9f4a commit 9c8ba03
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/app/router.gleam
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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)
}

0 comments on commit 9c8ba03

Please sign in to comment.