2020-12-20 09:16:23 +01:00
|
|
|
|
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 et = require("etlua")
|
|
|
|
local sql = require("lsqlite3")
|
|
|
|
local zlib = require("zlib")
|
2020-08-13 19:59:33 +02:00
|
|
|
|
2020-12-21 05:22:22 +01:00
|
|
|
--stubs for overloading
|
|
|
|
function configure(...) end
|
|
|
|
|
2020-12-20 09:16:23 +01:00
|
|
|
--smr code
|
|
|
|
local cache = require("cache")
|
|
|
|
local pages = require("pages")
|
|
|
|
local util = require("util")
|
|
|
|
local config = require("config")
|
|
|
|
local db = require("db")
|
2020-12-22 00:32:29 +01:00
|
|
|
|
|
|
|
--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"},
|
|
|
|
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-10-11 01:28:39 +02: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")
|
|
|
|
|
|
|
|
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
|
2020-05-17 18:05:00 +02:00
|
|
|
--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
|
|
|
|
|
2020-12-22 00:32:29 +01:00
|
|
|
--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)
|
2020-08-13 19:59:33 +02:00
|
|
|
end
|
2020-05-17 18:05:00 +02:00
|
|
|
end
|
|
|
|
|
2020-05-16 01:10:11 +02:00
|
|
|
function read(req)
|
2020-08-13 19:59:33 +02:00
|
|
|
local method = http_method_text(req)
|
|
|
|
if method == "GET" then
|
2020-12-21 05:22:22 +01:00
|
|
|
endpoints.read_get(req)
|
2020-08-13 19:59:33 +02:00
|
|
|
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
|
|
|
|
|
2020-05-17 18:05:00 +02:00
|
|
|
--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
|
|
|
|
|
2020-05-17 18:05:00 +02:00
|
|
|
--TODO
|
2020-05-16 01:10:11 +02:00
|
|
|
function edit_bio()
|
2020-12-22 00:32:29 +01:00
|
|
|
error("Not yet implemented")
|
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
|
|
|
|
print("Finished lua teardown")
|
|
|
|
end
|
|
|
|
|
2020-08-13 19:59:33 +02:00
|
|
|
function download(req)
|
2020-12-21 05:22:22 +01:00
|
|
|
endpoints.download_get(req)
|
2020-08-13 19:59:33 +02:00
|
|
|
end
|
|
|
|
|
2020-08-24 23:38:24 +02:00
|
|
|
function preview(req)
|
2020-12-21 05:22:22 +01:00
|
|
|
endpoints.preview_post(req)
|
2020-08-24 23:38:24 +02:00
|
|
|
end
|
|
|
|
|
2020-10-11 01:28:39 +02:00
|
|
|
function search(req)
|
2020-12-21 05:22:22 +01:00
|
|
|
endpoints.search_get(req)
|
2020-10-11 01:28:39 +02:00
|
|
|
end
|
|
|
|
|
2020-05-16 01:10:11 +02:00
|
|
|
print("Done with init.lua")
|