Skip to content

Commit

Permalink
feat: added base controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsam480 committed Jun 30, 2024
1 parent 4de461b commit b09d5e0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
2 changes: 2 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ gleam_stdlib = ">= 0.34.0 and < 2.0.0"
wisp = ">= 0.15.0 and < 1.0.0"
gleam_erlang = ">= 0.25.0 and < 1.0.0"
mist = ">= 1.2.0 and < 2.0.0"
gleam_http = ">= 3.6.0 and < 4.0.0"
radiate = ">= 0.4.0 and < 1.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
8 changes: 7 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ packages = [
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "filespy", version = "0.5.0", build_tools = ["gleam"], requirements = ["fs", "gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "filespy", source = "hex", outer_checksum = "F8E7A9C9CA86D68CCC25491125BFF36BEF7483892D7BEC24AA30D6B540504F06" },
{ name = "fs", version = "8.6.1", build_tools = ["rebar3"], requirements = [], otp_app = "fs", source = "hex", outer_checksum = "61EA2BDAEDAE4E2024D0D25C63E44DCCF65622D4402DB4A2DF12868D1546503F" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
Expand All @@ -18,15 +20,19 @@ packages = [
{ name = "logging", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "FCB111401BDB4703A440A94FF8CC7DA521112269C065F219C2766998333E7738" },
{ name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" },
{ name = "mist", version = "1.2.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "109B4D64E68C104CC23BB3CC5441ECD479DD7444889DA01113B75C6AF0F0E17B" },
{ name = "radiate", version = "0.4.0", build_tools = ["gleam"], requirements = ["filespy", "gleam_otp", "gleam_stdlib", "shellout"], otp_app = "radiate", source = "hex", outer_checksum = "93A76A66EE4741DBFD3E79E27CBD11FE58EC3CB1C58F017FC165944E339D6293" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "wisp", version = "0.15.0", build_tools = ["gleam"], requirements = ["exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "33D17A50276FE0A10E4F694E4EF7D99836954DC2D920D4B5741B1E0EBCAE403F" },
]

[requirements]
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
gleam_http = { version = ">= 3.6.0 and < 4.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
mist = { version = ">= 1.2.0 and < 2.0.0"}
mist = { version = ">= 1.2.0 and < 2.0.0" }
radiate = { version = ">= 0.4.0 and < 1.0.0"}
wisp = { version = ">= 0.15.0 and < 1.0.0" }
3 changes: 3 additions & 0 deletions research.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## articles
- https://medium.com/@mithunmk93/algorithm-behind-splitwises-debt-simplification-feature-8ac485e97688
- https://medium.com/@howoftech/how-does-the-splitwise-algorithm-work-dc1de5eaa371
24 changes: 15 additions & 9 deletions src/app/router.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import app/web
import gleam/string_builder
import controllers/home
import controllers/session
import wisp.{type Request, type Response}

/// The HTTP request handler- your application!
///
pub fn handle_request(req: Request) -> Response {
// Apply the middleware stack for this request/response.
use _req <- web.middleware(req)
use req <- web.middleware(req)

// Later we'll use templates, but for now a string will do.
let body = string_builder.from_string("<h1>Hello, Joe!</h1>")
// Wisp doesn't have a special router abstraction, instead we recommend using
// regular old pattern matching. This is faster than a router, is type safe,
// and means you don't have to learn or be limited by a special DSL.
//
case wisp.path_segments(req) {
// This matches `/`.
[] -> home.controller(req)

// Return a 200 OK response with the body and a HTML content type.
wisp.html_response(body, 200)
// responds to session
["session"] -> session.controller(req)

_ -> wisp.not_found()
}
}
22 changes: 22 additions & 0 deletions src/controllers/home.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gleam/http.{Get}
import gleam/string_builder
import wisp.{type Request, type Response}

fn show(_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 html = string_builder.from_string("Welcome to Okane")

wisp.ok()
|> wisp.html_body(html)
}

pub fn controller(req: Request) -> Response {
case req.method {
Get -> show(req)

_ -> wisp.method_not_allowed([Get])
}
}
18 changes: 18 additions & 0 deletions src/controllers/session.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import gleam/http.{Get}
import gleam/string_builder
import wisp.{type Request, type Response}

fn show_user(_req: Request) -> Response {
let html = string_builder.from_string("Welcome Okane")

wisp.ok()
|> wisp.html_body(html)
}

pub fn controller(req: Request) -> Response {
case req.method {
Get -> show_user(req)

_ -> wisp.method_not_allowed([Get])
}
}
7 changes: 7 additions & 0 deletions src/okane.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import app/router
import gleam/erlang/process
import mist
import radiate
import wisp

pub fn main() {
let _ =
radiate.new()
|> radiate.add_dir(".")
|> radiate.start()

// This sets the logger to print INFO level logs, and other sensible defaults
// for a web application.
wisp.configure_logger()
Expand All @@ -21,5 +27,6 @@ pub fn main() {

// The web server runs in new Erlang process, so put this one to sleep while
// it works concurrently.

process.sleep_forever()
}
33 changes: 30 additions & 3 deletions test/okane_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ pub fn main() {
gleeunit.main()
}

pub fn hello_world_test() {
let response = router.handle_request(testing.get("/", []))
pub fn get_home_page_test() {
let request = testing.get("/", [])
let response = router.handle_request(request)

response.status
|> should.equal(200)
Expand All @@ -18,5 +19,31 @@ pub fn hello_world_test() {

response
|> testing.string_body
|> should.equal("<h1>Hello, Joe!</h1>")
|> should.equal("Welcome to Okane")
}

pub fn post_home_page_test() {
let request = testing.post("/", [], "a body")
let response = router.handle_request(request)

response.status
|> should.equal(405)
}

pub fn page_not_found_test() {
let request = testing.get("/nothing-here", [])
let response = router.handle_request(request)

response.status
|> should.equal(404)
}

pub fn page_session_show_test() {
let request = testing.get("/session", [])

let response = router.handle_request(request)

response.status |> should.equal(200)

response |> testing.string_body |> should.equal("Welcome Okane")
}

0 comments on commit b09d5e0

Please sign in to comment.