Packages
mist
0.8.3
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
README.md
# mist
A (hopefully) nice, pure Gleam web server
## Installation
This package can be added to your Gleam project:
```sh
gleam add mist
```
and its documentation can be found at <https://hexdocs.pm/mist>.
## Usage
Right now there are a few options. Let's say you want a "simple" HTTP server
that you can customize to your heart's content. In that case, you want:
```gleam
import mist
import gleam/bit_builder
import gleam/erlang/process
import gleam/http/response
pub fn main() {
assert Ok(_) =
mist.run_service(
8080,
fn(_req) {
response.new(200)
|> response.set_body(bit_builder.from_bit_string(<<
"hello, world!":utf8,
>>))
},
max_body_limit: 4_000_000
)
process.sleep_forever()
}
```
Maybe you also want to work with websockets. Maybe those should only be
upgradable at a certain endpoint. For that, you can use `handler.with_func`.
The websocket methods help you build a handler with connect/disconnect handlers.
You can use these to, for example, track connected clients. For example:
```gleam
import mist
import gleam/bit_builder
import gleam/erlang/process
import gleam/http.{Get, Post}
import gleam/http/request
import gleam/http/response
import gleam/result
import mist/handler.{Response, Upgrade}
import mist/http.{BitBuilderBody}
import mist/websocket
pub fn main() {
assert Ok(_) =
mist.serve(
8080,
handler.with_func(fn(req) {
case req.method, request.path_segments(req) {
Get, ["echo", "test"] ->
websocket.echo_handler
|> websocket.with_handler
|> Upgrade
Post, ["echo", "body"] ->
req
|> http.read_body
|> result.map(fn(req) {
response.new(200)
|> response.set_body(BitBuilderBody(bit_builder.from_bit_string(
req.body,
)))
|> response.prepend_header(
"content-type",
request.get_header(req, "content-type")
|> result.unwrap("application/octet-stream"),
)
})
|> result.unwrap(
response.new(400)
|> response.set_body(BitBuilderBody(bit_builder.new())),
)
|> Response
Get, ["home"] ->
response.new(200)
|> response.set_body(BitBuilderBody(bit_builder.from_bit_string(<<
"sup home boy":utf8,
>>)))
|> Response
_, _ ->
response.new(200)
|> response.set_body(BitBuilderBody(bit_builder.from_bit_string(<<
"Hello, world!":utf8,
>>)))
|> Response
}
}),
)
process.sleep_forever()
}
```
There is some initial support for sending files as well:
```gleam
import gleam/bit_builder
import gleam/bit_string
import gleam/erlang/process
import gleam/http/request.{Request}
import gleam/http/response
import gleam/int
import gleam/string
import mist
import mist/file
import mist/handler.{Response}
import mist/http.{BitBuilderBody, Body, FileBody}
pub fn main() {
assert Ok(_) =
mist.serve(
8080,
handler.with_func(fn(req: Request(Body)) {
case request.path_segments(req) {
["static", ..path] -> {
// verify, validate, etc
let file_path =
path
|> string.join("/")
|> string.append("/", _)
|> bit_string.from_string
let size = file.size(file_path)
assert Ok(fd) = file.open(file_path)
response.new(200)
|> response.set_body(FileBody(fd, int.to_string(size), 0, size))
|> Response
}
_ ->
response.new(404)
|> response.set_body(BitBuilderBody(bit_builder.new()))
|> Response
}
}),
)
process.sleep_forever()
}
```
If you need something a little more complex or custom, you can always use the
helpers exported by the various `glisten`/`mist` modules.
## Benchmarks
These are currently located [here](https://github.com/rawhat/http-benchmarks)