馃 A practical web framework for Gleam
1
2
3[](https://hex.pm/packages/wisp)
4[](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.{Request, 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.{Request, Response}
45
46pub fn handle_request(request: Request) -> Response {
47 use <- wisp.log_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/0-hello-world)
59- [Routing](https://github.com/lpil/wisp/tree/main/examples/1-routing)
60- [Working with form data](https://github.com/lpil/wisp/tree/main/examples/2-working-with-form-data)
61- [Working with JSON](https://github.com/lpil/wisp/tree/main/examples/3-working-with-json)
62- [Working with other formats](https://github.com/lpil/wisp/tree/main/examples/4-working-with-other-formats)
63- [Using a database](https://github.com/lpil/wisp/tree/main/examples/5-using-a-database)
64- [Serving static assets](https://github.com/lpil/wisp/tree/main/examples/6-serving-static-assets)
65- [Logging](https://github.com/lpil/wisp/tree/main/examples/7-logging)
66- [Working with cookies](https://github.com/lpil/wisp/tree/main/examples/8-working-with-cookies)
67- [Configuring default responses](https://github.com/lpil/wisp/tree/main/examples/9-configuring-default-responses)
68
69API documentation is available on [HexDocs](https://hexdocs.pm/wisp/).
70
71# Wisp applications
72
73These open source Wisp applications may be useful examples.
74
75- [https://packages.gleam.run/](https://github.com/gleam-lang/packages): A HTML
76 serving application that uses an SQLite + LiteFS database, deployed to Fly.io.