Guide

Getting started

Getting started

Whiplash is an HTTP server for Pooka. One handler serves HTTP/1.1 and HTTP/2 on the same port; with a certificate it serves HTTP/3 too.

A first server

sh
pooka add whiplash
pooka add pooka_http
pooka add pooka_otp
Pooka
module main

import pooka/http
import pooka/otp
import whiplash

handle(request: http.Request) -> http.Response {
  match #(request.method, request.segments()) {
    #(http.Get, []) -> http.text(200, "Hello from Whiplash!")
    #(http.Get, ["hello", name]) -> http.text(200, "Hello, " + name + "!")
    _ -> http.text(404, "Not found")
  }
}

main() {
  let assert Ok(_) = whiplash.start(whiplash.options(8080), handle)
  otp.sleep_forever()
}
sh
pooka run
curl http://127.0.0.1:8080/hello/Joe
curl --http2-prior-knowledge http://127.0.0.1:8080/

Requests and responses are pooka/http's: match on request.method and request.segments(), read request.query_param, request.header and request.text(), answer with http.text, http.html, http.json or a http.Response.

Next