-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |