Skip to content

Commit

Permalink
Static linking: call register from run functions in extensions
Browse files Browse the repository at this point in the history
to simplify the code written by users
  • Loading branch information
balat committed May 4, 2024
1 parent 524b2e2 commit 78d1825
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 39 deletions.
5 changes: 4 additions & 1 deletion src/extensions/authbasic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ let () =
()

(** Instruction for static linking without config file: *)
let run ~realm ~auth () _ _ _ = gen ~realm ~auth
let instruction ~realm ~auth () _ _ _ = gen ~realm ~auth

let run ?site ~realm ~auth () =
Ocsigen_server.Site.register ?site (instruction ~realm ~auth ())
7 changes: 6 additions & 1 deletion src/extensions/authbasic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ val register_basic_authentication_method : (Xml.xml -> auth) -> unit
from the point of view of plugin developers and is totally
transparent to the plugin. *)

val run : realm:string -> auth:auth -> unit -> Ocsigen_server.Site.instruction
val run :
?site:Ocsigen_server.Site.t
-> realm:string
-> auth:auth
-> unit
-> unit
(** [run ~realm ~auth ()] makes it possible to use this extension without
configuration file. *)

Expand Down
6 changes: 5 additions & 1 deletion src/extensions/cors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ let () =
~fun_site:(fun _ _ _ -> parse_config)
()

let run ?credentials ?max_age ?exposed_headers ?methods () _ _ _ =
let instruction ?credentials ?max_age ?exposed_headers ?methods () _ _ _ =
let credentials = Ocsigen_lib.Option.get' false credentials in
let exposed_headers = Ocsigen_lib.Option.get' [] exposed_headers in
main {credentials; methods; max_age; exposed_headers}

let run ?site ?credentials ?max_age ?exposed_headers ?methods () =
Ocsigen_server.Site.register ?site
(instruction ?credentials ?max_age ?exposed_headers ?methods ())
5 changes: 3 additions & 2 deletions src/extensions/cors.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
val run :
?credentials:bool
?site:Ocsigen_server.Site.t
-> ?credentials:bool
-> ?max_age:int
-> ?exposed_headers:string list
-> ?methods:Cohttp.Code.meth list
-> unit
-> Ocsigen_server.Site.instruction
-> unit
5 changes: 4 additions & 1 deletion src/extensions/deflatemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,7 @@ let () =
~fun_site:(fun _ _ _ _ _ _ -> parse_config)
~init_fun:parse_global_config ()

let run ~mode () _ _ _ = filter mode
let instruction ~mode () _ _ _ = filter mode

let run ?site ~mode () =
Ocsigen_server.Site.register ?site (instruction ~mode ())
5 changes: 3 additions & 2 deletions src/extensions/deflatemod.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ val set_buffer_size : int -> unit
type filter = [`Type of string option * string option | `Extension of string]

val run :
mode:[`All_but of filter list | `Only of filter list]
?site:Ocsigen_server.Site.t
-> mode:[`All_but of filter list | `Only of filter list]
-> unit
-> unit
-> Ocsigen_server.Site.instruction
5 changes: 4 additions & 1 deletion src/extensions/outputfilter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@ let () =
~fun_site:(fun _ _ _ _ _ _ -> parse_config)
()

let run ~mode () _ _ _ =
let instruction ~mode () _ _ _ =
match mode with `Code c -> gen_code c | #header_filter as f -> gen f

let run ?site ~mode () =
Ocsigen_server.Site.register ?site (instruction ~mode ())
11 changes: 6 additions & 5 deletions src/extensions/outputfilter.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
val run :
mode:
[ `Rewrite of Ocsigen_header.Name.t * Re.Pcre.regexp * string
| `Add of Ocsigen_header.Name.t * string * bool option
| `Code of Cohttp.Code.status ]
?site:Ocsigen_server.Site.t
-> mode:
[ `Rewrite of Ocsigen_header.Name.t * Re.Pcre.regexp * string
| `Add of Ocsigen_header.Name.t * string * bool option
| `Code of Cohttp.Code.status ]
-> unit
-> unit
-> Ocsigen_server.Site.instruction
5 changes: 4 additions & 1 deletion src/extensions/redirectmod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,7 @@ let () =
~fun_site:(fun _ _ _ _ _ _ -> parse_config)
()

let run ~redirection () _ _ _ = gen redirection
let instruction ~redirection () _ _ _ = gen redirection

let run ?site ~redirection () =
Ocsigen_server.Site.register ?site (instruction ~redirection ())
2 changes: 1 addition & 1 deletion src/extensions/redirectmod.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ val create_redirection :
-> string
-> redirection

val run : redirection:redirection -> unit -> Ocsigen_server.Site.instruction
val run : ?site:Ocsigen_server.Site.t -> redirection:redirection -> unit -> unit
5 changes: 4 additions & 1 deletion src/extensions/revproxy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,7 @@ let () =
when sending to extensions! *)
()

let run ~redirection () _ _ _ = gen redirection
let instruction ~redirection () _ _ _ = gen redirection

let run ?site ~redirection () =
Ocsigen_server.Site.register ?site (instruction ~redirection ())
2 changes: 1 addition & 1 deletion src/extensions/revproxy.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ val create_redirection :
-> string
-> redirection

val run : redirection:redirection -> unit -> Ocsigen_server.Site.instruction
val run : ?site:Ocsigen_server.Site.t -> redirection:redirection -> unit -> unit
8 changes: 7 additions & 1 deletion src/extensions/rewritemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,16 @@ let () =
~fun_site:(fun _ _ _ _ _ _ -> parse_config)
()

let run ?(continue = false) ?(full_rewrite = false) ~regexp dest () _ _ _ =
let instruction ?(continue = false) ?(full_rewrite = false) ~regexp dest () _ _
_
=
gen
(Regexp
( Ocsigen_lib.Netstring_pcre.regexp ("^" ^ regexp ^ "$")
, dest
, full_rewrite ))
continue

let run ?site ?continue ?full_rewrite ~regexp dest () =
Ocsigen_server.Site.register ?site
(instruction ?continue ?full_rewrite ~regexp dest ())
5 changes: 3 additions & 2 deletions src/extensions/rewritemod.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ val section : Lwt_log_core.section
(** use Lwt_log.Section.set_level in order to debug *)

val run :
?continue:bool
?site:Ocsigen_server.Site.t
-> ?continue:bool
-> ?full_rewrite:bool
-> regexp:string
-> string
-> unit
-> Ocsigen_server.Site.instruction
-> unit
6 changes: 5 additions & 1 deletion src/extensions/staticmod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ let () =
(* Registration for static linking: *)
let preprocess s = "^" ^ s ^ "$"

let run ?dir ?regexp ?dest ?code ?cache ?root () =
let instruction ?dir ?regexp ?dest ?code ?cache ?root () =
let kind =
kind dir
(Ocsigen_lib.Option.map (fun x -> Pcre.regexp (preprocess x)) regexp)
Expand All @@ -296,3 +296,7 @@ let run ?dir ?regexp ?dest ?code ?cache ?root () =
root)
in
fun _ _ _ -> gen ~usermode:None ?cache kind

let run ?site ?dir ?regexp ?dest ?code ?cache ?root () =
Ocsigen_server.Site.register ?site
(instruction ?dir ?regexp ?dest ?code ?cache ?root ())
10 changes: 5 additions & 5 deletions src/extensions/staticmod.mli
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
val section : Lwt_log_core.section
(** use Lwt_log.Section.set_level in order to debug *)

(** Use the following config key to set options
for a statically linked executable without configuration file: *)

val run :
?dir:string
?site:Ocsigen_server.Site.t
-> ?dir:string
-> ?regexp:string
-> ?dest:string
-> ?code:string
-> ?cache:int
-> ?root:string
-> unit
-> Ocsigen_server.Site.instruction
-> unit
(** Run static mod on a specific directory.
Call this if you want to run Ocsigen Server without configuration file. *)
20 changes: 10 additions & 10 deletions src/server/ocsigen_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,8 @@ module Site = struct
let path, hosts = path_and_hosts s in
path @ path', hosts

let register ({s_config_info; s_children_l; _} as s) f =
let path, hosts = path_and_hosts s in
s.s_children_l <- `Instruction (f hosts s_config_info path) :: s_children_l

let create ?(config_info = Ocsigen_extensions.default_config_info ())
?(id = `Host (default_re_string, None)) ?charset
()
?(id = `Host (default_re_string, None)) ?charset ()
=
let s_id =
match id with
Expand All @@ -170,17 +165,22 @@ module Site = struct
`Attach (parent, Ocsigen_extensions.preprocess_site_path path)
in
let s =
{ s_id
; s_charset = charset
; s_config_info = config_info
; s_children_l = [] }
{s_id; s_charset = charset; s_config_info = config_info; s_children_l = []}
in
(match s_id with
| `Host _ -> l := s :: !l
| `Attach (parent, _) ->
parent.s_children_l <- `Child s :: parent.s_children_l);
s

let default_host = create ()

let register ?(site = default_host) f =
let {s_config_info; s_children_l; _} = site in
let path, hosts = path_and_hosts site in
site.s_children_l <-
`Instruction (f hosts s_config_info path) :: s_children_l

let rec dump_host path {s_children_l; _} =
let f = function
| `Instruction f -> f
Expand Down
9 changes: 7 additions & 2 deletions src/server/ocsigen_server.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ module Site : sig
This is equivalent to the [<host>] or [<site>] config file options.
*)

val default_host : t
(** Defaut host. Any hostname, any port.
Will be used if you don not specify [?site]. *)

type instruction =
Ocsigen_extensions.virtual_hosts
-> Ocsigen_extensions.config_info
Expand All @@ -53,6 +57,7 @@ module Site : sig
(** Instructions are defined by extensions, and correspond to the
configuration file options defined by extensions (<staticmod/> ...)*)

val register : t -> instruction -> unit
(** [register t s e] registers instruction [e] to be run inside site [s] *)
val register : ?site:t -> instruction -> unit
(** [register t s e] registers instruction [e] to be run inside site [s].
Use this if you want to create an extension yourself. *)
end

0 comments on commit 78d1825

Please sign in to comment.