server.ml 1.08 KB
Newer Older
zapashcanon's avatar
zapashcanon committed
1 2 3 4
let render ~lang ~title ~content =
  Template.render_unsafe ~lang ~title ~content:(Dream.html_escape content)

let asset_loader _root path _request =
zapashcanon's avatar
zapashcanon committed
5
  match Content.read ("assets/" ^ path) with
zapashcanon's avatar
zapashcanon committed
6 7 8 9
  | None -> Dream.empty `Not_Found
  | Some asset -> Dream.respond asset

let page path =
zapashcanon's avatar
zapashcanon committed
10
  match Content.read (path ^ ".md") with
zapashcanon's avatar
zapashcanon committed
11 12 13 14 15 16 17 18 19 20 21 22
  | 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 ->
               Dream.html
zapashcanon's avatar
typo  
zapashcanon committed
23
                 (Template.render_unsafe ~lang:"en" ~title:"title" ~content) )
zapashcanon's avatar
zapashcanon committed
24 25 26 27 28 29 30 31
       ; Dream.get "/:page" (fun request ->
             match page (Dream.param "page" request) with
             | None -> Dream.empty `Not_Found
             | Some content ->
               Dream.html
                 (Template.render_unsafe ~lang:"en" ~title:"title" ~content) )
       ]
  @@ Dream.not_found