diff options
| author | Marius Peter <marius.peter@tutanota.com> | 2025-02-02 17:08:52 +0100 | 
|---|---|---|
| committer | Marius Peter <marius.peter@tutanota.com> | 2025-02-02 17:08:52 +0100 | 
| commit | aacec6588c3aebffda6c6221b02622576c85c407 (patch) | |
| tree | 213da645d447ac0e82a9209412f94fa78a8bc594 | |
| parent | 6e8e49ba49beeeb63e1e027fc23e8883a1185109 (diff) | |
Change Views render function return type.
All Views submodules' render functions now return a Dream_html node
promise, rather than the node itself.
| -rw-r--r-- | bin/main.ml | 7 | ||||
| -rw-r--r-- | lib/config.ml | 1 | ||||
| -rw-r--r-- | lib/dune | 2 | ||||
| -rw-r--r-- | lib/handlers.ml | 6 | ||||
| -rw-r--r-- | lib/views.ml | 100 | 
5 files changed, 65 insertions, 51 deletions
| diff --git a/bin/main.ml b/bin/main.ml index 895bfb8..c30e4fc 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -2,8 +2,9 @@ let () =    Dream.run @@ Dream.logger    @@ Dream.router         [ -         Dream.get "/" (fun _ -> Ogit.Handlers.ogit_root ()); -         Dream.get "/:repo_name" (fun request -> -             Ogit.Handlers.repo_root @@ Dream.param request "repo_name"); +         Dream.get "/" (fun _req -> Ogit.Handlers.ogit_root _req); +         Dream.get "/:repo_name" (fun req -> +             let repo_name = Dream.param req "repo_name" in +             Ogit.Handlers.repo_root repo_name);           Dream.get "/static/**" (Dream.static "./lib/static");         ] diff --git a/lib/config.ml b/lib/config.ml index 4aab858..d47101f 100644 --- a/lib/config.ml +++ b/lib/config.ml @@ -1 +1,2 @@  let git_directory = Filename.concat (Unix.getenv "HOME") "git" +let author = "Marius Peter" @@ -1,5 +1,5 @@  (library   (name ogit) - (libraries dream dream-html lwt_ppx git-unix) + (libraries dream dream-html git-unix)   (preprocess    (pps lwt_ppx))) diff --git a/lib/handlers.ml b/lib/handlers.ml index 1f6d920..7c80bcb 100644 --- a/lib/handlers.ml +++ b/lib/handlers.ml @@ -1,4 +1,6 @@ -let ogit_root _ = Dream_html.respond @@ Views.Ogit_root.render () +open Lwt.Infix + +let ogit_root _ = Views.Ogit_root.render () >>= Dream_html.respond  let repo_root repo_name = -  Dream_html.respond @@ Lwt_main.run @@ Views.Repo_root.render repo_name +  Views.Repo_root.render repo_name >>= Dream_html.respond diff --git a/lib/views.ml b/lib/views.ml index 64b003e..b625ca6 100644 --- a/lib/views.ml +++ b/lib/views.ml @@ -1,30 +1,18 @@ -type page_data = { title : string; main_content : Dream_html.node list } +type head_data = { page_title : string } + +type body_data = { +  title : string; +  subtitle : string; +  topnav : Dream_html.node; +  content : Dream_html.node list; +}  module Layout = struct    open Dream_html    open HTML -  let header = -    null -      [ -        h1 [] [ txt "ogit" ]; -        h2 [] [ txt "A mobile-friendly Git repository viewer" ]; -      ] - -  let topnav = -    nav -      [ id "top" ] -      [ -        ul [] -          [ -            li [] [ a [ href "/" ] [ txt "Home" ] ]; -            li [] [ a [ href "/" ] [ txt "Refs" ] ]; -            li [] [ a [ href "/" ] [ txt "Log" ] ]; -            li [] [ a [ href "/" ] [ txt "Tree" ] ]; -            li [] [ a [ href "/" ] [ txt "Commit" ] ]; -            li [] [ a [ href "/" ] [ txt "Diff" ] ]; -          ]; -      ] +  let header title subtitle = +    null [ h1 [] [ txt "%s" title ]; h2 [] [ txt "%s" subtitle ] ]    let footer name =      let today = Unix.localtime (Unix.time ()) in @@ -33,16 +21,21 @@ module Layout = struct      let footer_text = String.concat space [ "©"; year; name ] in      footer [] [ txt "%s" footer_text ] -  let application page = +  let head_data = { page_title = "Ogit" } + +  let application ?(head_data = head_data) body_data =      html []        [ -        head [] [ title [] "%s" page.title ]; -        link [ rel "stylesheet"; href "/static/styles.css" ]; +        head [] +          [ +            title [] "%s" head_data.page_title; +            link [ rel "stylesheet"; href "/static/styles.css" ]; +          ];          body []            [ -            header; -            topnav; -            div [ id "main" ] page.main_content; +            header body_data.title body_data.subtitle; +            body_data.topnav; +            div [ id "main" ] body_data.content;              footer "Marius PETER";            ];        ] @@ -57,13 +50,15 @@ module Ogit_root = struct      and li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in      div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repositories ] -  let page_data = +  let body_data =      {        title = "My repositories"; -      main_content = [ repositories_in Config.git_directory ]; +      subtitle = "Repositories for " ^ Config.author; +      topnav = null []; +      content = [ repositories_in Config.git_directory ];      } -  let render () = Layout.application page_data +  let render () = Lwt.return @@ Layout.application body_data  end  module Repo_root = struct @@ -72,7 +67,22 @@ module Repo_root = struct    open Git    open Lwt.Infix -  let page_promise repo_name = +  let topnav = +    nav +      [ id "top" ] +      [ +        ul [] +          [ +            li [] [ a [ href "/" ] [ txt "Home" ] ]; +            li [] [ a [ href "/" ] [ txt "Refs" ] ]; +            li [] [ a [ href "/" ] [ txt "Log" ] ]; +            li [] [ a [ href "/" ] [ txt "Tree" ] ]; +            li [] [ a [ href "/" ] [ txt "Commit" ] ]; +            li [] [ a [ href "/" ] [ txt "Diff" ] ]; +          ]; +      ] + +  let render repo_name =      let repo_path = Filename.concat Config.git_directory repo_name in      (* 1. Open the Git repository *) @@ -84,7 +94,7 @@ module Repo_root = struct      (* 2. Resolve HEAD to get the latest commit hash *)      let%lwt commit_hash = -      Git_unix.Store.Ref.resolve repo Reference.head >>= function +      Git_unix.Store.Ref.resolve repo Reference.master >>= function        | Ok hash -> Lwt.return hash        | Error _ -> Lwt.fail_with "Failed to resolve HEAD"      in @@ -114,23 +124,23 @@ module Repo_root = struct      (* in *)      (* 6. Assemble the page *) -    let main_content = -      [ -        ul [] -          [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_raw_string) ] ]; -      ] +    let content = +      [ ul [] [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_hex) ] ] ]      in -    let page_data = { title = repo_name; main_content } in -    Lwt.return (Layout.application page_data) - -  let render repo_name = page_promise repo_name >>= fun page -> Lwt.return page +    let title = repo_name in +    let subtitle = Git_unix.(repo |> Store.root |> Fpath.to_string) in +    let body_data = { title; subtitle; topnav; content } in +    Lwt.return @@ Layout.application body_data  end  module Repo_tree = struct    open Dream_html +  open HTML    let render repo_name = -    let title = repo_name and main_content = [ txt "foobar" ] in -    let page_data = { title; main_content } in -    Layout.application page_data +    let title = repo_name and content = [ txt "foobar" ] in +    let subtitle = "Dinglefops" in +    let topnav = null [] in +    let body_data = { title; subtitle; topnav; content } in +    Lwt.return @@ Layout.application body_data  end | 
