diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/handlers/dune | 8 | ||||
-rw-r--r-- | lib/handlers/handlers.ml | 43 | ||||
-rw-r--r-- | lib/helpers/dune | 9 | ||||
-rw-r--r-- | lib/helpers/file_helpers.ml | 7 | ||||
-rw-r--r-- | lib/helpers/html_helpers.ml | 18 | ||||
-rw-r--r-- | lib/layouts/dune | 16 | ||||
-rw-r--r-- | lib/layouts/header.eml.html | 3 | ||||
-rw-r--r-- | lib/layouts/topnav.eml.html | 11 |
8 files changed, 115 insertions, 0 deletions
diff --git a/lib/handlers/dune b/lib/handlers/dune new file mode 100644 index 0000000..ae64b71 --- /dev/null +++ b/lib/handlers/dune @@ -0,0 +1,8 @@ +(library + (name handlers) + (public_name ogit.handlers) + (libraries + dream + ogit.helpers + ogit.layouts) + (modules handlers)) diff --git a/lib/handlers/handlers.ml b/lib/handlers/handlers.ml new file mode 100644 index 0000000..7142439 --- /dev/null +++ b/lib/handlers/handlers.ml @@ -0,0 +1,43 @@ +(* Page layout function *) +let page_layout ~content = + "<html><body>" + ^ Layouts.Header.header () + ^ Layouts.Topnav.topnav () + ^ "<main>" ^ content ^ "</main>" + ^ "</body></html>" + +let root _ = + let content = + "<h1>Available Repositories</h1><ul>" + ^ Helpers.Html_helpers.generate_repo_links () + ^ "</ul>" + in + Dream.html (page_layout ~content) + +let repo_root _ = + (* let repo_name = Dream.param request "repo_name" in *) + let content = + "<div class=\"content\"><table summary=\"repository info\" class=\"list nowrap\"><tbody>\n" + ^ "<tr class=\"nohover\"><th class=\"left\">Branch</th><th class=\"left\">Commit message</th><th class=\"left\">Author</th><th class=\"left\" colspan=\"2\">Age</th></tr>\n" + ^ "<tr><td><a href=\"/wtt.git/log/\">master</a></td><td><a href=\"/wtt.git/commit/\">Ensure session[:id] before scoring: all tests now pass.</a></td><td>Marius Peter</td><td colspan=\"2\"><span class=\"age-weeks\" title=\"2025-01-02 19:11:34 +0100\">2 weeks</span></td></tr>\n" + ^ "<tr class=\"nohover\"><td colspan=\"3\"> </td></tr>\n" + ^ "<tr class=\"nohover\"><th class=\"left\">Tag</th><th class=\"left\">Download</th><th class=\"left\">Author</th><th class=\"left\" colspan=\"2\">Age</th></tr>\n" + ^ "<tr><td><a href=\"/wtt.git/tag/?h=v1.0\">v1.0</a></td><td><a href=\"/wtt.git/commit/?id=175111f9d84354dce00503525649197e9acb6382\">commit 175111f9d8...</a></td><td>Marius Peter</td><td colspan=\"2\"><span class=\"age-weeks\" title=\"2025-01-02 13:58:46 +0100\">2 weeks</span></td></tr>\n" + ^ "<tr class=\"nohover\"><td colspan=\"3\"> </td></tr>\n" + ^ "<tr class=\"nohover\"><th class=\"left\">Age</th><th class=\"left\">Commit message</th><th class=\"left\">Author</th></tr>\n" + ^ "<tr><td><span title=\"2025-01-02 19:11:34 +0100\">2025-01-02</span></td><td><a href=\"/wtt.git/commit/?id=b34d2b51174b511d59fa324d9f42abc75a1fa09a\">Ensure session[:id] before scoring: all tests now pass.</a><span class=\"decoration\"><a class=\"deco\" href=\"/wtt.git/commit/?id=b34d2b51174b511d59fa324d9f42abc75a1fa09a\">HEAD</a><a class=\"branch-deco\" href=\"/wtt.git/log/\">master</a></span></td><td>Marius Peter</td></tr>\n" + ^ "</tbody></table></div>" + in + Dream.html (page_layout ~content) + +let repo_tree request = + let repo_name = Dream.param request "repo_name" in + let repo_path = Filename.concat (Unix.getenv "HOME") ("git/" ^ repo_name) in + let content = + if Sys.file_exists repo_path && Sys.is_directory repo_path then + let files = Helpers.File_helpers.list_files repo_path in + "<h1>Browsing repository: " ^ repo_name ^ "</h1><ul>" ^ files ^ "</ul>" + else + "<h1>Repository not found: " ^ repo_name ^ "</h1>" + in + Dream.html (page_layout ~content) diff --git a/lib/helpers/dune b/lib/helpers/dune new file mode 100644 index 0000000..a8c1733 --- /dev/null +++ b/lib/helpers/dune @@ -0,0 +1,9 @@ +(library + (name helpers) + (public_name ogit.helpers) + (libraries + unix + ogit.layouts) + (modules + html_helpers + file_helpers)) diff --git a/lib/helpers/file_helpers.ml b/lib/helpers/file_helpers.ml new file mode 100644 index 0000000..892d807 --- /dev/null +++ b/lib/helpers/file_helpers.ml @@ -0,0 +1,7 @@ +let list_files dir = + try + Array.to_list (Sys.readdir dir) + |> List.map (fun file -> Printf.sprintf "<li>%s</li>" file) + |> String.concat "" + with _ -> + "<li>Error: Unable to read directory</li>" diff --git a/lib/helpers/html_helpers.ml b/lib/helpers/html_helpers.ml new file mode 100644 index 0000000..da5addc --- /dev/null +++ b/lib/helpers/html_helpers.ml @@ -0,0 +1,18 @@ +(* Helper function to generate links for repositories *) +let generate_repo_links () = + let git_dir = Filename.concat (Unix.getenv "HOME") "git" in + if Sys.file_exists git_dir && Sys.is_directory git_dir then + Sys.readdir git_dir + |> Array.to_list + |> List.map (fun repo -> Printf.sprintf "<li><a href='/%s/tree'>%s</a></li>" repo repo) + |> String.concat "" + else + "<li>Error: '~/git' directory not found.</li>" + +(* Page layout function *) +let page_layout ~content = + "<html><body>" + ^ Layouts.Header.header () + ^ Layouts.Topnav.topnav () + ^ "<main>" ^ content ^ "</main>" + ^ "</body></html>" diff --git a/lib/layouts/dune b/lib/layouts/dune new file mode 100644 index 0000000..ee236ea --- /dev/null +++ b/lib/layouts/dune @@ -0,0 +1,16 @@ +(library + (name layouts) + (public_name ogit.layouts) + (modules header topnav)) + +; Rule to preprocess header.eml.html +(rule + (targets header.ml) + (deps header.eml.html) + (action (run dream_eml %{deps} --workspace %{workspace_root}))) + +; Rule to preprocess topnav.eml.html +(rule + (targets topnav.ml) + (deps topnav.eml.html) + (action (run dream_eml %{deps} --workspace %{workspace_root}))) diff --git a/lib/layouts/header.eml.html b/lib/layouts/header.eml.html new file mode 100644 index 0000000..d7bf4ef --- /dev/null +++ b/lib/layouts/header.eml.html @@ -0,0 +1,3 @@ +let header _ = + <h1>ogit</h1> + <h2>Your mobile-friendly Git repository viewer.</h2> diff --git a/lib/layouts/topnav.eml.html b/lib/layouts/topnav.eml.html new file mode 100644 index 0000000..a9f0864 --- /dev/null +++ b/lib/layouts/topnav.eml.html @@ -0,0 +1,11 @@ +let topnav _ = +<nav id="top"> + <ul> + <li><a href="/">Home</a></li> + <li><a href="/">Refs</a></li> + <li><a href="/">Log</a></li> + <li><a href="/">Tree</a></li> + <li><a href="/">Commit</a></li> + <li><a href="/">Diff</a></li> + </ul> +</nav> |