tobogganSe connecter

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.

159 lignes · 4419 octetsBrut · Historique
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
16import app.
17{type log, type users, add_user, find_user, log, new_session, session_user}
18import std/actor
19import std/ffi.{type Resource}
20import std/http.{Settings}
21import std/io
22import std/net.{type TcpListener, type net}
23import std/os
24import std/process.{type Subject, type proc}
25import std/random.{type random}
26import std/result
27import std/string
28import std/supervisor.{Child, OneForOne}
29import std/test.{assert_equal}
30import store.{AddUser, FindUser, NewSession, SessionUser}
31
32pub fn main :: -> () / <io, proc, net>
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.
41fn 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.
50fn 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.
57fn 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
70fn an_unset_variable_takes_its_default ::
71{
72 assert_equal (int_setting "PORT" None 8080) Ok(8080)
73}
74
75@test
76fn 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
88fn start :: port: Int connections: Int -> () / <io, proc, net>
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
105fn web ::
106 server: Resource<TcpListener>
107 connections: Int
108 // Web orchestration reads no file directly. `use req <- http.serve_with`
109 // supplies a separate handler actor whose full callback row includes fs/net.
110 -> () / <proc, random, io, ffi>
111{
112 let db = store.start_linked ()
113 let me = process.self ()
114 io.println "web: ready"
115 use req <-
116 http.serve_with
117 server
118 Settings(..http.defaults (), connections: connections)
119 if req.path == "/admin/restart"
120 {
121 process.kill me
122 http.text 200 "restarting the web server"
123 }
124 else
125 {
126 use <- with_console_log
127 use <- with_store db
128 app.route req
129 }
130}
131
132/// `log` to stdout.
133fn with_console_log :: action: fn() -> a / <log, io | e> -> a / <io | e>
134{
135 handle
136 {
137 action ()
138 }
139 with
140 { log message -> io.println "[log] {message}" }
141}
142
143/// `users` from the store actor.
144fn with_store ::
145 db: Subject<store.Msg>
146 action: fn() -> a / <users, proc | e>
147 -> a / <proc | e>
148{
149 handle
150 {
151 action ()
152 }
153 with
154 { find_user name -> actor.call db FindUser(name, _) 5000
155 , add_user user -> actor.call db AddUser(user, _) 5000
156 , new_session name -> actor.call db NewSession(name, _) 5000
157 , session_user token -> actor.call db SessionUser(token, _) 5000
158 }
159}