f-static
Run this example:
$ dune exec --root . ./static.exe
...and visit http://localhost:8080/static/static.ml. You will see that it prints this example's source code!
let () =
Dream.run
@@ Dream.logger
@@ Dream.router [
Dream.get "/static/*" (Dream.static ".")
]
@@ Dream.not_found
That is because the example uses
Dream.static to serve this
very directory at /static! Obviously, you shouldn't do this in a real app
— serve a subdirectory instead.
The static route ends with *. This is a subsite
route. Generally, you should
prefer Dream.scope to *,
because Dream.scope will
support router introspection, if it is added in the future.
However, * is exactly what it is needed for
Dream.static. Pure
introspection of a static subsite is impossible to begin with, because the
available sub-routes depend on the actual files in the file system.
If you inspect the response headers for our request, you will see
Content-Type: text/x-ocaml. That is because
Dream.static uses
magic-mime to guess
Content-Type: based on the extension.
You can replace the file loading behavior of
Dream.static by passing it a
~handler argument. One possibility is to use
crunch to compile a directory right
into your Web app binary, and then serve that directory from memory with
Dream.static!
You can also use ~handler to set arbitrary headers on the response.
Next steps: