1 | -- resty-gitweb@init.lua |
2 | -- Preloads scripts and config for OpenResty workers. MUST be called by init_by_lua_file. |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://git.joshstock.in/resty-gitweb> |
6 | -- This software is licensed under the MIT License. |
7 |
|
8 |
|
9 | -- Check for RESTY_GITWEB_ENABLE in environment variables |
10 | if os.getenv("RESTY_GITWEB_ENABLE") == nil then |
11 | ngx.log(ngx.ERR, "RESTY_GITWEB_ENABLE not found in environment variables; are you missing an `env` directive?") |
12 | os.exit(1) |
13 | end |
14 |
|
15 | -- In production mode? |
16 | PRODUCTION = os.getenv("RESTY_GITWEB_ENV") == "PROD" |
17 |
|
18 | -- Get config path |
19 | local resty_gitweb_config = os.getenv("RESTY_GITWEB_CONFIG") |
20 | if resty_gitweb_config == nil then |
21 | ngx.log(ngx.ERR, "RESTY_GITWEB_CONFIG not found in environment variables; are you missing an `env` directive?") |
22 | os.exit(1) |
23 | elseif resty_gitweb_config == "" then |
24 | ngx.log(ngx.ERR, "RESTY_GITWEB_CONFIG is empty") |
25 | os.exit(1) |
26 | end |
27 |
|
28 | -- Require external modules |
29 | local ffi = require "ffi" |
30 | local lyaml = require "lyaml" |
31 | local puremagic = require "puremagic" |
32 |
|
33 | -- Load YAML configuration |
34 | local yaml_config_file = io.open(resty_gitweb_config) |
35 | CONFIG = lyaml.load(yaml_config_file:read("*a")) |
36 | yaml_config_file:close() |
37 |
|
38 | -- Load libgit2 into FFI and initialize |
39 | ffi.include = function(header) |
40 | local p = io.popen("echo '#include <"..header..">' | gcc -E -") |
41 | local c = {} |
42 | while true do |
43 | local line = p:read() |
44 | if line then |
45 | if not line:match("^#") then |
46 | table.insert(c, line) |
47 | end |
48 | else |
49 | break |
50 | end |
51 | end |
52 | p:close() |
53 | ffi.cdef(table.concat(c, "\n")) |
54 | end |
55 |
|
56 | ffi.include("git2.h") |
57 | git2 = ffi.load("git2") |
58 | git2.git_libgit2_init() |
59 |
|
60 | -- Require internal modules |
61 | local git = require "git/git" |
62 | local git_git2_error = require "git/git2_error" |
63 | local git_find_rev = require "git/find_rev" |
64 | local git_read_blob = require "git/read_blob" |
65 | local git_repo = require "git/repo" |
66 |
|
67 | --local pages = require "pages/pages" |
68 | local pages_blob = require "pages/blob" |
69 | local pages_commit = require "pages/commit" |
70 | local pages_download = require "pages/download" |
71 | local pages_index = require "pages/index" |
72 | local pages_log = require "pages/log" |
73 | local pages_row = require "pages/raw" |
74 | local pages_refs = require "pages/refs" |
75 | local pages_tree = require "pages/tree" |
76 |
|
77 | --local utils = require "utils/utils" |
78 | local builder = require "utils/builder" |
79 | local nav = require "utils/nav" |
80 | local parse_uri = require "utils/parse_uri" |
81 | local tabulate = require "utils/tabulate" |
82 | local utils = require "utils/utils" |
83 |
|