馃 A practical web framework for Gleam
1![Wisp](https://github.com/lpil/wisp/blob/main/docs/images/cover.png?raw=true) 2 3[![Package Version](https://img.shields.io/hexpm/v/wisp)](https://hex.pm/packages/wisp) 4[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/wisp/) 5 6Wisp is a practical Gleam web framework for rapid development and easy maintenance. 7We worry about the hassle of web development, and you focus on writing your 8application. 9 10It is based around two concepts: handlers and middleware. 11 12# Handlers 13 14A handler is a function that takes a HTTP request and returns a HTTP 15response. A handler may also take other arguments, such as a "context" type 16defined in your application which may hold other state such as a database 17connection or user session. 18 19```gleam 20import wisp.{type Request, type Response} 21 22pub type Context { 23 Context(secret: String) 24} 25 26pub fn handle_request(request: Request, context: Context) -> Response { 27 wisp.ok() 28} 29``` 30 31# Middleware 32 33A middleware is a function that takes a response returning function as its 34last argument, and itself returns a response. As with handlers both 35middleware and the functions they take as an argument may take other 36arguments. 37 38Middleware can be applied in a handler with Gleam's `use` syntax. Here the 39`log_request` middleware is used to log a message for each HTTP request 40handled, and the `serve_static` middleware is used to serve static files 41such as images and CSS. 42 43```gleam 44import wisp.{type Request, type Response} 45 46pub fn handle_request(request: Request) -> Response { 47 use <- wisp.log_request(request) 48 use <- wisp.serve_static(request, under: "/static", from: "/public") 49 wisp.ok() 50} 51``` 52 53# Learning Wisp 54 55The Wisp examples are a good place to start. They cover various scenarios and 56include comments and tests. 57 58- [Hello, World!](https://github.com/lpil/wisp/tree/main/examples/00-hello-world) 59- [Routing](https://github.com/lpil/wisp/tree/main/examples/01-routing) 60- [Working with form data](https://github.com/lpil/wisp/tree/main/examples/02-working-with-form-data) 61- [Working with JSON](https://github.com/lpil/wisp/tree/main/examples/03-working-with-json) 62- [Working with other formats](https://github.com/lpil/wisp/tree/main/examples/04-working-with-other-formats) 63- [Using a database](https://github.com/lpil/wisp/tree/main/examples/05-using-a-database) 64- [Serving static assets](https://github.com/lpil/wisp/tree/main/examples/06-serving-static-assets) 65- [Logging](https://github.com/lpil/wisp/tree/main/examples/07-logging) 66- [Working with cookies](https://github.com/lpil/wisp/tree/main/examples/08-working-with-cookies) 67- [Configuring default responses](https://github.com/lpil/wisp/tree/main/examples/09-configuring-default-responses) 68- [Working with files](https://github.com/lpil/wisp/tree/main/examples/10-working-with-files) 69 70API documentation is available on [HexDocs](https://hexdocs.pm/wisp/). 71 72# Wisp applications 73 74These open source Wisp applications may be useful examples. 75 76- [https://packages.gleam.run/](https://github.com/gleam-lang/packages): A HTML 77 serving application that uses an SQLite + LiteFS database, deployed to Fly.io.