louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 1 | /// The v0 demo: a supervised HTTP server rendering pages on the server, |
| 2 | /// with argon2 logins (issue #12). |
| 3 | /// |
| 4 | /// main (supervisor, one_for_one) |
| 5 | /// └── web: the HTTP listener ─ linked ─ store: users and sessions |
| 6 | /// └── one actor per connection, running `app.route` |
| 7 | /// |
| 8 | /// `PORT` chooses the port (default 8080; 0 picks a free one). The port |
| 9 | /// is printed first. `GET /admin/restart` kills the web actor: the |
| 10 | /// supervisor restarts it (and, through the link, the store). |
| 11 | /// `MAX_CONNECTIONS`, if set, makes the web actor stop after accepting |
| 12 | /// that many connections, and the program exit (load tests use it to |
| 13 | /// check the leak oracle at exit). If either is set but not a number, |
| 14 | /// the program says so and stops. |
| 15 | |
| 16 | import app. |
| 17 | {type log, type users, add_user, find_user, log, new_session, session_user} |
| 18 | import std/actor |
| 19 | import std/ffi.{type Resource} |
| 20 | import std/http.{Settings} |
| 21 | import std/io |
| 22 | import std/net.{type TcpListener} |
| 23 | import std/os |
| 24 | import std/process.{type Subject, type proc} |
| 25 | import std/random.{type random} |
| 26 | import std/result |
| 27 | import std/string |
| 28 | import std/supervisor.{Child, OneForOne} |
| 29 | import std/test.{assert_equal} |
| 30 | import store.{AddUser, FindUser, NewSession, SessionUser} |
| 31 | |
| 32 | pub fn main :: -> () / <io, proc> |
| 33 | { |
| 34 | match settings () |
| 35 | { Ok((port, connections)) -> start port connections |
| 36 | , Error(e) -> io.eprintln e |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// `PORT` and `MAX_CONNECTIONS`, or why one of them is not a number. |
| 41 | fn settings :: -> Result<(Int, Int), String> / <io> |
| 42 | { |
| 43 | use port <- result.try (int_variable "PORT" 10800) |
| 44 | use connections <- result.try (int_variable "MAX_CONNECTIONS" (-1)) |
| 45 | Ok((port, connections)) |
| 46 | } |
| 47 | |
| 48 | /// The environment variable `name` as an Int, or `default` if it is not |
| 49 | /// set. |
| 50 | fn int_variable :: name: String default: Int -> Result<Int, String> / <io> |
| 51 | { |
| 52 | int_setting name (os.env name) default |
| 53 | } |
| 54 | |
| 55 | /// `value`, the value of variable `name`, as an Int, or `default` if |
| 56 | /// there is none. |
| 57 | fn int_setting :: |
| 58 | name: String |
| 59 | value: Option<String> |
| 60 | default: Int |
| 61 | -> Result<Int, String> |
| 62 | { |
| 63 | match value |
| 64 | { None -> Ok(default) |
| 65 | , Some(text) -> string.to_int text |> result.map_error fn e { "{name}: {e}" } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @test |
| 70 | fn an_unset_variable_takes_its_default :: |
| 71 | { |
| 72 | assert_equal (int_setting "PORT" None 8080) Ok(8080) |
| 73 | } |
| 74 | |
| 75 | @test |
| 76 | fn a_set_variable_must_be_a_number :: |
| 77 | { |
| 78 | assert_equal (int_setting "PORT" Some("0") 8080) Ok(0) |
| 79 | assert_equal (int_setting "MAX_CONNECTIONS" Some("-1") 5) Ok(-1) |
| 80 | assert_equal |
| 81 | (int_setting "PORT" Some("80a") 8080) |
| 82 | Error("PORT: not an integer: \"80a\"") |
| 83 | assert_equal |
| 84 | (int_setting "PORT" Some("") 8080) |
| 85 | Error("PORT: not an integer: \"\"") |
| 86 | } |
| 87 | |
| 88 | fn start :: port: Int connections: Int -> () / <io, proc> |
| 89 | { |
| 90 | match http.listen "127.0.0.1" port |
| 91 | { Ok(server) -> |
| 92 | { |
| 93 | io.println "listening on {net.port server}" |
| 94 | // The supervisor owns the socket, so a restarted listener keeps the |
| 95 | // same port. It returns when the web actor stops normally. |
| 96 | supervisor.supervise |
| 97 | OneForOne |
| 98 | [Child("web", fn { web server connections })] |
| 99 | 10 |
| 100 | } |
| 101 | , Error(e) -> io.eprintln "cannot listen on port {port}: {e}" |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn web :: |
| 106 | server: Resource<TcpListener> |
| 107 | connections: Int |
| 108 | -> () / <proc, random, io, ffi> |
| 109 | { |
| 110 | let db = store.start_linked () |
| 111 | let me = process.self () |
| 112 | io.println "web: ready" |
| 113 | use req <- |
| 114 | http.serve_with |
| 115 | server |
| 116 | Settings(..http.defaults (), connections: connections) |
| 117 | if req.path == "/admin/restart" |
| 118 | { |
| 119 | process.kill me |
| 120 | http.text 200 "restarting the web server" |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | use <- with_console_log |
| 125 | use <- with_store db |
| 126 | app.route req |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// `log` to stdout. |
| 131 | fn with_console_log :: action: fn() -> a / <log, io | e> -> a / <io | e> |
| 132 | { |
| 133 | handle |
| 134 | { |
| 135 | action () |
| 136 | } |
| 137 | with |
| 138 | { log message -> io.println "[log] {message}" } |
| 139 | } |
| 140 | |
| 141 | /// `users` from the store actor. |
| 142 | fn with_store :: |
| 143 | db: Subject<store.Msg> |
| 144 | action: fn() -> a / <users, proc | e> |
| 145 | -> a / <proc | e> |
| 146 | { |
| 147 | handle |
| 148 | { |
| 149 | action () |
| 150 | } |
| 151 | with |
| 152 | { find_user name -> actor.call db FindUser(name, _) 5000 |
| 153 | , add_user user -> actor.call db AddUser(user, _) 5000 |
| 154 | , new_session name -> actor.call db NewSession(name, _) 5000 |
| 155 | , session_user token -> actor.call db SessionUser(token, _) 5000 |
| 156 | } |
| 157 | } |