[OCaml] Mobile-friendly clone of cgit.
Add configurable host and port to config system
- Add host (default 127.0.0.1) and port (default 8081) fields to Config.t - Parse host/port from config.toml with backward-compatible defaults - Pass interface and port to Dream.run - Remove Config.default_port in favor of config record fields - Add test for backward compatibility (missing host/port keys) - Downgrade dune lang to 3.20 to match installed toolchain
Changed files
dune-project
@@ -1,4 +1,4 @@
1
Removed:
(lang dune 3.24)
1
Added:
(lang dune 3.20)
2
2
3
3
(name ogit)
4
4
lib/config.ml
@@ -7,6 +7,8 @@
7
7
default_branch : string;
8
8
git_project_root : string;
9
9
commits_max_displayed : int;
10
Added:
host : string;
11
Added:
port : int;
10
12
}
11
13
12
14
let default =
@@ -15,6 +17,8 @@
15
17
default_branch = "master";
16
18
git_project_root = Filename.concat (Sys.getenv "HOME") "git";
17
19
commits_max_displayed = 10;
20
Added:
host = "127.0.0.1";
21
Added:
port = 8081;
18
22
}
19
23
20
24
let locate_config_file () =
@@ -37,6 +41,8 @@
37
41
("default_branch", TString t.default_branch);
38
42
("git_project_root", TString t.git_project_root);
39
43
("commits_max_displayed", TInt t.commits_max_displayed);
44
Added:
("host", TString t.host);
45
Added:
("port", TInt t.port);
40
46
]
41
47
|> Min.of_key_values
42
48
@@ -63,15 +69,28 @@
63
69
| Some _ -> Error ("Expected int for key: " ^ key)
64
70
| None -> Error ("Missing key: " ^ key)
65
71
in
72
Added:
let find_string_opt key ~default:d =
73
Added:
match Types.Table.find_opt (Min.key key) table with
74
Added:
| Some (TString s) -> Ok s
75
Added:
| Some _ -> Error ("Expected string for key: " ^ key)
76
Added:
| None -> Ok d
77
Added:
in
78
Added:
let find_int_opt key ~default:d =
79
Added:
match Types.Table.find_opt (Min.key key) table with
80
Added:
| Some (TInt i) -> Ok i
81
Added:
| Some _ -> Error ("Expected int for key: " ^ key)
82
Added:
| None -> Ok d
83
Added:
in
66
84
let ( let* ) = Result.bind in
67
85
let* git_project_root = find_string "git_project_root" in
68
86
let* user = find_string "user" in
69
87
let* default_branch = find_string "default_branch" in
70
88
let* commits_max_displayed = find_int "commits_max_displayed" in
71
Removed:
Ok { git_project_root; user; default_branch; commits_max_displayed }
89
Added:
let* host = find_string_opt "host" ~default:default.host in
90
Added:
let* port = find_int_opt "port" ~default:default.port in
91
Added:
Ok { git_project_root; user; default_branch; commits_max_displayed; host; port }
72
92
with _ ->
73
93
prerr_endline "[config.ml] Falling back to default config.";
74
94
Ok default
75
95
76
Removed:
let default_port = 8081
77
96
let config = match read_file () with Ok cfg -> cfg | Error _ -> default
lib/main.ml
@@ -1,5 +1,6 @@
1
1
(* -*- mode: tuareg; -*- *)
2
2
3
3
let run () =
4
Removed:
let port = Config.default_port in
5
Removed:
Dream.run ~port @@ Dream.logger @@ Dream.router Handlers.all_handlers
4
Added:
let port = Config.config.port in
5
Added:
let interface = Config.config.host in
6
Added:
Dream.run ~port ~interface @@ Dream.logger @@ Dream.router Handlers.all_handlers
test/test_ogit.ml
@@ -7,6 +7,8 @@
7
7
default_branch = "main";
8
8
git_project_root = "/srv/git";
9
9
commits_max_displayed = 25;
10
Added:
host = "127.0.0.1";
11
Added:
port = 9000;
10
12
}
11
13
in
12
14
Ogit.Config.write_file ~file (Ogit.Config.to_table config);
@@ -14,6 +16,18 @@
14
16
| Ok config' -> assert (config' = config)
15
17
| Error message -> failwith message
16
18
19
Added:
let test_config_backward_compat () =
20
Added:
let file = Filename.temp_file "ogit" ".toml" in
21
Added:
let oc = open_out file in
22
Added:
Printf.fprintf oc
23
Added:
"user = \"bob\"\ndefault_branch = \"main\"\ngit_project_root = \"/srv/git\"\ncommits_max_displayed = 10\n";
24
Added:
close_out oc;
25
Added:
match Ogit.Config.read_file ~file () with
26
Added:
| Ok config ->
27
Added:
assert (config.host = "127.0.0.1");
28
Added:
assert (config.port = 8081)
29
Added:
| Error message -> failwith message
30
Added:
17
31
let test_config_location () =
18
32
if Sys.getenv_opt "OGIT_CONFIG" = None then (
19
33
Unix.putenv "XDG_CONFIG_HOME" "/tmp/xdg-config";
@@ -131,6 +145,7 @@
131
145
assert (Ogit.Resolvers.Reference.tag_name "refs/tags/v1.0.0" = Some "v1.0.0");
132
146
assert (Ogit.Resolvers.Reference.tag_name "refs/heads/v1.0.0" = None);
133
147
test_config_round_trip ();
148
Added:
test_config_backward_compat ();
134
149
test_config_location ();
135
150
test_description_reader ();
136
151
test_repository_layout ();