/// The v0 demo: a supervised HTTP server rendering pages on the server, /// with argon2 logins (issue #12). /// /// main (supervisor, one_for_one) /// └── web: the HTTP listener ─ linked ─ store: users and sessions /// └── one actor per connection, running `app.route` /// /// `PORT` chooses the port (default 8080; 0 picks a free one). The port /// is printed first. `GET /admin/restart` kills the web actor: the /// supervisor restarts it (and, through the link, the store). /// `MAX_CONNECTIONS`, if set, makes the web actor stop after accepting /// that many connections, and the program exit (load tests use it to /// check the leak oracle at exit). If either is set but not a number, /// the program says so and stops. import app. {type log, type users, add_user, find_user, log, new_session, session_user} import std/actor import std/ffi.{type Resource} import std/http.{Settings} import std/io import std/net.{type TcpListener} import std/os import std/process.{type Subject, type proc} import std/random.{type random} import std/result import std/string import std/supervisor.{Child, OneForOne} import std/test.{assert_equal} import store.{AddUser, FindUser, NewSession, SessionUser} pub fn main :: -> () / { match settings () { Ok((port, connections)) -> start port connections , Error(e) -> io.eprintln e } } /// `PORT` and `MAX_CONNECTIONS`, or why one of them is not a number. fn settings :: -> Result<(Int, Int), String> / { use port <- result.try (int_variable "PORT" 10800) use connections <- result.try (int_variable "MAX_CONNECTIONS" (-1)) Ok((port, connections)) } /// The environment variable `name` as an Int, or `default` if it is not /// set. fn int_variable :: name: String default: Int -> Result / { int_setting name (os.env name) default } /// `value`, the value of variable `name`, as an Int, or `default` if /// there is none. fn int_setting :: name: String value: Option default: Int -> Result { match value { None -> Ok(default) , Some(text) -> string.to_int text |> result.map_error fn e { "{name}: {e}" } } } @test fn an_unset_variable_takes_its_default :: { assert_equal (int_setting "PORT" None 8080) Ok(8080) } @test fn a_set_variable_must_be_a_number :: { assert_equal (int_setting "PORT" Some("0") 8080) Ok(0) assert_equal (int_setting "MAX_CONNECTIONS" Some("-1") 5) Ok(-1) assert_equal (int_setting "PORT" Some("80a") 8080) Error("PORT: not an integer: \"80a\"") assert_equal (int_setting "PORT" Some("") 8080) Error("PORT: not an integer: \"\"") } fn start :: port: Int connections: Int -> () / { match http.listen "127.0.0.1" port { Ok(server) -> { io.println "listening on {net.port server}" // The supervisor owns the socket, so a restarted listener keeps the // same port. It returns when the web actor stops normally. supervisor.supervise OneForOne [Child("web", fn { web server connections })] 10 } , Error(e) -> io.eprintln "cannot listen on port {port}: {e}" } } fn web :: server: Resource connections: Int -> () / { let db = store.start_linked () let me = process.self () io.println "web: ready" use req <- http.serve_with server Settings(..http.defaults (), connections: connections) if req.path == "/admin/restart" { process.kill me http.text 200 "restarting the web server" } else { use <- with_console_log use <- with_store db app.route req } } /// `log` to stdout. fn with_console_log :: action: fn() -> a / -> a / { handle { action () } with { log message -> io.println "[log] {message}" } } /// `users` from the store actor. fn with_store :: db: Subject action: fn() -> a / -> a / { handle { action () } with { find_user name -> actor.call db FindUser(name, _) 5000 , add_user user -> actor.call db AddUser(user, _) 5000 , new_session name -> actor.call db NewSession(name, _) 5000 , session_user token -> actor.call db SessionUser(token, _) 5000 } }