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)

local LUA_WEB_APP_FRAMEWORK = os.getenv("LUA_WEB_APP_FRAMEWORK");

if LUA_WEB_APP_FRAMEWORK then
	package.path  = ("%s/?.lua;%s"):format(LUA_WEB_APP_FRAMEWORK, package.path);
	package.cpath =  ("%s/?.so;%s"):format(LUA_WEB_APP_FRAMEWORK, package.cpath);
end

package.preload["net.adns"] = function ()
	return (require "net.unbound");
end;
local ssl = require "ssl";
local server = require "net.server_epoll";
local envload = require"util.envload";
local logger = require "util.logger";

package.loaded["net.server"] = server; -- COMPAT this module from Prosody can't be loaded outside of prosody

local log = logger.init("main");

local config = {
	loglevel = "debug";
};

local logfile = io.stderr;

local function log2file(source, level, message, ...)
	logfile:write(os.date("!%Y-%m-%dT%H:%M:%SZ"), "\t", source, "\t", level, "\t", message:format(...), "\n");
end

local function init(config_file)
	if type(config_file) ~= "table" then
		setmetatable(config, { __index = { ENV = os.getenv } });
		local ok, err = envload.envloadfile(config_file or "./config.lua", config);
		if ok then ok, err = pcall(ok); end
		setmetatable(config, nil);
		if not ok then log2file("config", "error", "Parse failed: %s", err or "Unknown error"); end
	else
		config = config_file;
	end

	local events = require"util.events".new();

	-- Set up logging to stdout
	logger.reset();
	local logsink = log2file;
	if config.logfile then
		if config.logfile == "*stdout" then
			logfile = io.stdout;
		elseif config.logfile == "*stderr" then
			logfile = io.stderr;
		else
			logfile = assert(io.open(config.logfile, "a"));
		end
	end
	if logfile then
		logfile:setvbuf("line");
		for _, level in ipairs{"error", "warn", "info", "debug"} do
			logger.add_level_sink(level, logsink);
			if config.loglevel == level then break; end
		end
	end
	log("debug", "Logging ready");

	-- Configure HTTP client
	require "net.http".default.options.sslctx = server.tls_builder(".")
		:apply({mode="client", protocol="sslv23", options="no_sslv2",capath="/etc/ssl/certs"})
		:build();

	require "http".init(config, events);

	-- Load optional extensions specified in the config
	for _, ext in ipairs(config.extensions or {}) do
		require("extensions."..ext).init(config, events);
	end
end

local function run()
	server.loop();
end

if arg then
	init(...);
	run();
else
	return {
		init = init,
		run = run,
	};
end

mercurial