smr/src/lua/init.lua

156 lines
4.0 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
]]
2023-06-10 04:06:42 +02:00
--[[ md
@name lua
# Lua namespace
By default, smr will run init.lua defined by smr, and then addons in order,
see {{lua/addon}} for information on how smr loads addons. You can use any
of the modules that ship with smr by including them, and then calling the
functions defined in that module.
For example, the module {{lua/db}} holdes a reference to the sqlite3 database
that smr uses for data storage. If you addon needs to set up a table and
prepare sql statements for an api endpoint, you might set it up like this:
local db = require("db")
local oldconfigure = configure -- Hold a refrence to configure()
function configure(...) -- Detour the configure function
db.sqlassert(db.conn:exec([=[
CREATE TABLE IF NOT EXISTS foo (
id INTEGER AUTOINCREMENT PRIMARY KEY
value TEXT
);
]=]))
oldconfigure(...)
end
Be sure to always {{doc/appendix/detourin}}
]]
print("Really fast print from init.lua")
--Luarocks libraries
2020-05-16 01:10:11 +02:00
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")
require("pages")
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 _,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")