Fix examples
This commit is contained in:
parent
396555f397
commit
6cbf850340
14 changed files with 37 additions and 25 deletions
29
README.md
29
README.md
|
@ -9,21 +9,31 @@ However, we do not recommend using this project in production.
|
||||||
Vif is a small program that runs an OCaml script and launches a Web server from
|
Vif is a small program that runs an OCaml script and launches a Web server from
|
||||||
it. The main idea is to be able to set up a typed Web server as quickly as
|
it. The main idea is to be able to set up a typed Web server as quickly as
|
||||||
possible (note that we use [hurl][hurl], an HTTP client in OCaml)
|
possible (note that we use [hurl][hurl], an HTTP client in OCaml)
|
||||||
```ocaml
|
```shell
|
||||||
$ opam pin add -y https://github.com/robur-coop/vif
|
$ opam pin add -y https://github.com/robur-coop/vif
|
||||||
$ opam pin add -y https://github.com/robur-coop/hurl
|
$ opam pin add -y https://github.com/robur-coop/hurl
|
||||||
$ opam install vif hurl
|
$ opam install vif hurl
|
||||||
$ cat >main.ml <<EOF
|
$ cat >main.ml <<EOF
|
||||||
#require "vif" ;;
|
#require "vif" ;;
|
||||||
|
|
||||||
let default req target server () =
|
open Vif ;;
|
||||||
let headers = [ "content-type", "text/html" ] in
|
|
||||||
Vif.Response.with_string ~headers server `OK "Hello World!\n"
|
let default req server () =
|
||||||
|
let field = "content-type" in
|
||||||
|
let* () = Response.add ~field "text/html; charset=utf-8" in
|
||||||
|
let* () = Response.with_string req "Hello World!" in
|
||||||
|
Response.respond `OK
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
let routes =
|
||||||
|
let open Vif.U in
|
||||||
|
let open Vif.R in
|
||||||
|
let open Vif.T in
|
||||||
|
[ get (rel /?? nil) --> default ]
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Miou_unix.run @@ fun () ->
|
Miou_unix.run @@ fun () ->
|
||||||
Vif.run ~default [] ()
|
Vif.run routes ()
|
||||||
;;
|
;;
|
||||||
EOF
|
EOF
|
||||||
$ vif --pid vif.pid main.ml &
|
$ vif --pid vif.pid main.ml &
|
||||||
|
@ -31,7 +41,7 @@ $ hurl http://localhost:8080/
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
|
|
||||||
connection: close
|
connection: close
|
||||||
content-length: 13
|
content-length: 12
|
||||||
content-type: text/html
|
content-type: text/html
|
||||||
|
|
||||||
Hello World!
|
Hello World!
|
||||||
|
@ -39,4 +49,11 @@ Hello World!
|
||||||
$ kill -SIGINT $(cat vid.pid)
|
$ kill -SIGINT $(cat vid.pid)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
The [examples][./examples] folder contains several examples of the use of `vif`.
|
||||||
|
It shows the management of more complex requests (json, multipart-form, etc.) as
|
||||||
|
well as the use of an SQL database with [caqti][caqti].
|
||||||
|
|
||||||
[hurl]: https://github.com/robur-coop/hurl
|
[hurl]: https://github.com/robur-coop/hurl
|
||||||
|
[caqti]: https://github.com/paurkedal/ocaml-caqti/
|
||||||
|
|
|
@ -3,16 +3,18 @@
|
||||||
open Vif ;;
|
open Vif ;;
|
||||||
|
|
||||||
let default req _server () =
|
let default req _server () =
|
||||||
let* () = Response.with_string req "Hello World!\n" in
|
let str = "Hello World!\n" in
|
||||||
|
let* () = Response.with_string req str in
|
||||||
Response.respond `OK
|
Response.respond `OK
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let () = Miou_unix.run @@ fun () ->
|
let () = Miou_unix.run @@ fun () ->
|
||||||
Vif.run routes () ;;
|
Vif.run routes ()
|
||||||
|
;;
|
||||||
|
|
|
@ -15,7 +15,7 @@ let default req _server () =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
|
|
||||||
let () = Miou_unix.run @@ fun () ->
|
let () = Miou_unix.run @@ fun () ->
|
||||||
|
|
|
@ -11,7 +11,7 @@ let default req server () = raise Foo ;;
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ let default req server () =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
|
|
Binary file not shown.
|
@ -56,7 +56,7 @@ let list req server _cfg =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ post any (rel / "add" /% Tyre.int /?? nil) --> add
|
[ post any (rel / "add" /% Tyre.int /?? nil) --> add
|
||||||
; get (rel /?? nil) --> list ]
|
; get (rel /?? nil) --> list ]
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ let default req server () =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
|
|
||||||
let () = Miou_unix.run @@ fun () ->
|
let () = Miou_unix.run @@ fun () ->
|
||||||
|
|
|
@ -24,7 +24,7 @@ let default req server () =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ post any (rel /?? nil) --> default ]
|
[ post any (rel /?? nil) --> default ]
|
||||||
|
|
||||||
let () = Miou_unix.run @@ fun () ->
|
let () = Miou_unix.run @@ fun () ->
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
# Netscape HTTP Cookie File
|
|
||||||
# https://curl.se/docs/http-cookies.html
|
|
||||||
# This file was generated by libcurl! Edit at your own risk.
|
|
||||||
|
|
||||||
#HttpOnly_localhost FALSE / TRUE 0 __Host-vif-token AIT3qkbKq7NBg7ePl7Xbs9ikzUNdcxLuKYbinTM3KQdTVm2jAHY36a8c9mOFSf5opEudhbozpU2oXxt_bfCkR9YlqAlzjK8OYcLwgBVMLZaJBiontKM9BdS70yUDdPawY4h883n9P0A9lEUhMNMQr7-NVieuIgZTGbtTV-B7Xrcf9pipVYOIiyiPcILaoDwa-ycs-_b9yqlyzDYfMPOwsWphSPR0Ji9lVg
|
|
|
@ -1,2 +0,0 @@
|
||||||
{ "username": "dinosaure",
|
|
||||||
"password": "foo" }
|
|
|
@ -21,7 +21,7 @@ let default req server _cfg =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel / "echo" /% string /?? nil) --> hello
|
[ get (rel / "echo" /% string /?? nil) --> hello
|
||||||
; get (rel /?? nil) --> default ]
|
; get (rel /?? nil) --> default ]
|
||||||
;;
|
;;
|
||||||
|
|
|
@ -13,7 +13,7 @@ let cat req server _ =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ post any (rel /?? nil) --> cat ]
|
[ post any (rel /?? nil) --> cat ]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ let default req server _ =
|
||||||
let routes =
|
let routes =
|
||||||
let open Vif.U in
|
let open Vif.U in
|
||||||
let open Vif.R in
|
let open Vif.R in
|
||||||
let open Vif.Content_type in
|
let open Vif.T in
|
||||||
[ get (rel /?? nil) --> default ]
|
[ get (rel /?? nil) --> default ]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue