Skip to content

Commit

Permalink
feat: add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjhan committed Jan 5, 2025
1 parent caa4b0d commit 16400ea
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ version = "1.0.0"
gleam_stdlib = ">= 0.52.0 and < 1.0.0"
gleam_json = ">= 2.3.0 and < 3.0.0"
gleam_http = ">= 3.7.2 and < 4.0.0"
mist = ">= 4.0.4 and < 5.0.0"
gleam_erlang = ">= 0.33.0 and < 1.0.0"
gleam_otp = ">= 0.16.0 and < 1.0.0"
gleam_yielder = ">= 1.1.0 and < 2.0.0"
wisp = ">= 1.5.1 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
10 changes: 9 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# You typically do not need to edit this file

packages = [
{ name = "directories", version = "1.1.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "BDA521A4EB9EE3A7894F0DC863797878E91FF5C7826F7084B2E731E208BDB076" },
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_http", version = "3.7.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8A70D2F70BB7CFEB5DF048A2183FFBA91AF6D4CF5798504841744A16999E33D2" },
Expand All @@ -14,8 +18,12 @@ packages = [
{ name = "gramps", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "630BDE35E465511945253A06EBCDE8D5E4B8B1988F4AC6B8FAC297DEF55B4CA2" },
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
{ name = "mist", version = "4.0.4", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gleam_yielder", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "9F87DA535A75AA1B8B37D27E59B1E60A1BE34EB2CA63BE8B211D75E5D10BEEAF" },
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
{ name = "wisp", version = "1.5.1", build_tools = ["gleam"], requirements = ["directories", "exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "DB7968F777CA77B41AAC8067A5151B022E857E1EECF16BFC9D5F914D0F628844" },
]

[requirements]
Expand All @@ -26,4 +34,4 @@ gleam_otp = { version = ">= 0.16.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.52.0 and < 1.0.0" }
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
mist = { version = ">= 4.0.4 and < 5.0.0" }
wisp = { version = ">= 1.5.1 and < 2.0.0" }
16 changes: 16 additions & 0 deletions src/app/router.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import app/web
import gleam/string_tree
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)

// Later we'll use templates, but for now a string will do.
let body = string_tree.from_string("<h1>Hello, Joe!</h1>")

// Return a 200 OK response with the body and a HTML content type.
wisp.html_response(body, 200)
}
32 changes: 32 additions & 0 deletions src/app/web.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import wisp

/// The middleware stack that the request handler uses. The stack is itself a
/// middleware function!
///
/// Middleware wrap each other, so the request travels through the stack from
/// top to bottom until it reaches the request handler, at which point the
/// response travels back up through the stack.
///
/// The middleware used here are the ones that are suitable for use in your
/// typical web application.
///
pub fn middleware(
req: wisp.Request,
handle_request: fn(wisp.Request) -> wisp.Response,
) -> wisp.Response {
// Permit browsers to simulate methods other than GET and POST using the
// `_method` query parameter.
let req = wisp.method_override(req)

// Log information about the request and response.
use <- wisp.log_request(req)

// Return a default 500 response if the request handler crashes.
use <- wisp.rescue_crashes

// Rewrite HEAD requests to GET requests and return an empty body.
use req <- wisp.handle_head(req)

// Handle the request!
handle_request(req)
}
27 changes: 24 additions & 3 deletions src/prism.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import gleam/io
import app/router
import gleam/erlang/process
import mist
import wisp
import wisp/wisp_mist

pub fn main() {
io.println("Hello from prism!")
}
// This sets the logger to print INFO level logs, and other sensible defaults
// for a web application.
wisp.configure_logger()

// Here we generate a secret key, but in a real application you would want to
// load this from somewhere so that it is not regenerated on every restart.
let secret_key_base = wisp.random_string(64)

// Start the Mist web server.
let assert Ok(_) =
wisp_mist.handler(router.handle_request, secret_key_base)
|> mist.new
|> mist.port(8000)
|> mist.start_http

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

0 comments on commit 16400ea

Please sign in to comment.