Skip to content

Commit

Permalink
Higher level handler incl file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
actionshrimp committed Apr 16, 2018
1 parent a25faea commit f0dd8bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Livereload server in ocaml that can live in your webserver process.

- [ ] Work out the best way to hook this into e.g. an [opium](https://github.com/rgrinberg/opium) middleware or to live alongside opium in the http server.
- [ ] Improve file watching API to handle single files and make sure path mapping works correctly
- [ ] Wire together file watcher and reload handler into a single entrypoint
- [x] Wire together file watcher and reload handler into a single entrypoint

See `test/test_files_server.ml` for an example of wiring with file watcher.
31 changes: 23 additions & 8 deletions src/livereload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ module ByConn = CCMap.Make(struct
end)


type handler = Conduit_lwt_unix.flow * Cohttp.Connection.t -> Cohttp_lwt_unix.Request.t -> Cohttp_lwt.Body.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t

let make_handler
let make_raw_handler
?(debug=false)
(next : Conduit_lwt_unix.flow * Cohttp.Connection.t -> Cohttp_lwt_unix.Request.t -> Cohttp_lwt.Body.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t)
: ((string -> unit Lwt.t) *
(Conduit_lwt_unix.flow * Cohttp.Connection.t -> Cohttp_lwt_unix.Request.t -> Cohttp_lwt.Body.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t))
(next : handler)
: ((string -> unit Lwt.t) * handler)
=
let debug_log (msg : string) = if debug then print_endline msg else () in
let debug_log_lwt (msg : string) = if debug then Lwt_io.eprint msg else Lwt.return () in
Expand Down Expand Up @@ -96,17 +96,21 @@ let make_handler
Lwt.return (resp, (body :> Cohttp_lwt.Body.t))
| _ -> next conn req body)

type path_config =
{ fs_dir : string
; server_base : string
}

let make_watcher
?(debug=false)
(path_configs : (string * string) list)
(path_configs : path_config list)
(change_cb : string -> unit Lwt.t)
: unit Lwt.t =
let debug_log_lwt (msg : string) = if debug then Lwt_io.eprint msg else Lwt.return () in
Lwt_inotify.create () >>= fun inotify ->
path_configs
|> CCList.map (fun (fs_path, server_base) ->
Lwt_inotify.add_watch inotify fs_path [Inotify.S_Attrib; Inotify.S_Modify]
|> CCList.map (fun {fs_dir; server_base} ->
Lwt_inotify.add_watch inotify fs_dir [Inotify.S_Attrib; Inotify.S_Modify]
>>= fun _ -> Lwt.return ()
)
|> Lwt.join >>= fun () ->
Expand All @@ -117,11 +121,22 @@ let make_watcher
let i = (Inotify.int_of_watch watch) - 1 in
begin
match (CCList.get_at_idx i path_configs, fname_opt) with
| (Some (_, server_base), Some fname) ->
| (Some {server_base}, Some fname) ->
let server_path = server_base ^ "/" ^ fname in
debug_log_lwt (Printf.sprintf "[livereload] %s%!" server_path) >>= fun _ ->
change_cb server_path
| _ -> Lwt.return ()
end;
>>= fun _ -> go ()
in go ()

let make_handler
?(debug=false)
(path_configs : path_config list)
(next : handler)
: handler
= let send_update_fn, handler = make_raw_handler ~debug next in
Lwt.async (fun _ -> make_watcher ~debug path_configs send_update_fn);
handler


6 changes: 1 addition & 5 deletions test/test_files_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ let make_handler () =
~body:(Sexplib.Sexp.to_string_hum (Cohttp.Request.sexp_of_t req))
()
in
let send_update_fn, handler = Livereload.make_handler next in
let _ =
let watcher = Livereload.make_watcher ["test/static", "/static"] send_update_fn in
Lwt.async (fun _ -> watcher)
in
let handler = Livereload.make_handler [{ fs_dir = "test/static"; server_base = "/static" }] next in
fun conn req body ->
Lwt_io.eprintf "[CONN] %s\n%!" (Cohttp.Connection.to_string @@ snd conn)
>>= fun _ ->
Expand Down
2 changes: 1 addition & 1 deletion test/test_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let make_handler () =
~body:(Sexplib.Sexp.to_string_hum (Cohttp.Request.sexp_of_t req))
()
in
let send_update_fn, handler = Livereload.make_handler next in
let send_update_fn, handler = Livereload.make_raw_handler next in
let _ =
let rec gocss (c : string) =
Lwt_unix.sleep 3.
Expand Down

0 comments on commit f0dd8bb

Please sign in to comment.