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 utils = require("utils/utils") |
8 | local git = require("git/git_commands") |
9 | local parse_uri = require("utils/parse_uri") |
10 |
|
11 | local parsed_uri = parse_uri() |
12 | local content |
13 |
|
14 | if parsed_uri.repo == nil then |
15 | content = require("pages/index")(yaml_config) |
16 | else -- repo found |
17 | local repo |
18 | for _,r in pairs(yaml_config) do |
19 | if parsed_uri.repo == r.name then |
20 | repo = r |
21 | break |
22 | end |
23 | end |
24 | if repo then |
25 | local repo_dir = repo.location.dev |
26 | local view = parsed_uri.parts[2] or "tree" |
27 | local branch |
28 |
|
29 | if pcall(function() -- if branch is real |
30 | branch = git.get_head(repo_dir, parsed_uri.parts[3]) -- if parts[3] is nil, defaults to "HEAD" |
31 | end) then |
32 | local res, status = pcall(function() -- effectively catch any errors, 404 if any |
33 | if view == "tree" then -- directory display (with automatic README rendering) |
34 | local path = parsed_uri.parts |
35 | table.remove(path, 3) -- branch |
36 | table.remove(path, 2) -- "tree" |
37 | table.remove(path, 1) -- repo |
38 | if #path > 0 then |
39 | path = table.concat(path, "/").."/" |
40 | else |
41 | path = "" |
42 | end |
43 |
|
44 | content = require("pages/tree")(repo, repo_dir, branch, path) |
45 | elseif view == "blob" then |
46 | local path = parsed_uri.parts |
47 | table.remove(path, 3) -- branch |
48 | table.remove(path, 2) -- "tree" |
49 | table.remove(path, 1) -- repo |
50 | if #path > 0 then |
51 | path = table.concat(path, "/") |
52 | else |
53 | path = "" |
54 | end |
55 |
|
56 | content = require("pages/blob")(repo, repo_dir, branch, path) |
57 | elseif view == "raw" then |
58 | -- /repo/raw/branch/[FILE PATH] |
59 | elseif view == "log" then |
60 | content = require("pages/log")(repo, repo_dir, branch, ngx.var.arg_n, ngx.var.arg_skip) |
61 | elseif view == "refs" then |
62 | content = require("pages/refs")(repo, repo_dir, branch) |
63 | elseif view == "download" then |
64 | content = require("pages/download")(repo, repo_dir, branch) |
65 | elseif view == "commit" then |
66 | -- /repo/commit/[COMMIT HASH] |
67 | end |
68 | end) -- pcall |
69 |
|
70 | if res ~= true then |
71 | ngx.say(res) |
72 | ngx.say(status) |
73 | ngx.exit(ngx.HTTP_NOT_FOUND) |
74 | return |
75 | end |
76 | end |
77 | end |
78 | end |
79 |
|
80 | if content ~= nil then -- TODO: HTML templates from files, static serving |
81 | ngx.say([[<style> |
82 | @import url('https://fonts.googleapis.com/css?family=Fira+Sans:400,400i,700,700i&display=swap'); |
83 | *{ |
84 | box-sizing:border-box; |
85 | } |
86 | body{ |
87 | font-family:'Fira Sans', sans-serif; |
88 | padding-bottom:200px; |
89 | line-height:1.4; |
90 | max-width:1000px; |
91 | margin:20px auto; |
92 | } |
93 | body>h2{ |
94 | margin-top:5px; |
95 | margin-bottom:0; |
96 | } |
97 | h3{ |
98 | margin-bottom:4px; |
99 | } |
100 | td,th{ |
101 | padding:2px 5px; |
102 | border:1px solid #858585; |
103 | text-align:left; |
104 | vertical-align:top; |
105 | } |
106 | th{ |
107 | border:1px solid #000; |
108 | } |
109 | table.files,table.log,table.blob-lines{ |
110 | width:100%; |
111 | max-width:100%; |
112 | } |
113 | table{ |
114 | border-collapse:collapse; |
115 | overflow:auto; |
116 | font-family: monospace; |
117 | font-size:14px; |
118 | } |
119 | table.files td:first-child{ |
120 | padding-right:calc(5px + 1em); |
121 | } |
122 | table.files td:not(:nth-child(2)), table.log td:not(:nth-child(4)){ |
123 | width:1%; |
124 | white-space:nowrap; |
125 | } |
126 | span.q{ |
127 | text-decoration:underline; |
128 | text-decoration-style:dotted; |
129 | } |
130 | .q:hover{ |
131 | cursor:help; |
132 | } |
133 | th, tr:hover{ /*darker color for table head, hovered-over rows*/ |
134 | background-color:#dedede; |
135 | } |
136 | div.markdown{ |
137 | width:100%; |
138 | padding:20px 50px; |
139 | border:1px solid #858585; |
140 | border-radius:6px; |
141 | } |
142 | img{ |
143 | max-width:100%; |
144 | } |
145 | pre{ |
146 | background-color:#eee; |
147 | padding:15px; |
148 | overflow-x:auto; |
149 | border-radius:8px; |
150 | } |
151 | :not(pre)>code{ |
152 | background-color:#eee; |
153 | padding:2.5px; |
154 | border-radius:4px; |
155 | } |
156 | |
157 | div.blob { |
158 | border:1px solid #858585; |
159 | overflow-x: auto; |
160 | } |
161 | table.blob-lines{ |
162 | font-size:1em; |
163 | max-width:100%; |
164 | line-height:1; |
165 | } |
166 | table.blob-lines tr:hover { |
167 | background-color: inherit; |
168 | } |
169 | table.blob-lines td{ |
170 | border:none; |
171 | padding:1px 5px; |
172 | } |
173 | table.blob-lines td:first-child{ |
174 | text-align: right; |
175 | padding-left:20px; |
176 | cursor: pointer; |
177 | user-select: none; |
178 | color:#858585; |
179 | max-width:1%; |
180 | white-space:nowrap; |
181 | } |
182 | table.blob-lines td:first-child:hover{ |
183 | color: #454545; |
184 | } |
185 | table.blob-lines td:nth-child(2){ |
186 | width:100%; |
187 | white-space:pre; |
188 | } |
189 | |
190 | a{ |
191 | text-decoration:none; |
192 | color: #0077aa; |
193 | } |
194 | a:hover{ |
195 | text-decoration:underline; |
196 | } |
197 | .home-banner h1 { |
198 | margin-top:40px; |
199 | margin-bottom:0; |
200 | } |
201 | .home-banner p { |
202 | margin-top:8px; |
203 | margin-bottom:30px; |
204 | } |
205 | .repo-section .name { |
206 | margin-bottom:0; |
207 | } |
208 | .repo-section h3 { |
209 | margin-top:10px; |
210 | } |
211 | .repo-section .description { |
212 | margin-top:8px; |
213 | } |
214 | .repo-section .nav { |
215 | margin-top:10px; |
216 | } |
217 | hr { |
218 | margin: 20px 0; |
219 | } |
220 | </style>]]) |
221 |
|
222 | if parsed_uri.repo then |
223 | 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"/>]] |
224 | ngx.say("<a style=\"margin-left:-1.35em\" href=\"/\">"..arrow_left_circle.."<span style=\"vertical-align:middle\">Index</span></a>") |
225 | end |
226 | ngx.say(content:build()) |
227 | ngx.exit(ngx.HTTP_OK) |
228 | return |
229 | else |
230 | ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior |
231 | return |
232 | end |
233 |
|