fix report a bad HITO_PORT as a configuration error

An unparseable HITO_PORT raised an uncaught Failure from int_of_string and the server died with a stack trace that never named the variable. The port is now parsed and range-checked, and a bad value stops the server with one line naming HITO_PORT and the offending value.

Commit
a3ab401264ad98d27e3fecfdf7783a3e86c110c7
Author
Marius Peter <dev@marius-peter.com>
Author date
Committer
Marius Peter <dev@marius-peter.com>
Committer date
bin/main.ml
index 9de8b90e..25914b03 100644..100644
@@ -36,9 +36,26 @@
36 36 let getenv name default =
37 37 match Sys.getenv_opt name with Some v when v <> "" -> v | _ -> default
38 38
39 Added: (* A configuration value that cannot be read is a deployment mistake. Name the
40 Added: variable and the offending value, then stop, rather than surfacing a raw
41 Added: exception and a stack trace. *)
42 Added: let fail_config fmt =
43 Added: Printf.ksprintf
44 Added: (fun message ->
45 Added: prerr_endline ("hito: " ^ message);
46 Added: exit 2)
47 Added: fmt
48 Added:
49 Added: let port_of_env () =
50 Added: let raw = getenv "HITO_PORT" "8080" in
51 Added: match int_of_string_opt raw with
52 Added: | Some port when port >= 1 && port <= 65535 -> port
53 Added: | _ ->
54 Added: fail_config "HITO_PORT must be a TCP port between 1 and 65535, got %S" raw
55 Added:
39 56 let () =
40 57 let db_uri = getenv "HITO_DB" "sqlite3:local_db/hito.sqlite" in
41 Removed: let port = int_of_string (getenv "HITO_PORT" "8080") in
58 Added: let port = port_of_env () in
42 59 let secret =
43 60 match Sys.getenv_opt "HITO_SECRET" with
44 61 | Some s when s <> "" -> s