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}}
|
|
|
|
]]
|
2020-08-13 19:59:33 +02:00
|
|
|
print("Really fast print from init.lua")
|
|
|
|
|
2020-12-20 09:16:23 +01:00
|
|
|
--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")
|
2020-08-13 19:59:33 +02:00
|
|
|
|
2022-11-20 00:58:56 +01:00
|
|
|
--stubs for detouring
|
2020-12-21 05:22:22 +01:00
|
|
|
function configure(...) end
|
|
|
|
|
2020-12-20 09:16:23 +01:00
|
|
|
--smr code
|
2022-11-20 00:58:56 +01:00
|
|
|
require("global")
|
2020-12-20 09:16:23 +01:00
|
|
|
local cache = require("cache")
|
2023-06-20 03:11:42 +02:00
|
|
|
require("pages")
|
2020-12-20 09:16:23 +01:00
|
|
|
local db = require("db")
|
2020-12-22 00:32:29 +01:00
|
|
|
|
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(...)
|
2020-12-20 09:16:23 +01:00
|
|
|
--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")
|
|
|
|
|
2022-11-20 00:58:56 +01:00
|
|
|
-- TODO: Fill this out
|
2022-09-20 00:07:35 +02:00
|
|
|
local http_methods = {"GET","POST"}
|
|
|
|
local http_m_rev = {}
|
2023-06-20 03:11:42 +02:00
|
|
|
for _,v in pairs(http_methods) do
|
2022-11-20 00:58:56 +01:00
|
|
|
http_m_rev[v] = true
|
|
|
|
end
|
2022-09-20 00:07:35 +02:00
|
|
|
|
2022-11-20 00:58:56 +01:00
|
|
|
--Endpoints, all this stuff gets required here.
|
2022-09-20 00:07:35 +02:00
|
|
|
for funcname, spec in pairs({
|
|
|
|
home = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.index_get"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
claim = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.claim_get"),
|
|
|
|
POST = require("endpoints.claim_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
paste = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.paste_get"),
|
|
|
|
POST = require("endpoints.paste_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
read = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.read_get"),
|
|
|
|
POST = require("endpoints.read_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
login = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.login_get"),
|
|
|
|
POST = require("endpoints.login_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
logout = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.logout_get"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
edit = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.edit_get"),
|
|
|
|
POST = require("endpoints.edit_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
delete = {
|
2022-11-20 00:58:56 +01:00
|
|
|
POST = require("endpoints.delete_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
edit_bio = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.bio_get"),
|
|
|
|
POST = require("endpoints.bio_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
download = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.download_get"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
preview = {
|
2022-11-20 00:58:56 +01:00
|
|
|
POST = require("endpoints.preview_post"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
search = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.search_get"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
archive = {
|
2022-11-20 00:58:56 +01:00
|
|
|
GET = require("endpoints.archive_get"),
|
2022-09-20 00:07:35 +02:00
|
|
|
},
|
|
|
|
api = {
|
2022-11-20 00:58:56 +01:00
|
|
|
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)
|
2022-09-03 01:24:39 +02:00
|
|
|
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
|
2020-12-20 09:16:23 +01:00
|
|
|
db.close()
|
2020-05-16 01:10:11 +02:00
|
|
|
end
|
|
|
|
if cache then
|
2020-12-20 09:16:23 +01:00
|
|
|
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")
|