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 | tr:hover,th{ /*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:12px; |
163 | max-width:100%; |
164 | } |
165 | table.blob-lines td{ |
166 | border:none; |
167 | } |
168 | table.blob-lines td:first-child{ |
169 | text-align: right; |
170 | padding-left:20px; |
171 | user-select: none; |
172 | color:#858585; |
173 | max-width:1%; |
174 | white-space:nowrap; |
175 | } |
176 | table.blob-lines td:nth-child(2){ |
177 | width:100%; |
178 | white-space:pre; |
179 | } |
180 | |
181 | a{ |
182 | text-decoration:none; |
183 | color: #0077aa; |
184 | } |
185 | a:hover{ |
186 | text-decoration:underline; |
187 | } |
188 | .home-banner h1 { |
189 | margin-top:40px; |
190 | margin-bottom:0; |
191 | } |
192 | .home-banner p { |
193 | margin-top:8px; |
194 | margin-bottom:30px; |
195 | } |
196 | .repo-section .name { |
197 | margin-bottom:0; |
198 | } |
199 | .repo-section h3 { |
200 | margin-top:10px; |
201 | } |
202 | .repo-section .description { |
203 | margin-top:8px; |
204 | } |
205 | .repo-section .nav { |
206 | margin-top:10px; |
207 | } |
208 | hr { |
209 | margin: 20px 0; |
210 | } |
211 | </style>]]) |
212 |
|
213 | if parsed_uri.repo then |
214 | 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"/>]] |
215 | ngx.say("<a style=\"margin-left:-1.35em\" href=\"/\">"..arrow_left_circle.."<span style=\"vertical-align:middle\">Index</span></a>") |
216 | end |
217 | ngx.say(content:build()) |
218 | ngx.exit(ngx.HTTP_OK) |
219 | return |
220 | else |
221 | ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior |
222 | return |
223 | end |
224 |
|