open Database open Lwt module Render = struct let not_found_template = "404 Not found" let error_template = "500 Server Error" let header_template = {eos| rawley.xyz

/home/rawley.xyz

|eos} let footer_template = {eos|
|eos} let render_page content = Printf.sprintf {eos| %s %s %s |eos} header_template content footer_template let generate_link (p : BlogPost.t) = Printf.sprintf {eos| |eos} p.slug p.title p.date let handle_error e = print_endline (Caqti_error.show e); error_template |> Dream.html let render_blog_post request = let slug = Dream.param request "post" in let post_t = Database.get_blog_post_by_slug slug in post_t >>= fun post -> match post with | Error e -> handle_error e | Ok p_opt -> match p_opt with | None -> not_found_template |> Dream.html | Some p -> Printf.sprintf "

%s

\r\n%s" p.title p.content |> render_page |> Dream.html let render_blog_index _ = let buff = Buffer.create 512 in let () = Buffer.add_string buff "

Blog

" in let posts_t = Database.get_all_blog_posts () in posts_t >>= function | Error e -> handle_error e | Ok posts -> List.iter (fun p -> Buffer.add_string buff (generate_link p)) posts; let c = if List.length posts <> 0 then render_page @@ Buffer.contents buff else render_page "
No blog posts..." in Dream.html c end