Skip to content

Commit

Permalink
Expose default static file loader
Browse files Browse the repository at this point in the history
Part of #14.
  • Loading branch information
aantron committed Apr 27, 2021
1 parent c1ba95a commit fd7023d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 1 addition & 3 deletions example/z-playground/server/playground.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ let () =
Dream.empty `Not_Found
else
let%lwt response =
Dream__middleware.Static.default_loader
"static" "playground.html" request in
let response : Dream.response = Obj.magic response in
Dream.from_filesystem "static" "playground.html" request in
Dream.with_header "Content-Type" "text/html; charset=utf-8" response
|> Lwt.return);

Expand Down
20 changes: 17 additions & 3 deletions src/dream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,10 @@ val any : string -> handler -> route
val not_found : handler
(** Always responds with [404 Not Found]. *)



(** {1 Static files} *)

val static :
?loader:(string -> string -> handler) ->
string -> handler
Expand Down Expand Up @@ -1232,9 +1236,7 @@ val static :
{!Dream.static}.
- [path] is what was substituted for [**].
The default loader joins [local_directory] and [path], and uses the result
to respond with a file from the file system. If such a file does not exist,
the default loader responds with [404 Not Found].
The default loader is {!Dream.from_filesystem}.
See example
{{:https://github.com/aantron/dream/tree/master/example/w-one-binary#files}
Expand All @@ -1251,6 +1253,18 @@ val static :
[ETag]}. See {{:https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching}
MDN {i HTTP caching}}. *)

val from_filesystem : string -> string -> handler
(** [Dream.from_filesystem local_directory path request] responds with a file
from the file system found at [local_directory ^ "/" ^ path].
If such a file does not exist, it responds with [404 Not Found].
To serve single files like [sitemap.xml] from the file system, use routes
like
{[
Dream.get "/sitemap.xml" (Dream.from_filesystem "assets" "sitemap.xml")
]} *)



(** {1 Sessions}
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/static.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Dream = Dream__pure.Inmost
(* TODO NOTE Using Lwt_io because it has a nice "read the whole thing"
function. *)

let default_loader local_root path _ =
let from_filesystem local_root path _ =
let file = Filename.concat local_root path in
Lwt.catch
(fun () ->
Expand Down Expand Up @@ -60,7 +60,7 @@ let validate_path request =
else
None

let static ?(loader = default_loader) local_root = fun request ->
let static ?(loader = from_filesystem) local_root = fun request ->

if not @@ Dream.methods_equal (Dream.method_ request) `GET then
Dream.empty `Not_Found
Expand Down

0 comments on commit fd7023d

Please sign in to comment.