smr/src/lua/init.lua

146 lines
3.3 KiB
Lua

--[[
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
local et = require("etlua")
local sql = require("lsqlite3")
local zlib = require("zlib")
--stub for detouring
function configure(...) end
--smr code
local cache = require("cache")
local pages = require("pages")
local util = require("util")
local config = require("config")
local db = require("db")
--Pages
local endpoint_names = {
read = {"get","post"},
preview = {"post"},
index = {"get"},
paste = {"get","post"},
download = {"get"},
login = {"get","post"},
logout = {"get"},
edit = {"get","post"},
claim = {"get","post"},
search = {"get"},
archive = {"get"},
api = {"get"},
delete = {"post"},
bio = {"get","post"},
}
local endpoints = {}
for name, methods in pairs(endpoint_names) do
for _,method in pairs(methods) do
local epn = string.format("%s_%s",name,method)
endpoints[epn] = require("endpoints." .. epn)
end
end
print("Hello from init.lua")
local oldconfigure = configure
function configure(...)
--Test that compression works. For some reason, the zlib library
--fails if this is done as a one-liner
local msg = "test message"
local one = zlib.compress(msg)
local two = zlib.decompress(one)
assert(two == msg, "zlib not working as expected")
oldconfigure(...)
end
print("Created configure function")
local http_methods = {"GET","POST"}
local http_m_rev = {}
for funcname, spec in pairs({
home = {
GET = endpoints.index_get,
},
claim = {
GET = endpoints.claim_get,
POST = endpoints.claim_post,
},
paste = {
GET = endpionts.paste_get,
POST = endpoints.paste_post,
},
read = {
GET = endpoints.read_get,
POST = endpoints.read_post
},
login = {
GET = endpoints.login_get,
POST = endpoints.login_post,
},
logout = {
GET = endpoints.logout_get,
},
edit = {
GET = endpoints.edit_get,
POST = endpoints.edit_post,
},
delete = {
POST = endpoints.delete_post,
},
edit_bio = {
GET = endpoints.bio_edit_get,
POST = endpoints.bio_post,
},
download = {
GET = endpoints.download_get,
},
preview = {
POST = endpoints.preview_post,
},
search = {
GET = endpoints.search_get,
},
archive = {
GET = endpoints.archive_get,
},
api = {
GET = endpoints.api_get,
POST = endpoints.api_post,
},
}) do
assert(_G[funcname] == nil, "Tried to overwrite an endpoint, please define endpoints exactly once")
-- TODO: Fill this out
for k,v in pairs(http_methods) do
http_m_rev[v] = true
end
for k,v in pairs(spec) do
assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'")
end
_G[funcname] = function(req)
local method = http_method_text(req)
if spec[method] == nil then
log(LOG_NOTICE,string.format("Endpoint %s called with http method %s, but no such route defined.", funcname, method))
end
spec[method](req)
end
log(LOG_INFO,string.format("Associateing endpoint %q", funcname))
end
function teardown()
print("Exiting...")
if db then
db.close()
end
if cache then
cache.close()
end
print("Finished lua teardown")
end
print("Done with init.lua")