| 1 | -- app.lua |
| 2 | -- Entry point for git HTTP site implementation |
| 3 |
|
| 4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
| 5 | -- <https://joshstock.in> |
| 6 |
|
| 7 | local lyaml = require("lyaml") |
| 8 | local utils = require("utils") |
| 9 | local git = require("git_commands") |
| 10 | local request = require("request") |
| 11 | local tabulate = require("tabulate") |
| 12 | local html = require("html") |
| 13 |
|
| 14 | local parsed_uri = request.parse_uri() |
| 15 | local content |
| 16 |
|
| 17 | if parsed_uri.repo == nil then |
| 18 | -- home page, list of repositories |
| 19 | else -- repo found |
| 20 | local repo |
| 21 | for _,r in pairs(yaml_config) do |
| 22 | if parsed_uri.repo == r.name then |
| 23 | repo = r |
| 24 | break |
| 25 | end |
| 26 | end |
| 27 | if repo then |
| 28 | local repo_dir = repo.location.dev |
| 29 | local view = parsed_uri.parts[2] or "tree" |
| 30 | local branch |
| 31 | if pcall(function() -- if branch is real |
| 32 | branch = git.get_head(repo_dir, parsed_uri.parts[3]) -- if parts[3] is nil, defaults to "HEAD" |
| 33 | end) then |
| 34 | if view == "tree" then -- directory display (with automatic README rendering) |
| 35 | -- /repo/tree/branch/[DIRECTORY PATH] |
| 36 |
|
| 37 | local path = parsed_uri.parts |
| 38 | table.remove(path, 3) -- branch |
| 39 | table.remove(path, 2) -- "tree" |
| 40 | table.remove(path, 1) -- repo |
| 41 | if #path > 0 then |
| 42 | path = table.concat(path, "/").."/" |
| 43 | else |
| 44 | path = "" |
| 45 | end |
| 46 |
|
| 47 | content = html.tree(repo, repo_dir, branch, path) |
| 48 |
|
| 49 | elseif view == "blob" then |
| 50 | -- /repo/blob/branch/[FILE PATH] |
| 51 | elseif view == "raw" then |
| 52 | -- /repo/raw/branch/[FILE PATH] |
| 53 | elseif view == "log" then |
| 54 | -- /repo/log/branch?n=[COMMITS PER PAGE]&skip=[COMMITS TO SKIP] |
| 55 |
|
| 56 | content = html.log(repo, repo_dir, branch, 40, 0) |
| 57 |
|
| 58 | elseif view == "refs" then |
| 59 |
|
| 60 | content = html.refs(repo, repo_dir, branch) |
| 61 |
|
| 62 | elseif view == "download" then |
| 63 |
|
| 64 | content = html.download(repo, repo_dir, branch) |
| 65 |
|
| 66 | elseif view == "commit" then |
| 67 | -- /repo/commit/[COMMIT HASH] |
| 68 | end |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 |
|
| 73 | if content ~= nil then |
| 74 | ngx.say([[<style> |
| 75 | @import url('https://fonts.googleapis.com/css?family=Fira+Sans:400,400i,700,700i&display=swap'); |
| 76 | @import url('https://fonts.googleapis.com/css?family=Fira+Mono:400,400i&display=swap'); |
| 77 | *{ |
| 78 | box-sizing:border-box; |
| 79 | } |
| 80 | body{ |
| 81 | font-family:'Fira Sans', sans-serif; |
| 82 | padding-bottom:200px; |
| 83 | line-height:1.4; |
| 84 | max-width:1000px; |
| 85 | margin:20px auto; |
| 86 | } |
| 87 | body>h2{ |
| 88 | margin-top:5px; |
| 89 | margin-bottom:0; |
| 90 | } |
| 91 | h3{ |
| 92 | margin-bottom:4px; |
| 93 | } |
| 94 | td,th{ |
| 95 | padding:2px 5px; |
| 96 | border:1px solid #858585; |
| 97 | text-align:left; |
| 98 | vertical-align:top; |
| 99 | } |
| 100 | th{ |
| 101 | border:1px solid #000; |
| 102 | } |
| 103 | table.log,table.files{ |
| 104 | width:100%; |
| 105 | } |
| 106 | table{ |
| 107 | border-collapse:collapse; |
| 108 | overflow:auto; |
| 109 | font-family:'Fira Mono', monospace; |
| 110 | font-size:14px; |
| 111 | } |
| 112 | table.log td:not(:nth-child(3)){ |
| 113 | max-width:1%; |
| 114 | white-space:nowrap; |
| 115 | } |
| 116 | table.tree td:not(:nth-child(2)){ |
| 117 | max-width:1%; |
| 118 | white-space:nowrap; |
| 119 | } |
| 120 | span.q{ |
| 121 | text-decoration:underline; |
| 122 | text-decoration-style:dotted; |
| 123 | } |
| 124 | .q:hover{ |
| 125 | cursor:help; |
| 126 | } |
| 127 | tr:hover,th{ /*darker color for table head, hovered-over rows*/ |
| 128 | background-color:#dedede; |
| 129 | } |
| 130 | div.markdown{ |
| 131 | width:100%; |
| 132 | padding:20px 50px; |
| 133 | border:1px solid #858585; |
| 134 | border-radius:6px; |
| 135 | } |
| 136 | img{ |
| 137 | max-width:100%; |
| 138 | } |
| 139 | pre{ |
| 140 | background-color:#eee; |
| 141 | padding:15px; |
| 142 | overflow-x:auto; |
| 143 | border-radius:8px; |
| 144 | } |
| 145 | :not(pre)>code{ |
| 146 | background-color:#eee; |
| 147 | padding:2.5px; |
| 148 | border-radius:4px; |
| 149 | } |
| 150 | a{ |
| 151 | text-decoration:none; |
| 152 | color: #0077aa; |
| 153 | } |
| 154 | a:hover{ |
| 155 | text-decoration:underline; |
| 156 | } |
| 157 | </style>]]) |
| 158 |
|
| 159 | local arrow_left_circle = [[<img style="width:1.2em;height:1.2em;vertical-align:middle;margin-right:0.2em" src="https://joshuas3.s3.amazonaws.com/svg/arrow-left.svg"/>]] |
| 160 | ngx.say("<a style=\"margin-left:-1.35em\" href=\"/\">"..arrow_left_circle.."<span style=\"vertical-align:middle\">Index</span></a>") |
| 161 | ngx.say(content.body) |
| 162 | ngx.exit(ngx.HTTP_OK) |
| 163 | return |
| 164 | else |
| 165 | ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior |
| 166 | return |
| 167 | end |
| 168 |
|