src/main.lua

Thu, 22 Jun 2023 21:29:31 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Jun 2023 21:29:31 +0100
changeset 14
afcc217db356
parent 0
6279a7d40ae7
permissions
-rw-r--r--

main: Tweaks for prosody trunk (use unbound, fix HTTPS client)

0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local LUA_WEB_APP_FRAMEWORK = os.getenv("LUA_WEB_APP_FRAMEWORK");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 if LUA_WEB_APP_FRAMEWORK then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 package.path = ("%s/?.lua;%s"):format(LUA_WEB_APP_FRAMEWORK, package.path);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 package.cpath = ("%s/?.so;%s"):format(LUA_WEB_APP_FRAMEWORK, package.cpath);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
14
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
8 package.preload["net.adns"] = function ()
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
9 return (require "net.unbound");
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
10 end;
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local ssl = require "ssl";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local server = require "net.server_epoll";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local envload = require"util.envload";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local logger = require "util.logger";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 package.loaded["net.server"] = server; -- COMPAT this module from Prosody can't be loaded outside of prosody
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local log = logger.init("main");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local config = {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 loglevel = "debug";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 };
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local logfile = io.stderr;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local function log2file(source, level, message, ...)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 logfile:write(os.date("!%Y-%m-%dT%H:%M:%SZ"), "\t", source, "\t", level, "\t", message:format(...), "\n");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local function init(config_file)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if type(config_file) ~= "table" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 setmetatable(config, { __index = { ENV = os.getenv } });
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local ok, err = envload.envloadfile(config_file or "./config.lua", config);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if ok then ok, err = pcall(ok); end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 setmetatable(config, nil);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if not ok then log2file("config", "error", "Parse failed: %s", err or "Unknown error"); end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 config = config_file;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local events = require"util.events".new();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 -- Set up logging to stdout
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 logger.reset();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local logsink = log2file;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if config.logfile then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if config.logfile == "*stdout" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 logfile = io.stdout;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 elseif config.logfile == "*stderr" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 logfile = io.stderr;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 logfile = assert(io.open(config.logfile, "a"));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if logfile then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 logfile:setvbuf("line");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 for _, level in ipairs{"error", "warn", "info", "debug"} do
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 logger.add_level_sink(level, logsink);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if config.loglevel == level then break; end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 log("debug", "Logging ready");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
14
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
64 -- Configure HTTP client
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
65 require "net.http".default.options.sslctx = server.tls_builder(".")
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
66 :apply({mode="client", protocol="sslv23", options="no_sslv2",capath="/etc/ssl/certs"})
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
67 :build();
afcc217db356 main: Tweaks for prosody trunk (use unbound, fix HTTPS client)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
68
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 require "http".init(config, events);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 -- Load optional extensions specified in the config
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 for _, ext in ipairs(config.extensions or {}) do
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 require("extensions."..ext).init(config, events);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local function run()
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 server.loop();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if arg then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 init(...);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 run();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 return {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 init = init,
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 run = run,
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 };
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91

mercurial