Skip to content

roc-lang/basic-webserver

Repository files navigation

📖 docs: main branch

👀 examples: 0.4, 0.3, 0.2, 0.1

Basic Web Server for Roc

A webserver platform with a simple interface.

Write a function which takes a Http.Request, perform I/O like fetching content or reading environment variables, and return a Http.Response. It's that easy!

Behind the scenes, basic-webserver uses Rust's high-performance hyper and tokio libraries to execute your Roc function on incoming requests.

⚠️ On linux --linker=legacy is necessary for this package because of this Roc issue.

Example

Hello world webserver:

app "helloweb"
    packages { pf: "https://github.com/roc-lang/basic-webserver/releases/download/0.4.0/iAiYpbs5zdVB75golcg_YMtgexN3e2fwhsYPLPCeGzk.tar.br" }
    imports [
        pf.Stdout,
        pf.Task.{ Task },
        pf.Http.{ Request, Response },
        pf.Utc,
    ]
    provides [main] to pf

main : Request -> Task Response []
main = \req ->

    # Log request date, method and url
    date <- Utc.now |> Task.map Utc.toIso8601Str |> Task.await
    {} <- Stdout.line "$(date) $(Http.methodToStr req.method) $(req.url)" |> Task.await

    Task.ok { status: 200, headers: [], body: Str.toUtf8 "<b>Hello, world!</b>\n" }

Run this example server with $ roc run helloweb.roc --linker=legacy and go to http://localhost:8000 in your browser.

Contributing

If you'd like to contribute, check out our group chat and let us know what you're thinking, we're friendly!

Steps to re-generate glue

Run the following from the repository root directory.

  1. Run bash platform/glue-gen.sh
  2. Manually fix any issues with glue generated code in platform/glue-manual/*.rs, this is a temporary workaround and should not be needed in future