smr/src/lua/init.lua

149 lines
3.2 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")
2020-12-23 07:02:02 +01:00
--stub for detouring
2020-12-21 05:22:22 +01:00
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
2020-12-21 05:22:22 +01:00
local endpoint_names = {
read = {"get","post"},
preview = {"post"},
index = {"get"},
paste = {"get","post"},
download = {"get"},
login = {"get","post"},
logout = {"get"},
2020-12-21 05:22:22 +01:00
edit = {"get","post"},
claim = {"get","post"},
search = {"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
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")
function home(req)
local method = http_method_text(req)
2020-12-21 05:22:22 +01:00
if method == "GET" then
endpoints.index_get(req)
end
2020-05-16 01:10:11 +02:00
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.
2020-05-16 01:10:11 +02:00
function claim(req)
local method = http_method_text(req)
if method == "GET" then
2020-12-21 05:22:22 +01:00
endpoints.claim_get(req)
2020-05-16 01:10:11 +02:00
elseif method == "POST" then
2020-12-21 05:22:22 +01:00
endpoints.claim_post(req)
2020-05-16 01:10:11 +02:00
end
end
--Create a new paste on the site
2020-05-16 01:10:11 +02:00
function paste(req)
local method = http_method_text(req)
if method == "GET" then
2020-12-21 05:22:22 +01:00
endpoints.paste_get(req)
2020-05-16 01:10:11 +02:00
elseif method == "POST" then
2020-12-21 05:22:22 +01:00
endpoints.paste_post(req)
end
end
2020-05-16 01:10:11 +02:00
function read(req)
local method = http_method_text(req)
if method == "GET" then
2020-12-21 05:22:22 +01:00
endpoints.read_get(req)
elseif method == "POST" then
2020-12-21 05:22:22 +01:00
endpoints.read_post(req)
2020-05-16 01:10:11 +02:00
end
end
function login(req)
local method = http_method_text(req)
if method == "GET" then
2020-12-21 05:22:22 +01:00
endpoints.login_get(req)
2020-05-16 01:10:11 +02:00
elseif method == "POST" then
2020-12-21 05:22:22 +01:00
endpoints.login_post(req)
2020-05-16 01:10:11 +02:00
end
end
function logout(req)
endpoints.logout_get(req)
end
--Edit a story
2020-05-16 01:10:11 +02:00
function edit(req)
local method = http_method_text(req)
if method == "GET" then
2020-12-21 05:22:22 +01:00
endpoints.edit_get(req)
2020-05-16 01:10:11 +02:00
elseif method == "POST" then
2020-12-21 05:22:22 +01:00
endpoints.edit_post(req)
2020-05-16 01:10:11 +02:00
end
end
--TODO
2020-05-16 01:10:11 +02:00
function edit_bio()
error("Not yet implemented")
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
print("Finished lua teardown")
end
function download(req)
2020-12-21 05:22:22 +01:00
endpoints.download_get(req)
end
function preview(req)
2020-12-21 05:22:22 +01:00
endpoints.preview_post(req)
end
function search(req)
2020-12-21 05:22:22 +01:00
endpoints.search_get(req)
end
2020-05-16 01:10:11 +02:00
print("Done with init.lua")