smr/src/lua/init.lua

130 lines
3.3 KiB
Lua
Raw Normal View History

2020-12-23 07:02:02 +01:00
--[[
Contains the code entrypoint, this is the first (and only) file run from kore.
It registers a bunch of global functions that get called from kore when users
visit particular pages. See src/smr.c for the names of the public functions.
See conf/smr.conf for the data that can be access in each function
]]
print("Really fast print from init.lua")
--Luarocks libraries
2020-05-16 01:10:11 +02:00
local et = require("etlua")
local sql = require("lsqlite3")
local zlib = require("zlib")
2023-02-20 03:39:42 +01:00
local api = require("hooks")
--stubs for detouring
2020-12-21 05:22:22 +01:00
function configure(...) end
--smr code
require("global")
local cache = require("cache")
local pages = require("pages")
local util = require("util")
local config = require("config")
local db = require("db")
2020-05-16 01:10:11 +02:00
print("Hello from init.lua")
2020-12-21 05:22:22 +01:00
local oldconfigure = configure
function configure(...)
--Test that compression works. For some reason, the zlib library
--fails if this is done as a one-liner
2020-05-16 01:10:11 +02:00
local msg = "test message"
local one = zlib.compress(msg)
local two = zlib.decompress(one)
assert(two == msg, "zlib not working as expected")
2020-12-21 05:22:22 +01:00
oldconfigure(...)
2020-05-16 01:10:11 +02:00
end
print("Created configure function")
-- TODO: Fill this out
2022-09-20 00:07:35 +02:00
local http_methods = {"GET","POST"}
local http_m_rev = {}
for k,v in pairs(http_methods) do
http_m_rev[v] = true
end
2022-09-20 00:07:35 +02:00
--Endpoints, all this stuff gets required here.
2022-09-20 00:07:35 +02:00
for funcname, spec in pairs({
home = {
GET = require("endpoints.index_get"),
2022-09-20 00:07:35 +02:00
},
claim = {
GET = require("endpoints.claim_get"),
POST = require("endpoints.claim_post"),
2022-09-20 00:07:35 +02:00
},
paste = {
GET = require("endpoints.paste_get"),
POST = require("endpoints.paste_post"),
2022-09-20 00:07:35 +02:00
},
read = {
GET = require("endpoints.read_get"),
POST = require("endpoints.read_post"),
2022-09-20 00:07:35 +02:00
},
login = {
GET = require("endpoints.login_get"),
POST = require("endpoints.login_post"),
2022-09-20 00:07:35 +02:00
},
logout = {
GET = require("endpoints.logout_get"),
2022-09-20 00:07:35 +02:00
},
edit = {
GET = require("endpoints.edit_get"),
POST = require("endpoints.edit_post"),
2022-09-20 00:07:35 +02:00
},
delete = {
POST = require("endpoints.delete_post"),
2022-09-20 00:07:35 +02:00
},
edit_bio = {
GET = require("endpoints.bio_get"),
POST = require("endpoints.bio_post"),
2022-09-20 00:07:35 +02:00
},
download = {
GET = require("endpoints.download_get"),
2022-09-20 00:07:35 +02:00
},
preview = {
POST = require("endpoints.preview_post"),
2022-09-20 00:07:35 +02:00
},
search = {
GET = require("endpoints.search_get"),
2022-09-20 00:07:35 +02:00
},
archive = {
GET = require("endpoints.archive_get"),
2022-09-20 00:07:35 +02:00
},
api = {
GET = require("endpoints.api_get"),
2022-09-20 00:07:35 +02:00
},
}) do
assert(_G[funcname] == nil, "Tried to overwrite an endpoint, please define endpoints exactly once")
for k,v in pairs(spec) do
assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'")
2022-11-21 01:32:49 +01:00
assert(type(v) == "function", "Endpoint %s %s must be a function, but was a %s",funcname, k, type(v))
2020-05-16 01:10:11 +02:00
end
2022-09-20 00:07:35 +02:00
_G[funcname] = function(req)
local method = http_method_text(req)
if spec[method] == nil then
2022-11-21 01:32:49 +01:00
log(LOG_WARNING,string.format("Endpoint %s called with http method %s, but no such route defined.", funcname, method))
else
log(LOG_DEBUG,string.format("Endpoint %s called with method %s",funcname,method))
2022-09-20 00:07:35 +02:00
end
2023-02-20 03:39:42 +01:00
api.pre_request(req)
2022-09-20 00:07:35 +02:00
spec[method](req)
2023-02-20 03:39:42 +01:00
api.post_request(req)
end
2022-09-20 00:07:35 +02:00
log(LOG_INFO,string.format("Associateing endpoint %q", funcname))
2020-05-16 01:10:11 +02:00
end
function teardown()
print("Exiting...")
if db then
db.close()
2020-05-16 01:10:11 +02:00
end
if cache then
cache.close()
2020-05-16 01:10:11 +02:00
end
2023-02-20 03:39:42 +01:00
api.worker_shutdown()
2020-05-16 01:10:11 +02:00
print("Finished lua teardown")
end
2023-02-20 03:39:42 +01:00
api.worker_init()
2020-05-16 01:10:11 +02:00
print("Done with init.lua")