Skip to content

Commit 6ffd13a

Browse files
authored
Merge pull request #120 from maxtori/fix-register-bug
Revamp register of services
2 parents fed22b0 + 65f7c5b commit 6ffd13a

File tree

6 files changed

+62
-54
lines changed

6 files changed

+62
-54
lines changed

src/common/doc.ml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
(**************************************************************************)
1010

1111
type t = {
12-
doc_id : int; (* uniq service identifier *)
12+
mutable doc_id : int; (* uniq service identifier *)
1313
doc_name : string option;
1414
doc_descr : string option;
1515
doc_path : string;
1616
doc_args : Arg.descr list;
1717
doc_params : Param.t list;
18-
mutable doc_registered : bool;
1918
doc_section : section;
2019
doc_input : Json_schema.schema Lazy.t option;
2120
doc_output : Json_schema.schema Lazy.t option;
@@ -25,29 +24,24 @@ type t = {
2524
doc_security : Security.scheme list;
2625
doc_input_example : Json_repr.any option;
2726
doc_output_example : Json_repr.any option;
27+
doc_hide : bool;
28+
doc_register : bool;
2829
}
2930

3031
and section = {
3132
section_name : string;
3233
mutable section_docs : t list;
3334
}
3435

35-
let services = ref []
36-
let nservices = ref 0
3736
let default_section = { section_name = "Misc"; section_docs = [] }
3837
let sections = ref [ default_section ]
3938

4039
let definitions_path = "/components/schemas/"
4140

42-
let rec update_service_list services doc = match services with
43-
| [] -> [ doc ]
44-
| h :: t when h.doc_path = doc.doc_path -> doc :: t
45-
| h :: t -> h :: (update_service_list t doc)
46-
4741
let make :
48-
type i. ?name:string -> ?descr:string -> ?register:bool -> ?section:section ->
42+
type i. ?name:string -> ?descr:string -> ?register:bool -> ?hide:bool -> ?section:section ->
4943
?input_example:i -> ?output_example:'o -> (_, i, 'o, _, _) Service.t -> t =
50-
fun ?name ?descr ?(register=true) ?(section=default_section) ?input_example ?output_example s ->
44+
fun ?name ?descr ?(register=true) ?(hide=false) ?(section=default_section) ?input_example ?output_example s ->
5145
let path = Service.path s in
5246
let input = Service.input s in
5347
let output = Service.output s in
@@ -67,29 +61,35 @@ let make :
6761
let doc_output_example = match output_example, output with
6862
| Some ex, Service.IO.Json enc -> Some (Json_repr.to_any @@ Json_encoding.construct enc ex)
6963
| _ -> None in
70-
let doc_id = if register then (
71-
let id = !nservices in
72-
incr nservices;
73-
id)
74-
else -1 in
75-
let doc = {
64+
{
7665
doc_path = Path.to_string path;
7766
doc_args = Path.args path;
7867
doc_params = Service.params s;
79-
doc_registered = false;
80-
doc_name = name; doc_descr = descr; doc_id;
68+
doc_name = name; doc_descr = descr; doc_id = -1;
8169
doc_section = section;
8270
doc_input; doc_mime;
8371
doc_output;
8472
doc_errors = Err.merge_errs_same_code ~definitions_path (Service.errors s);
8573
doc_meth = Service.meth s;
8674
doc_security = (Service.security s :> Security.scheme list);
87-
doc_input_example; doc_output_example
88-
} in
89-
if register then (
90-
section.section_docs <- update_service_list section.section_docs doc;
91-
services := update_service_list !services doc);
92-
doc
75+
doc_input_example; doc_output_example;
76+
doc_hide = hide;
77+
doc_register = register
78+
}
79+
80+
let rec update_service_list services doc = match services with
81+
| [] -> [ doc ]
82+
| h :: t when h.doc_path = doc.doc_path -> doc :: t
83+
| h :: t -> h :: (update_service_list t doc)
84+
85+
let services = ref []
86+
let nservices = ref 0
87+
88+
let register doc =
89+
doc.doc_id <- !nservices;
90+
incr nservices;
91+
doc.doc_section.section_docs <- update_service_list doc.doc_section.section_docs doc;
92+
services := update_service_list !services doc
9393

9494
let section section_name =
9595
let s = { section_name; section_docs = [] } in
@@ -98,7 +98,7 @@ let section section_name =
9898

9999
let all_services_registered () =
100100
let s = List.fold_left (fun acc doc ->
101-
if not doc.doc_registered then
101+
if doc.doc_id = -1 && doc.doc_register then
102102
Printf.sprintf "%s%s is not registered\n" acc doc.doc_path
103103
else acc
104104
) "" !services in

src/common/ezAPI.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ let forge2 url s arg1 arg2 params = forge url s ((Req.dummy, arg1), arg2) param
108108
let raw_service :
109109
type i. ?section:Doc.section -> ?name:string -> ?descr:string -> ?meth:Meth.t ->
110110
input:i io -> output:'o io -> ?errors:'e Err.case list -> ?params:Param.t list ->
111-
?security:'s list -> ?access_control:(string * string) list -> ?register:bool ->
112-
?input_example:i -> ?output_example:'o -> (Req.t, 'a) Path.t ->
111+
?security:'s list -> ?access_control:(string * string) list -> ?register:bool -> ?hide:bool ->
112+
?input_example:i -> ?output_example:'o -> (Req.t, 'a) Path.t ->
113113
('a, i, 'o, 'e, 's) service =
114114
fun ?section ?name ?descr ?meth ~input ~output ?(errors=[]) ?(params=[])
115-
?(security=[]) ?access_control ?register ?input_example ?output_example path ->
115+
?(security=[]) ?access_control ?register ?hide ?input_example ?output_example path ->
116116
let meth = match meth, input with
117117
| None, Empty -> `GET
118118
| None, _ -> `POST
119119
| Some m, _ -> m in
120120
let s = Service.make ~meth ~input ~output
121121
~errors ~params ~security ?access_control path in
122-
let doc = Doc.make ?name ?descr ?register ?section ?input_example ?output_example s in
122+
let doc = Doc.make ?name ?descr ?section ?input_example ?output_example ?hide ?register s in
123123
{ s; doc }
124124

125125
let post_service ?section ?name ?descr ?(meth=`POST)
@@ -140,7 +140,7 @@ let ws_service ?section ?name ?descr ~input ~output ?errors ?params
140140
?errors ~meth:`GET ?params ?security ?access_control ?register ?output_example path
141141

142142
let register service =
143-
service.doc.Doc.doc_registered <- true;
143+
Doc.register service.doc;
144144
service.s
145145

146146
let id s = s.doc.Doc.doc_id

src/ppx/ppx_common.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type options = {
3737
descr : expression;
3838
security : expression;
3939
register : expression;
40+
hide : expression;
4041
input_example : expression;
4142
output_example : expression;
4243
error_type : core_type;
@@ -63,7 +64,7 @@ let options ?register loc =
6364
path = pexp_ident ~loc (llid ~loc "EzAPI.Path.root");
6465
input = empty ~loc; output = empty ~loc; errors = enone ~loc; params = enone ~loc;
6566
section = enone ~loc; name = enone ~loc; descr = enone ~loc;
66-
security = enone ~loc; register; input_example = enone ~loc;
67+
security = enone ~loc; register; input_example = enone ~loc; hide = enone ~loc;
6768
output_example = enone ~loc; error_type = ptyp_constr ~loc (llid ~loc "exn") [];
6869
security_type = ptyp_constr ~loc (llid ~loc "EzAPI.no_security") [];
6970
debug = false; directory = None; service = None
@@ -134,6 +135,7 @@ let get_options ~loc ?name ?(client=false) a =
134135
| "descr" -> name, { acc with descr = esome e }
135136
| "security" -> name, { acc with security = esome e; security_type = ptyp_any ~loc }
136137
| "register" -> name, { acc with register = e }
138+
| "hide" -> name, { acc with hide = e }
137139
| "input_example" -> name, { acc with input_example = esome e }
138140
| "output_example" -> name, { acc with output_example = esome e }
139141
| "debug" -> name, { acc with debug = true }

src/server/ezAPIServerUtils.ml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ let register_res service handler dir =
6363
let security = Service.security service.s in
6464
let path = Service.path service.s in
6565
let handler args input =
66-
let t0 = (Path.get_root path args).Req.req_time in
67-
let add_timing_wrap b =
68-
let t1 = GMTime.time () in
69-
Timings.add_timing (EzAPI.id service) b t0 (t1-.t0) in
70-
Lwt.catch
71-
(function () ->
72-
handler args security input >>= fun res ->
73-
add_timing_wrap true;
74-
Lwt.return res)
75-
(fun exn -> add_timing_wrap true; Lwt.fail exn) in
66+
if !Timings.enabled then
67+
let t0 = (Path.get_root path args).Req.req_time in
68+
let add_timing_wrap b =
69+
let t1 = GMTime.time () in
70+
Timings.add_timing (EzAPI.id service) b t0 (t1-.t0) in
71+
Lwt.catch
72+
(function () ->
73+
handler args security input >>= fun res ->
74+
add_timing_wrap true;
75+
Lwt.return res)
76+
(fun exn -> add_timing_wrap true; Lwt.fail exn)
77+
else handler args security input in
7678
let service = register service in
7779
Directory.register_http dir service handler
7880

@@ -152,7 +154,7 @@ let handle ?meth ?content_type ?ws s r path body =
152154
(* Default access control headers *)
153155
let default_access_control_headers = [
154156
"access-control-allow-origin", "*";
155-
"access-control-allow-headers", "accept, content-type"
157+
"access-control-allow-headers", "accept, content-type"
156158
]
157159

158160
(* merge headers correctly with default one *)
@@ -165,7 +167,7 @@ let merge_headers_with_default headers : (string * string) list =
165167
| Some _ when hn = "access-control-allow-origin" ->
166168
h::acc
167169
| Some v when hn = "access-control-allow-headers" ->
168-
(hn, hv ^ "," ^ v)::acc
170+
(hn, hv ^ "," ^ v)::acc
169171
| _ -> acc)
170172
[]
171173
headers
@@ -174,5 +176,5 @@ let merge_headers_with_default headers : (string * string) list =
174176
List.fold_left (fun acc ((hn,_) as h) ->
175177
match List.assoc_opt hn l with
176178
| None -> h::acc
177-
| _ -> acc
178-
) l default_access_control_headers
179+
| _ -> acc
180+
) l default_access_control_headers

src/server/ezOpenAPI.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,10 @@ let make ?descr ?terms ?contact ?license ?(version="0.1") ?servers ?(docs=[])
672672
List.fold_left (fun acc s -> if List.mem s acc then acc else s :: acc) acc sd.Doc.doc_security)
673673
[] sds in
674674
let paths, definitions = List.fold_left (fun (paths, definitions) sd ->
675-
let path, definitions = make_path ~definitions ~docs sd in
676-
path :: paths, definitions) ([], Json_schema.any) sds in
675+
if sd.Doc.doc_hide then (paths, definitions)
676+
else
677+
let path, definitions = make_path ~definitions ~docs sd in
678+
path :: paths, definitions) ([], Json_schema.any) sds in
677679
let schemas = definitions_schemas definitions in
678680
let oa = Makers.mk_openapi ?servers ~info
679681
~components:(Makers.mk_components ~security ?schemas ())

src/server/timings.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
(* *)
99
(**************************************************************************)
1010

11+
let enabled = ref false
12+
let enable () = enabled := true
13+
let disable () = enabled := false
14+
1115
type period = {
1216
name : string;
1317
mutable prev : int;
@@ -125,11 +129,9 @@ let timings = {
125129
}
126130

127131
let add_timing n ok t dt =
128-
if ok then add timings.timings_ok.(n) t dt
129-
else add timings.timings_fail.(n) t dt
132+
if ok then try add timings.timings_ok.(n) t dt with _ -> ()
133+
else try add timings.timings_fail.(n) t dt with _ -> ()
130134

131135
let init t0 nservices =
132-
timings.timings_ok <-Array.init nservices
133-
(fun _ -> create t0);
134-
timings.timings_fail <- Array.init nservices
135-
(fun _ -> create t0)
136+
timings.timings_ok <- Array.init nservices (fun _ -> create t0);
137+
timings.timings_fail <- Array.init nservices (fun _ -> create t0)

0 commit comments

Comments
 (0)