olive 🫒

Package Version Hex Docs

Table of content

Objectives

Olive is a development tool to help working on a classic GLEAM server that renders HTML. Olive takes care of launching your server as well as a proxy to enable live reloading features.

⚠️ Olive has some limitations, is in active development. Some feature might still be rough around the edges, be aware!

Make sure to checkout the help for any configuration options with gleam run -m olive -- --help

Current limitations

Current limitations are:

Installation and usage

Example

This is a stripped example to show the relevant parts only. Be sure to check the docs for wisp / mist / any other lib that allows to have a server running on the BEAM.

Let’s say you did a gleam new my_project.

In src/my_project.gleam:

pub fn main() {
  wisp.configure_logger()

  let assert Ok(_) =
    wisp_mist.handler(router.handle_request, "secret_key")
    |> mist.new
    |> mist.port(3000)
    |> mist.start_http

  process.sleep_forever()
}

In src/my_project/router.gleam:

pub fn handle_request(_req: Request) -> Response {
  response.new(200)
  |> response.set_body(mist.Bytes(bytes_tree.from_string(html("Hello world"))))
}

fn html(content: String) {
  "<html>
    <head>
      <title>My project</title>
    </head>
    <body>"
    <> content
    <> "</body>
    </html>"
}

Now, after installing olive, you can run gleam run -m olive, and the following will happen:

  1. Your server will be listening on localhost:3000
  2. The olive server will be listening on localhost:1234

Open localhost:1234, and you should be granted with a Hello world.

Now update the file in router.gleam so the server sends back Hello olive, and voila! Your browser should refresh automatically after a quick rebuild and show you the new message 🎉

Technicalities

Olive works by:

  1. Spawning your gleam code:
spawn_link("<your_project>@@main", run, ["<your_project>"]).

With your_project being the name defined in gleam.toml.

  1. Launching a proxy that reroutes all requests to your server.
  2. Looping on file change events, trigger a new build, a code reload, and sending a websocket message for connected browsers to reload.

Code reload

Some limitations deny Olive from simply restarting your server on code changes. (some processes hang indefinitely instead of being killed when trying to restart everything)

To counter this, Olive hot reloads any modified modules, using the code module of erlang. This can cause some issue with global state and how updated code works with it. In case of strange bug, try to restart olive!

This code reload cannot work on your main file because it is an init function that only runs once. In case you modify it, olive crashes to let you know you have to restart it.

Search Document