読者です 読者をやめる 読者になる 読者になる

opiumいじってる

# $ opam switch 4.02.1
$ opam install opium
$ opam install core
open Core_kernel.Std
module O = Opium.Std

type person = {
  name: string;
  age: int;
}

let json_of_person { name ; age } =
  let open Ezjsonm in
  dict [ "name", (string name)
       ; "age", (int age) ]

let print_param = O.put "/hello/:name" begin fun req ->
  `String ("Hello " ^ O.param req "name") |> O.respond'
end

let print_person = O.get "/person/:name/:age" begin fun req ->
  let person = {
    name = O.param req "name";
    age = "age" |> O.param req |> Int.of_string;
  } in
  `Json (person |> json_of_person |> Ezjsonm.wrap) |> O.respond'
end

let _ =
  O.App.empty
  |> print_param
  |> print_person
  |> O.App.run_command
$ corebuild -pkg opium,cow.syntax hello_world.native
$ ./hello_world.native
$ http get http://localhost:3000/foo/10
GET /person/foo/10 HTTP/1.1
Connection: keep-alive
Host: 127.0.0.1:3000
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: HTTPie/0.9.2



HTTP/1.1 200 OK
content-length: 25
content-type: application/json

[{"name":"foo","age":10}]