Browse Source

restructure for easier reuse (#6085)

mudrz 4 years ago
parent
commit
0f77952d53

+ 1 - 0
frameworks/OCaml/.gitignore

@@ -1,5 +1,6 @@
 **/_opam
 **/_opam
 **/_build
 **/_build
+!**/bin
 .merlin
 .merlin
 .ocamlformat
 .ocamlformat
 _esy
 _esy

+ 9 - 2
frameworks/OCaml/opium/README.md

@@ -10,12 +10,19 @@
 * [FORTUNES](src/lib/routes.ml#L58-L67)
 * [FORTUNES](src/lib/routes.ml#L58-L67)
 
 
 ## Important Libraries
 ## Important Libraries
+* [Lwt](https://github.com/ocsigen/lwt)
 * [Opium](https://github.com/rgrinberg/opium)
 * [Opium](https://github.com/rgrinberg/opium)
 * [Httpaf](https://github.com/inhabitedtype/httpaf)
 * [Httpaf](https://github.com/inhabitedtype/httpaf)
 * [Caqti](https://github.com/paurkedal/ocaml-caqti)
 * [Caqti](https://github.com/paurkedal/ocaml-caqti)
-* [Lwt](https://github.com/ocsigen/lwt)
-* [Yojson](https://github.com/ocaml-community/yojson)
 * [Ppx_rapper](https://github.com/roddyyaga/ppx_rapper)
 * [Ppx_rapper](https://github.com/roddyyaga/ppx_rapper)
+* [Yojson](https://github.com/ocaml-community/yojson)
+* [Ppx_deriving_yojson](https://github.com/ocaml-ppx/ppx_deriving_yojson)
+* [Tyxml](https://github.com/ocsigen/tyxml)
+
+## Structure
+`lib/` contains most of the logc.
+It doesn't have a dependency on any web server to make it more portable and allow easier addition to new webservers.
+Feel free to copy paste when adding additional servers.
 
 
 ## Test URLs
 ## Test URLs
 
 

+ 1 - 1
frameworks/OCaml/opium/opium.dockerfile

@@ -18,4 +18,4 @@ COPY ./src ./
 
 
 RUN sudo chown -R opam: . && make build
 RUN sudo chown -R opam: . && make build
 
 
-CMD _build/default/exe/main.exe
+CMD _build/default/bin/main.exe

+ 2 - 2
frameworks/OCaml/opium/src/Makefile

@@ -19,10 +19,10 @@ gen-opam:
 	dune build @install
 	dune build @install
 
 
 build:
 build:
-	opam exec -- dune build --profile release exe/$(BINARY).exe
+	opam exec -- dune build --profile release bin/$(BINARY).exe
 
 
 run:
 run:
-	./_build/default/exe/$(BINARY).exe
+	./_build/default/bin/$(BINARY).exe
 
 
 run-debug:
 run-debug:
 	dune exec $(project_name) -- --debug
 	dune exec $(project_name) -- --debug

+ 0 - 0
frameworks/OCaml/opium/src/exe/dune → frameworks/OCaml/opium/src/bin/dune


+ 19 - 12
frameworks/OCaml/opium/src/exe/main.ml → frameworks/OCaml/opium/src/bin/main.ml

@@ -1,8 +1,15 @@
 open Opium.Std
 open Opium.Std
 
 
-let get_count_param req = match (int_of_string (Router.param req "count")) with
-  | x -> x
-  | exception _e -> 1
+module Tfb_headers = struct
+  let middleware =
+    let open Lwt.Syntax in
+    let filter handler req =
+      let+ res = handler req in
+      let headers = ["Server", "opium"; "Date", Opi.Time.now ()] in
+      Response.add_headers_or_replace headers res
+    in
+    Rock.Middleware.create ~name:"TFB Headers" ~filter
+end
 
 
 let main () =
 let main () =
   let port =
   let port =
@@ -12,14 +19,14 @@ let main () =
   in
   in
   let routes =
   let routes =
     [
     [
-      "/plaintext", (fun _req -> Opi.Routes.plaintext ());
-      "/json", (fun _req -> Opi.Routes.json ());
-      "/db", (fun _req -> Opi.Routes.db ());
-      "/fortunes", (fun _req -> Opi.Routes.fortunes ());
-      "/queries/", (fun _req -> Opi.Routes.queries 1);
-      "/queries/:count", (fun req -> Opi.Routes.queries (get_count_param req));
-      "/updates/", (fun req -> Opi.Routes.updates 1);
-      "/updates/:count", (fun req -> Opi.Routes.updates (get_count_param req))
+      "/plaintext", Routes.plaintext;
+      "/json", Routes.json;
+      "/db", Routes.single_query;
+      "/fortunes", Routes.fortunes;
+      "/queries/", Routes.multiple_queries;
+      "/queries/:count", Routes.multiple_queries;
+      "/updates/", Routes.updates;
+      "/updates/:count", Routes.updates
     ]
     ]
   in
   in
   let add_routes app = List.fold_left (fun app (route,handler) -> (get route handler) app) app routes in
   let add_routes app = List.fold_left (fun app (route,handler) -> (get route handler) app) app routes in
@@ -28,7 +35,7 @@ let main () =
     |> App.cmd_name "Opium"
     |> App.cmd_name "Opium"
     |> App.port port
     |> App.port port
     |> middleware Middleware.content_length
     |> middleware Middleware.content_length
-    |> middleware Tfb_headers.Middleware.m
+    |> middleware Tfb_headers.middleware
     |> add_routes in
     |> add_routes in
 
 
   match App.run_command' app with
   match App.run_command' app with

+ 41 - 0
frameworks/OCaml/opium/src/bin/routes.ml

@@ -0,0 +1,41 @@
+open Opium.Std
+open Lwt.Syntax
+
+(*
+ * bin/routes contains a mapping between
+ * Opium request -> response
+ *
+ * lib/routes contains the actual logic for
+ * db calls and etc.
+ *)
+
+let plaintext _req =
+  let text = Opi.Routes.plaintext () in
+  Lwt.return @@ Response.of_plain_text text
+
+let json _req =
+  let json = Opi.Routes.json () in
+  Lwt.return @@ Response.of_json json
+
+let single_query _req =
+  let+ json = Opi.Routes.single_query () in
+  Response.of_json json
+
+let get_count_param req = match (int_of_string (Router.param req "count")) with
+  | x -> x
+  | exception _e -> 1
+
+let multiple_queries req =
+  let count = get_count_param req in
+  let+ json = Opi.Routes.multiple_queries count in
+  Response.of_json json
+
+let updates req =
+  let count = get_count_param req in
+  let+ json = Opi.Routes.updates count in
+  Response.of_json json
+
+let fortunes _req =
+  let+ html = Opi.Routes.fortunes () in
+  Response.of_html ~indent:false html
+

+ 0 - 45
frameworks/OCaml/opium/src/exe/tfb_headers.ml

@@ -1,45 +0,0 @@
-module Time = struct
-  let weekday Unix.{tm_wday;_} = match tm_wday with
-    | 0 -> "Sun"
-    | 1 -> "Mon"
-    | 2 -> "Tue"
-    | 3 -> "Wed"
-    | 4 -> "Thu"
-    | 5 -> "Fri"
-    | 6 -> "Sat"
-    | _ -> failwith "weekday"
-
-  let month Unix.{tm_mon;_} = match tm_mon with
-    | 0 -> "Jan"
-    | 1 -> "Feb"
-    | 2 -> "Mar"
-    | 3 -> "Apr"
-    | 4 -> "May"
-    | 5 -> "Jun"
-    | 6 -> "Jul"
-    | 7 -> "Aug"
-    | 8 -> "Sep"
-    | 9 -> "Oct"
-    | 10 -> "Nov"
-    | 11 -> "Dec"
-    | _ -> failwith "month"
-
-  let gmt tm =
-    Printf.sprintf "%s, %02u %s %04u %02u:%02u:%02u GMT" (weekday tm) tm.tm_mday (month tm) (tm.tm_year + 1900)  tm.tm_hour tm.tm_min tm.tm_sec
-
-  let now () = (gmt (Unix.gmtime (Unix.gettimeofday ())))
-end
-
-module Middleware = struct
-  open Opium.Std
-
-  let m =
-    let open Lwt.Syntax in
-    let filter handler req =
-      let+ res = handler req in
-      let headers = ["Server", "opium"; "Date", Time.now ()] in
-      Response.add_headers_or_replace headers res
-    in
-    Rock.Middleware.create ~name:"Content length" ~filter
-end
-

+ 1 - 1
frameworks/OCaml/opium/src/lib/dune

@@ -1,6 +1,6 @@
 (library
 (library
  (name opi)
  (name opi)
- (libraries opium caqti caqti-driver-postgresql caqti-lwt tyxml lwt.unix
+ (libraries caqti caqti-driver-postgresql caqti-lwt tyxml lwt.unix
    ppx_rapper.runtime)
    ppx_rapper.runtime)
  (preprocess
  (preprocess
   (pps ppx_rapper ppx_deriving_yojson)))
   (pps ppx_rapper ppx_deriving_yojson)))

+ 10 - 19
frameworks/OCaml/opium/src/lib/routes.ml

@@ -1,23 +1,16 @@
 let random_int () = Random.int 10000 + 1
 let random_int () = Random.int 10000 + 1
 
 
-let plaintext () =
-  let response = Opium.Std.Response.of_plain_text "Hello, World!" in
-  Lwt.return response
+let plaintext () = "Hello, World!" 
 
 
-let json () =
-  let json = Models.World.message_response_to_yojson { message="Hello, World!" } in
-  let response = Opium.Std.Response.of_json json in
-  Lwt.return response
+let json () = Models.World.message_response_to_yojson { message="Hello, World!" }
 
 
-let db () =
+let single_query () =
   let open Lwt.Syntax in
   let open Lwt.Syntax in
   let id = random_int () in
   let id = random_int () in
   let+ result = Db.Get.get_world id in
   let+ result = Db.Get.get_world id in
   match result with
   match result with
   | Error _e -> failwith "failed db"
   | Error _e -> failwith "failed db"
-  | Ok world -> 
-    let json = Models.World.to_yojson world in
-    Opium.Std.Response.of_json json
+  | Ok world -> Models.World.to_yojson world
 
 
 let get_worlds count =
 let get_worlds count =
   let count = match count with
   let count = match count with
@@ -27,18 +20,17 @@ let get_worlds count =
   in
   in
   let query_ids = List.init count (fun _ -> random_int ()) in
   let query_ids = List.init count (fun _ -> random_int ()) in
   let open Lwt.Syntax in
   let open Lwt.Syntax in
-  List.map (fun id ->
+  query_ids |> List.map (fun id ->
       let+ result = Db.Get.get_world id in
       let+ result = Db.Get.get_world id in
       match result with
       match result with
       | Error _ -> failwith "failed queries"
       | Error _ -> failwith "failed queries"
       | Ok w -> w
       | Ok w -> w
-    ) query_ids
+    )
 
 
-let queries count =
+let multiple_queries count =
   let open Lwt.Syntax in
   let open Lwt.Syntax in
   let+ worlds = Lwt.all (get_worlds count) in
   let+ worlds = Lwt.all (get_worlds count) in
-  let json = Models.World.list_response_to_yojson worlds in
-  Opium.Std.Response.of_json json
+  Models.World.list_response_to_yojson worlds
 
 
 let updates count =
 let updates count =
   let open Lwt.Syntax in
   let open Lwt.Syntax in
@@ -52,8 +44,7 @@ let updates count =
       | Ok w -> {world with randomNumber=updated_random_number}
       | Ok w -> {world with randomNumber=updated_random_number}
     ) in
     ) in
   let+ updated_worlds = Lwt.all results in
   let+ updated_worlds = Lwt.all results in
-  let json = Models.World.list_response_to_yojson updated_worlds in
-  Opium.Std.Response.of_json json
+  Models.World.list_response_to_yojson updated_worlds
 
 
 let fortunes () =
 let fortunes () =
   let open Lwt.Syntax in
   let open Lwt.Syntax in
@@ -64,5 +55,5 @@ let fortunes () =
       let x = Models.Fortune.{id=0;message= "Additional fortune added at request time."} in
       let x = Models.Fortune.{id=0;message= "Additional fortune added at request time."} in
       x::xs |> List.sort Models.Fortune.compare
       x::xs |> List.sort Models.Fortune.compare
   in
   in
-  Opium.Std.Response.of_html ~indent:false (Views.fortunes_page fortunes)
+  Views.fortunes_page fortunes
 
 

+ 11 - 0
frameworks/OCaml/opium/src/lib/routes.mli

@@ -0,0 +1,11 @@
+val plaintext: unit -> string
+
+val json: unit -> Yojson.Safe.t
+
+val single_query: unit -> Yojson.Safe.t Lwt.t
+
+val multiple_queries: int -> Yojson.Safe.t Lwt.t
+
+val updates: int -> Yojson.Safe.t Lwt.t
+
+val fortunes: unit -> [> Html_types.html ] Tyxml_html.elt Lwt.t

+ 29 - 0
frameworks/OCaml/opium/src/lib/time.ml

@@ -0,0 +1,29 @@
+let weekday Unix.{tm_wday;_} = match tm_wday with
+  | 0 -> "Sun"
+  | 1 -> "Mon"
+  | 2 -> "Tue"
+  | 3 -> "Wed"
+  | 4 -> "Thu"
+  | 5 -> "Fri"
+  | 6 -> "Sat"
+  | _ -> failwith "weekday"
+
+let month Unix.{tm_mon;_} = match tm_mon with
+  | 0 -> "Jan"
+  | 1 -> "Feb"
+  | 2 -> "Mar"
+  | 3 -> "Apr"
+  | 4 -> "May"
+  | 5 -> "Jun"
+  | 6 -> "Jul"
+  | 7 -> "Aug"
+  | 8 -> "Sep"
+  | 9 -> "Oct"
+  | 10 -> "Nov"
+  | 11 -> "Dec"
+  | _ -> failwith "month"
+
+let gmt tm =
+  Printf.sprintf "%s, %02u %s %04u %02u:%02u:%02u GMT" (weekday tm) tm.tm_mday (month tm) (tm.tm_year + 1900)  tm.tm_hour tm.tm_min tm.tm_sec
+
+let now () = (gmt (Unix.gmtime (Unix.gettimeofday ())))

+ 1 - 1
frameworks/OCaml/opium/start-servers.sh

@@ -5,6 +5,6 @@ P=9000
 END=$(($P+$CPU_COUNT))
 END=$(($P+$CPU_COUNT))
 
 
 while [ $P -lt $END ]; do
 while [ $P -lt $END ]; do
-  PORT=$P /web/_build/default/exe/main.exe &
+  PORT=$P /web/_build/default/bin/main.exe &
   let P=P+1
   let P=P+1
 done
 done