Index

resty-gitweb / adbeb10

A git web interface for Lua/OpenResty (you're on it right now!)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
113 Dec 2020 16:16e49d46eInitial commit (imported, read description)Josh Stockin12320G

Blob @ resty-gitweb / app.lua

text/plain5730 bytesdownload raw
1-- app.lua
2-- Entry point for git HTTP site implementation
3
4-- Copyright (c) 2020 Joshua 'joshuas3' Stockin
5-- <https://joshstock.in>
6
7local utils = require("utils/utils")
8local git = require("git/git_commands")
9local parse_uri = require("utils/parse_uri")
10
11local parsed_uri = parse_uri()
12local content
13
14if parsed_uri.repo == nil then
15 content = require("pages/index")(yaml_config)
16else -- 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
78end
79
80if content ~= nil then -- TODO: HTML templates from files, static serving
81ngx.say([[<style>
82@import url('https://fonts.googleapis.com/css?family=Fira+Sans:400,400i,700,700i&display=swap');
83*{
84box-sizing:border-box;
85}
86body{
87font-family:'Fira Sans', sans-serif;
88padding-bottom:200px;
89line-height:1.4;
90max-width:1000px;
91margin:20px auto;
92}
93body>h2{
94 margin-top:5px;
95 margin-bottom:0;
96}
97h3{
98margin-bottom:4px;
99}
100td,th{
101padding:2px 5px;
102border:1px solid #858585;
103text-align:left;
104vertical-align:top;
105}
106th{
107border:1px solid #000;
108}
109table.files,table.log,table.blob-lines{
110 width:100%;
111 max-width:100%;
112}
113table{
114 border-collapse:collapse;
115 overflow:auto;
116 font-family: monospace;
117 font-size:14px;
118}
119table.files td:first-child{
120 padding-right:calc(5px + 1em);
121}
122table.files td:not(:nth-child(2)), table.log td:not(:nth-child(4)){
123 width:1%;
124 white-space:nowrap;
125}
126span.q{
127text-decoration:underline;
128text-decoration-style:dotted;
129}
130.q:hover{
131cursor:help;
132}
133th, tr:hover{ /*darker color for table head, hovered-over rows*/
134 background-color:#dedede;
135}
136div.markdown{
137width:100%;
138padding:20px 50px;
139border:1px solid #858585;
140border-radius:6px;
141}
142img{
143max-width:100%;
144}
145pre{
146background-color:#eee;
147padding:15px;
148overflow-x:auto;
149border-radius:8px;
150}
151:not(pre)>code{
152background-color:#eee;
153padding:2.5px;
154border-radius:4px;
155}
156
157div.blob {
158 border:1px solid #858585;
159 overflow-x: auto;
160}
161table.blob-lines{
162 font-size:1em;
163 max-width:100%;
164 line-height:1;
165}
166table.blob-lines tr:hover {
167 background-color: inherit;
168}
169table.blob-lines td{
170 border:none;
171 padding:1px 5px;
172}
173table.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}
182table.blob-lines td:first-child:hover{
183 color: #454545;
184}
185table.blob-lines td:nth-child(2){
186 width:100%;
187 white-space:pre;
188}
189
190a{
191text-decoration:none;
192color: #0077aa;
193}
194a: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}
217hr {
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
229else
230 ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior
231 return
232end
233