server.ml 2.38 KB
Newer Older
zapashcanon's avatar
zapashcanon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
let render ~title ~content =
  Template.render_unsafe ~title ~content:(Dream.html_escape content)

let asset_loader _root path _request =
  match Content.read ("assets/" ^ path) with
  | None -> Dream.empty `Not_Found
  | Some asset -> Dream.respond asset

let page path =
  match Content.read (path ^ ".md") with
  | None -> None
  | Some page -> Some (Omd.of_string page |> Omd.to_html)

let () =
  Dream.run @@ Dream.logger
  @@ Dream.router
       [ Dream.get "/assets/**" (Dream.static ~loader:asset_loader "")
       ; Dream.get "/" (fun _request ->
             match page "index" with
             | None -> Dream.empty `Not_Found
             | Some content ->
zapashcanon's avatar
zapashcanon committed
22 23
               Dream.html
                 (Template.render_unsafe ~title:"Osh by OCamlPro" ~content) )
zapashcanon's avatar
zapashcanon committed
24
       ; Dream.get "/badge" (fun request ->
zapashcanon's avatar
fmt  
zapashcanon committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
             Dream.respond
               ~headers:[ ("Content-Type", "image/svg+xml") ]
               (let open Ocb in
               let label =
                 match Dream.query "label" request with
                 | None -> "Label"
                 | Some label -> label
               in
               let color =
                 match Dream.query "color" request with
                 | None -> Color.Blue
                 | Some color -> Color.of_string color
               in
               let style =
                 match Dream.query "style" request with
                 | None -> Style.Flat
                 | Some style -> Style.of_string style
               in
               let label_color =
                 match Dream.query "label_color" request with
                 | None -> Color.Black
                 | Some label_color -> Color.of_string label_color
               in
               let status =
                 match Dream.query "status" request with
                 | None -> "Status"
                 | Some status -> status
               in
               let scale =
                 match Dream.query "scale" request with
                 | None -> 1.
                 | Some scale -> begin
                   match float_of_string_opt scale with
                   | None -> 1.
                   | Some scale -> scale
                 end
               in
               Format.asprintf "%a"
                 (Gen.mk ~label ~color ~style ~label_color ~status ~icon:None
                    ~icon_width:0. ~scale )
                 ()) )
zapashcanon's avatar
zapashcanon committed
66 67
       ]
  @@ Dream.not_found