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) ?(timeout=0.0)
?(buf_size=16 * 1_024) ?(buf_size=16 * 1_024)
?(new_thread=(fun f -> ignore (Thread.create f () : Thread.t))) ?(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 handler _req = Response.fail ~code:404 "no top handler" in
let max_connections = max 4 max_connections 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; running= true; sem_max_connections=Sem_.create max_connections;
path_handlers=[]; timeout; path_handlers=[]; timeout;
middlewares=[]; middlewares_sorted=lazy []; middlewares=[]; middlewares_sorted=lazy [];
} } in
List.iter (fun (stage,m) -> add_middleware self ~stage m) middlewares;
self
let stop s = s.running <- false let stop s = s.running <- false

View file

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