add a VFS in examples/echo.ml

This commit is contained in:
Simon Cruanes 2022-03-03 22:14:44 -05:00
parent f84b2df97f
commit 759995b9d5
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
6 changed files with 58 additions and 2 deletions

View file

@ -12,7 +12,7 @@
(executable
(name echo)
(flags :standard -warn-error -a+8)
(modules echo)
(modules echo vfs)
(libraries tiny_httpd tiny_httpd_camlzip))
(rule
@ -30,3 +30,13 @@
(deps test_output.txt)
(action
(diff test_output.txt.expected test_output.txt)))
; produce an embedded FS
(rule
(targets vfs.ml)
(deps (source_tree files) (:out test_output.txt.expected))
(enabled_if (= %{system} "linux"))
(action (run ../src/bin/vfs_pack.exe -o %{targets}
--mirror=files/
--file=test_out.txt,%{out}
--url=example_dot_com,http://example.com)))

View file

@ -107,16 +107,23 @@ let () =
S.Response.make_string @@ Ok stats
);
(* VFS *)
Tiny_httpd_dir.add_vfs server
~config:(Tiny_httpd_dir.config ~download:true
~dir_behavior:Tiny_httpd_dir.Index_or_lists ())
~vfs:Vfs.vfs ~prefix:"vfs";
(* main page *)
S.add_route_handler server S.Route.(return)
(fun _req ->
let s = "<head></head><body>\n\
<p><b>welcome!</b>\n<p>endpoints are:\n<ul>\
<li><pre>/hello/'name' (GET)</pre></li>\n\
<li><pre>/echo/ (GET) echoes back query</pre></li>\n\
<li><pre><a href=\"/echo/\">/echo/</a> (GET) echoes back query</pre></li>\n\
<li><pre>/upload/'path' (PUT) to upload a file</pre></li>\n\
<li><pre>/zcat/'path' (GET) to download a file (compressed)</pre></li>\n\
<li><pre>/stats/ (GET) to access statistics</pre></li>\n\
<li><pre><a href=\"/vfs/\">/vfs/</a> (GET) to access statistics</pre></li>\n\
</ul></body>"
in
S.Response.make_string ~headers:["content-type", "text/html"] @@ Ok s);

2
examples/files/a.txt Normal file
View file

@ -0,0 +1,2 @@
hello
world

18
examples/files/foo.html Normal file
View file

@ -0,0 +1,18 @@
<html>
<head>
</head>
<body>
<h1> hello! </h1>
<ul>
<li> item 1 </li>
<li> item 2 </li>
<li> item 3 </li>
<li> item 4 </li>
</ul>
<a href="."> escape from this file </a>
</body>
</html>

1
examples/files/sub/b.txt Normal file
View file

@ -0,0 +1 @@
lorem ipsum etc.

View file

@ -0,0 +1,18 @@
<html>
<head>
</head>
<body>
<h2> funky: </h2>
<ul>
<li>
<a href="../"> go up!! </a>
</li>
<li>
<a href="../foo.html"> up/foo </a>
</li>
</ul>
</body>
</html>