From 6bc130862b16d6d7e86a390e25972adf709d5faa Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 31 Mar 2024 11:13:32 -0700 Subject: [PATCH 1/2] add "random" endpoint that just redirects you to a random node. no hash required. --- src/ring/router.gleam | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ring/router.gleam b/src/ring/router.gleam index 682a0b6..ccc7e9c 100644 --- a/src/ring/router.gleam +++ b/src/ring/router.gleam @@ -12,6 +12,8 @@ pub fn handle_request(req: Request) -> Response { case wisp.path_segments(req) { [] -> home_page(req) + ["random"] -> handle_random() + [hash, "previous"] -> handle_previous(hash) [hash, "next"] -> handle_next(hash) @@ -55,6 +57,21 @@ fn handle_next(hash) { } } +fn handle_random() { + let db_path = "members.db" + use conn <- sqlight.with_connection(db_path) + let next = + sqlight.query( + "select next from ring order by random() limit 1", + on: conn, + expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), + ) + case next { + Ok([Row(next_link)]) -> wisp.redirect(next_link) + _ -> wisp.not_found() + } +} + 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. -- 2.34.1 From b2b1a6bab6e445722e9a6b8bb49cddae132a9174 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 31 Mar 2024 11:20:03 -0700 Subject: [PATCH 2/2] change 'next' to 'random' --- src/ring/router.gleam | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ring/router.gleam b/src/ring/router.gleam index ccc7e9c..ad80a27 100644 --- a/src/ring/router.gleam +++ b/src/ring/router.gleam @@ -60,14 +60,14 @@ fn handle_next(hash) { fn handle_random() { let db_path = "members.db" use conn <- sqlight.with_connection(db_path) - let next = + let random = sqlight.query( "select next from ring order by random() limit 1", on: conn, expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), ) - case next { - Ok([Row(next_link)]) -> wisp.redirect(next_link) + case random { + Ok([Row(random_link)]) -> wisp.redirect(random_link) _ -> wisp.not_found() } } -- 2.34.1