From 3cafc7054dbbefdd2204d57efa5c480385ca9837 Mon Sep 17 00:00:00 2001 From: Erika Rowland Date: Sun, 31 Mar 2024 11:43:38 -0700 Subject: [PATCH 1/2] [ring] Add index page at root path --- gleam.toml | 1 + manifest.toml | 4 +++- src/ring/router.gleam | 27 ++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gleam.toml b/gleam.toml index f1178a5..2526a3f 100644 --- a/gleam.toml +++ b/gleam.toml @@ -10,6 +10,7 @@ mist = "~> 0.14" gleam_http = "~> 3.5" wisp = "~> 0.14" sqlight = "~> 0.9" +lustre = "~> 4.1" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 6526abb..2cc8915 100644 --- a/manifest.toml +++ b/manifest.toml @@ -14,6 +14,7 @@ packages = [ { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, { name = "glisten", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "73BC09C8487C2FFC0963BFAB33ED2F0D636FDFA43B966E65C1251CBAB8458099" }, { name = "logging", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "82C112ED9B6C30C1772A6FE2613B94B13F62EA35F5869A2630D13948D297BD39" }, + { name = "lustre", version = "4.1.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "09530DF24E7E540CA59AB2486909B52EEA5C39AE1A41EBC640AD04E5418FD67A" }, { name = "marceau", version = "1.1.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "1AAD727A30BE0F95562C3403BB9B27C823797AD90037714255EEBF617B1CDA81" }, { name = "mist", version = "0.17.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten"], otp_app = "mist", source = "hex", outer_checksum = "DA8ACEE52C1E4892A75181B3166A4876D8CBC69D555E4770250BC84C80F75524" }, { name = "simplifile", version = "1.6.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "B75D3C64E526D9D7EDEED5F3BA31DAAF5F2B4D80A4183FE17FDB02ED526E4E96" }, @@ -27,6 +28,7 @@ gleam_erlang = { version = "~> 0.23" } gleam_http = { version = "~> 3.5" } gleam_stdlib = { version = "~> 0.30" } gleeunit = { version = "~> 1.0" } +lustre = { version = "~> 4.1"} mist = { version = "~> 0.14" } -sqlight = { version = "~> 0.9"} +sqlight = { version = "~> 0.9" } wisp = { version = "~> 0.14" } diff --git a/src/ring/router.gleam b/src/ring/router.gleam index c09f14b..02bef49 100644 --- a/src/ring/router.gleam +++ b/src/ring/router.gleam @@ -2,9 +2,14 @@ import wisp.{type Request, type Response} import gleam/string_builder import gleam/http.{Get} import gleam/io +import gleam/list +import gleam/result import gleam/dynamic import ring/web import sqlight +import lustre/attribute +import lustre/element/html +import lustre/element pub fn handle_request(req: Request) -> Response { use req <- web.middleware(req) @@ -77,8 +82,28 @@ fn home_page(req: Request) -> Response { // The home page can only be accessed via GET requests, so this middleware is // used to return a 405: Method Not Allowed response for all other methods. use <- wisp.require_method(req, Get) + let db_path = "members.db" + use conn <- sqlight.with_connection(db_path) + let members = + sqlight.query( + "select member from ring", + on: conn, + with: [], + expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), + ) + let members = + result.unwrap(members, []) + |> list.map(fn(row) { + let Row(member) = row + html.li([], [html.a([attribute.href(member)], [html.text(member)])]) + }) + let html = + html.body([], [ + html.h1([], [html.text("The Constellation Webring")]), + html.ul([], members), + ]) + |> element.to_document_string_builder() - let html = string_builder.from_string("Hello, Joe!") wisp.ok() |> wisp.html_body(html) } From 235c402efba8fefb26c62ca78f7876030386cf1d Mon Sep 17 00:00:00 2001 From: Erika Rowland Date: Sun, 31 Mar 2024 11:48:47 -0700 Subject: [PATCH 2/2] [ring] Update sort order --- src/ring/router.gleam | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ring/router.gleam b/src/ring/router.gleam index 02bef49..507cb21 100644 --- a/src/ring/router.gleam +++ b/src/ring/router.gleam @@ -1,7 +1,5 @@ import wisp.{type Request, type Response} -import gleam/string_builder import gleam/http.{Get} -import gleam/io import gleam/list import gleam/result import gleam/dynamic @@ -86,7 +84,7 @@ fn home_page(req: Request) -> Response { use conn <- sqlight.with_connection(db_path) let members = sqlight.query( - "select member from ring", + "select member from ring order by member asc", on: conn, with: [], expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)),