summaryrefslogtreecommitdiff
path: root/lib/handlers.ml
blob: deb96356abe5b5c5fb1af04847b42bcadfa54677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
(* -*- mode: tuareg; -*- *)

let root _req = Views.root () |> Dream_html.respond

module Repo = struct
  open Git_presenters

  let ( let* ) m f =
    let open Lwt.Infix in
    m >>= function
    | Ok x -> f x
    | Error e ->
        let msg = Format.asprintf "%a" Store.pp_error e in
        Views.error_page msg |> Dream_html.respond

  let repo req = Dream.param req "repo"
  let id_of_req req = Dream.param req "id"

  let summary req =
    let* branches = all_branches (repo req) in
    let* commits = recent_commits (repo req) 10 in
    let authors = [ "John Pork"; "Sebastian Jellybean" ] in
    Views.Repo.summary (repo req) branches commits authors |> Dream_html.respond

  let log req =
    let* commits = recent_commits (repo req) 100 in
    Views.Repo.log (repo req) commits |> Dream_html.respond

  let files_at_head req =
    let* tree = head_tree (repo req) in
    Views.Repo.files (repo req) tree |> Dream_html.respond

  let file_id req =
    let id = id_of_req req in
    let* blob = blob_of_id (repo req) id in
    Views.Repo.file (repo req) blob |> Dream_html.respond

  let refs req =
    let* branches = all_branches (repo req) in
    Views.Repo.refs (repo req) branches |> Dream_html.respond

  let commit req =
    let id = id_of_req req in
    let* commit = commit_of_id (repo req) id in
    Views.Repo.commit (repo req) commit |> Dream_html.respond
end

let all_handlers =
  Dream.
    [
      get "/" root;
      scope "/:repo" []
        Repo.
          [
            get "/" summary;
            get "/summary/" summary;
            get "/log/" log;
            get "/files/" files_at_head;
            get "/files/:id" file_id;
            get "/refs/" refs;
            get "/commit/:id" commit;
          ];
      get "/static/**" (static "./lib/static");
    ]
Copyright 2019--2025 Marius PETER