Packages
mist
0.3.1
6.0.3
6.0.2
6.0.1
6.0.0
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
5.0.0-rc1
4.0.7
4.0.6
4.0.5
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
3.0.0
2.0.0
1.2.0
1.1.0
1.0.0
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.17.0
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
a misty Gleam web server
Current section
Files
Jump to
Current section
Files
src/mist.gleam
import gleam/option.{None, Option}
import gleam/otp/process
import glisten
import glisten/tcp
import gleam/http
import gleam/http/request.{Request}
import gleam/http/response
import mist/http as mhttp
import gleam/erlang
/// Helper that wraps the `glisten.serve` with no state. If you want to just
/// write HTTP handler(s), this is what you want
pub fn serve(
port: Int,
handler: tcp.LoopFn(Option(process.Timer)),
) -> Result(Nil, glisten.StartError) {
glisten.serve(port, handler, None)
}
pub fn main() {
let empty_response =
response.new(200)
|> response.set_body(<<>>)
let not_found =
response.new(404)
|> response.set_body(<<>>)
assert Ok(_) =
serve(
8080,
mhttp.handler(fn(req: Request(BitString)) {
case req.method, request.path_segments(req) {
http.Get, [] -> empty_response
http.Get, ["user", id] ->
response.new(200)
|> response.set_body(<<id:utf8>>)
http.Post, ["user"] ->
response.new(200)
|> response.set_body(req.body)
_, _ -> not_found
}
}),
)
erlang.sleep_forever()
}