-- app.lua -- Entry point for git HTTP site implementation -- Copyright (c) 2020 Joshua 'joshuas3' Stockin -- local utils = require("utils/utils") local git = require("git/git_commands") local request = require("utils/parse_uri") local parsed_uri = request.parse_uri() local content if parsed_uri.repo == nil then content = require("pages/index")(yaml_config) else -- repo found local repo for _,r in pairs(yaml_config) do if parsed_uri.repo == r.name then repo = r break end end if repo then local repo_dir = repo.location.dev local view = parsed_uri.parts[2] or "tree" local branch if pcall(function() -- if branch is real branch = git.get_head(repo_dir, parsed_uri.parts[3]) -- if parts[3] is nil, defaults to "HEAD" end) then if pcall(function() -- effectively catch any errors, 404 if any if view == "tree" then -- directory display (with automatic README rendering) local path = parsed_uri.parts table.remove(path, 3) -- branch table.remove(path, 2) -- "tree" table.remove(path, 1) -- repo if #path > 0 then path = table.concat(path, "/").."/" else path = "" end content = require("pages/tree")(repo, repo_dir, branch, path) elseif view == "blob" then -- /repo/blob/branch/[FILE PATH] elseif view == "raw" then -- /repo/raw/branch/[FILE PATH] elseif view == "log" then content = require("pages/log")(repo, repo_dir, branch, ngx.var.arg_n, ngx.var.arg_skip) elseif view == "refs" then content = require("pages/refs")(repo, repo_dir, branch) elseif view == "download" then content = require("pages/download")(repo, repo_dir, branch) elseif view == "commit" then -- /repo/commit/[COMMIT HASH] end end) ~= true then ngx.exit(ngx.HTTP_NOT_FOUND) return end end end end if content ~= nil then -- TODO: HTML templates from files, static serving ngx.say([[]]) if parsed_uri.repo then local arrow_left_circle = [[]] ngx.say(""..arrow_left_circle.."Index") end ngx.say(content:build()) ngx.exit(ngx.HTTP_OK) return else ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior return end