Skip to content

Commit 4703287

Browse files
committed
httpev: add setup_bind_lwt function
setup http_server with http_config binded to systemd socket (by default) or to provided addr (host:port)
1 parent 6c1b06d commit 4703287

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

httpev.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ let default =
7979

8080
include Httpev_common
8181

82+
type http_server_bind =
83+
| Systemd of Lwt_unix.file_descr
84+
| Connection of Unix.sockaddr
85+
8286
type partial_body = {
8387
line1 : string;
8488
content_length : int option;
@@ -1119,6 +1123,24 @@ let setup_lwt config answer =
11191123
let server_lwt config answer =
11201124
Lwt_main.run @@ setup_lwt config answer
11211125

1126+
let setup_bind_lwt addr config answer =
1127+
let connection =
1128+
match addr, Systemd.Daemon.listen_fds_lwt () with
1129+
| None, [] -> Exn.fail "bind not provided and no systemd socket available"
1130+
| None, fd :: fds ->
1131+
if fds <> [] then
1132+
log#warn "more than one fd is provided by systemd, only the first one is used and the other ones are ignored";
1133+
Systemd fd
1134+
| Some addr, _ -> Connection addr
1135+
in
1136+
match connection with
1137+
| Systemd fd ->
1138+
log#info "starting httpev in systemd mode";
1139+
setup_fd_lwt fd config answer
1140+
| Connection connection ->
1141+
log#info "starting httpev on %s" (Nix.show_addr connection);
1142+
setup_lwt { config with connection } answer
1143+
11221144
module Answer = struct
11231145

11241146
let return ?(status=`Ok) ?(extra=[]) ~typ data : [> `Body of reply_status reply' ] Lwt.t =

0 commit comments

Comments
 (0)