Skip to content

Commit

Permalink
Split shark server code into sublibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
patricoferris committed Apr 9, 2024
1 parent c922fca commit 2d64b3a
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(libraries
eio_main
shark
shark.server
lwt.unix
obuilder
logs.cli
Expand Down
6 changes: 3 additions & 3 deletions src/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions src/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(library
(name shark)
(public_name shark)
(libraries
eio
cohttp-eio
Expand All @@ -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}")))))
17 changes: 17 additions & 0 deletions src/lib/server/dune
Original file line number Diff line number Diff line change
@@ -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))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/lib/serve.ml → src/lib/server/shark_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2d64b3a

Please sign in to comment.