feat: add ?middlewares param to create

This commit is contained in:
Simon Cruanes 2021-12-11 16:48:16 -05:00
parent e05e8867db
commit 5827328993
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
2 changed files with 11 additions and 3 deletions

View file

@ -989,14 +989,19 @@ let create
?(timeout=0.0)
?(buf_size=16 * 1_024)
?(new_thread=(fun f -> ignore (Thread.create f () : Thread.t)))
?(addr="127.0.0.1") ?(port=8080) ?sock () : t =
?(addr="127.0.0.1") ?(port=8080) ?sock
?(middlewares=[])
() : t =
let handler _req = Response.fail ~code:404 "no top handler" in
let max_connections = max 4 max_connections in
{ new_thread; addr; port; sock; masksigpipe; handler; buf_size;
let self = {
new_thread; addr; port; sock; masksigpipe; handler; buf_size;
running= true; sem_max_connections=Sem_.create max_connections;
path_handlers=[]; timeout;
middlewares=[]; middlewares_sorted=lazy [];
}
} in
List.iter (fun (stage,m) -> add_middleware self ~stage m) middlewares;
self
let stop s = s.running <- false

View file

@ -496,6 +496,7 @@ val create :
?addr:string ->
?port:int ->
?sock:Unix.file_descr ->
?middlewares:([`Encoding | `Stage of int] * Middleware.t) list ->
unit ->
t
(** Create a new webserver.
@ -513,6 +514,8 @@ val create :
new client connection. By default it is {!Thread.create} but one
could use a thread pool instead.
@param middlewares see {!add_middleware} for more details.
@param max_connections maximum number of simultaneous connections.
@param timeout connection is closed if the socket does not do read or
write for the amount of second. Default: 0.0 which means no timeout.