From 2d64b3a0b4a83f140fe5a43218cc83a3ed13fdd5 Mon Sep 17 00:00:00 2001 From: Patrick Ferris Date: Tue, 9 Apr 2024 08:37:51 +0100 Subject: [PATCH] Split shark server code into sublibrary --- src/bin/dune | 1 + src/bin/main.ml | 6 +++--- src/lib/dune | 12 +----------- src/lib/server/dune | 17 +++++++++++++++++ src/lib/{ => server}/editor.ml | 0 src/lib/{ => server}/html.ml | 0 src/lib/{ => server}/js/dune | 0 src/lib/{ => server}/js/edit.ml | 0 src/lib/{ => server}/js/main.ml | 0 src/lib/{serve.ml => server/shark_server.ml} | 6 +++--- 10 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 src/lib/server/dune rename src/lib/{ => server}/editor.ml (100%) rename src/lib/{ => server}/html.ml (100%) rename src/lib/{ => server}/js/dune (100%) rename src/lib/{ => server}/js/edit.ml (100%) rename src/lib/{ => server}/js/main.ml (100%) rename src/lib/{serve.ml => server/shark_server.ml} (98%) diff --git a/src/bin/dune b/src/bin/dune index 2090e63ac..a6007fc20 100644 --- a/src/bin/dune +++ b/src/bin/dune @@ -4,6 +4,7 @@ (libraries eio_main shark + shark.server lwt.unix obuilder logs.cli diff --git a/src/bin/main.ml b/src/bin/main.ml index f6f9f9591..9781c9c94 100644 --- a/src/bin/main.ml +++ b/src/bin/main.ml @@ -103,13 +103,13 @@ let edit ~proc ~net ~fs () file port = let port = match port with None -> 8080 | Some port -> port in let handler conn request body = let routes = - Shark.Serve.edit_routes ~proc Eio.Path.(fs / file) conn request body + Shark_server.edit_routes ~proc Eio.Path.(fs / file) conn request body in let router = Routes.one_of routes in match Routes.match' router ~target:(Http.Request.resource request) with | FullMatch a -> a | MatchWithTrailingSlash a -> a - | NoMatch -> Shark.Serve.respond_not_found + | NoMatch -> Shark_server.respond_not_found in let addr = `Tcp (Eio.Net.Ipaddr.V4.any, port) in Eio.traceln "Running edit server at http://%a:%i/editor" Eio.Net.Ipaddr.pp @@ -152,7 +152,7 @@ let md ~fs ~net ~domain_mgr ~proc () no_run store conf file port fetcher = let output_path = Eio.Path.(fs / Filename.temp_file "shark-md" "run") in Eio.Path.save ~create:(`If_missing 0o644) output_path (Cmarkit_commonmark.of_doc document); - let handler = Shark.Serve.router ~proc ~fs ~store output_path in + let handler = Shark_server.router ~proc ~fs ~store output_path in let addr = `Tcp (Eio.Net.Ipaddr.V4.any, port) in Eio.traceln "Running server on %a" Eio.Net.Sockaddr.pp addr; let socket = Eio.Net.listen net ~sw ~backlog:128 ~reuse_addr:true addr diff --git a/src/lib/dune b/src/lib/dune index ee7e7fd3a..a90808fb1 100644 --- a/src/lib/dune +++ b/src/lib/dune @@ -2,6 +2,7 @@ (library (name shark) + (public_name shark) (libraries eio cohttp-eio @@ -15,14 +16,3 @@ fpath) (preprocess (pps ppx_sexp_conv))) - -(rule - (deps js/main.bc.js) - (targets js.ml) - (action - (with-stdout-to - js.ml - (progn - (echo "let editor_js = {js|") - (run cat js/main.bc.js) - (echo "|js}"))))) diff --git a/src/lib/server/dune b/src/lib/server/dune new file mode 100644 index 000000000..027036940 --- /dev/null +++ b/src/lib/server/dune @@ -0,0 +1,17 @@ +(include_subdirs no) + +(rule + (deps js/main.bc.js) + (targets js.ml) + (action + (with-stdout-to + js.ml + (progn + (echo "let editor_js = {js|") + (run cat js/main.bc.js) + (echo "|js}"))))) + +(library + (name shark_server) + (public_name shark.server) + (libraries shark cohttp-eio)) diff --git a/src/lib/editor.ml b/src/lib/server/editor.ml similarity index 100% rename from src/lib/editor.ml rename to src/lib/server/editor.ml diff --git a/src/lib/html.ml b/src/lib/server/html.ml similarity index 100% rename from src/lib/html.ml rename to src/lib/server/html.ml diff --git a/src/lib/js/dune b/src/lib/server/js/dune similarity index 100% rename from src/lib/js/dune rename to src/lib/server/js/dune diff --git a/src/lib/js/edit.ml b/src/lib/server/js/edit.ml similarity index 100% rename from src/lib/js/edit.ml rename to src/lib/server/js/edit.ml diff --git a/src/lib/js/main.ml b/src/lib/server/js/main.ml similarity index 100% rename from src/lib/js/main.ml rename to src/lib/server/js/main.ml diff --git a/src/lib/serve.ml b/src/lib/server/shark_server.ml similarity index 98% rename from src/lib/serve.ml rename to src/lib/server/shark_server.ml index b77d8ac2f..0e218db2e 100644 --- a/src/lib/serve.ml +++ b/src/lib/server/shark_server.ml @@ -221,10 +221,10 @@ let custom_document_renderer _ = function (* We're only interested in getting the hash here. *) let info = Cmarkit.Block.Code_block.info_string node |> fun v -> - Option.bind v (fun (v, _) -> Block.of_info_string ~body:"" v) + Option.bind v (fun (v, _) -> Shark.Block.of_info_string ~body:"" v) in let info_block = - match Option.bind info (fun v -> Block.hash v) with + match Option.bind info (fun v -> Shark.Block.hash v) with | None -> Cmarkit.Block.Thematic_break (Cmarkit.Block.Thematic_break.make (), Cmarkit.Meta.none) @@ -442,7 +442,7 @@ let run_dot proc dot = let serve_dot proc _req body = let template_markdown = Eio.Flow.read_all body in - let txt = Dotrenderer.render ~template_markdown in + let txt = Shark.Dotrenderer.render ~template_markdown in let png = run_dot proc txt |> Base64.encode_string in respond_txt png