smr/src/lua/init.lua

165 lines
3.6 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"},
}
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")
function home(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.index_get(req)
end
end
--We prevent people from changing their password file, this way we don't really
--need to worry about logged in accounts being hijacked if someone gets at the
--database. The attacker can still paste & edit from the logged in account for
--a while, but whatever.
function claim(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.claim_get(req)
elseif method == "POST" then
endpoints.claim_post(req)
end
end
--Create a new paste on the site
function paste(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.paste_get(req)
elseif method == "POST" then
endpoints.paste_post(req)
end
end
function read(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.read_get(req)
elseif method == "POST" then
endpoints.read_post(req)
end
end
function login(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.login_get(req)
elseif method == "POST" then
endpoints.login_post(req)
end
end
function logout(req)
endpoints.logout_get(req)
end
--Edit a story
function edit(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.edit_get(req)
elseif method == "POST" then
endpoints.edit_post(req)
end
end
--TODO
function edit_bio()
error("Not yet implemented")
end
function teardown()
print("Exiting...")
if db then
db.close()
end
if cache then
cache.close()
end
print("Finished lua teardown")
end
function download(req)
endpoints.download_get(req)
end
function preview(req)
endpoints.preview_post(req)
end
function search(req)
endpoints.search_get(req)
end
function archive(req)
print("archive method:",http_method_text(req))
endpoints.archive_get(req)
end
function api(req)
local method = http_method_text(req)
if method == "GET" then
endpoints.api_get(req)
elseif method == "POST" then
endpoints.api_post(req)
end
end
print("Done with init.lua")