Refactoring

This commit is contained in:
Robin Malley 2022-09-19 22:07:35 +00:00
parent 223cfb9e46
commit 3431daee0b
2 changed files with 71 additions and 97 deletions

View File

@ -9,6 +9,10 @@ local config = require("config")
local stmnt_author_create
--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.
local oldconfigure = configure
function configure(...)

View File

@ -59,80 +59,76 @@ function configure(...)
end
print("Created configure function")
function home(req)
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 method == "GET" then
endpoints.index_get(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
--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
function delete(req)
endpoints.delete_post(req)
end
--TODO
function edit_bio()
local method = http_method_text(req)
if method == "GET" then
endpoints.bio_edit_get(req)
elseif method == "POST" then
error("Not yet implemented")
end
log(LOG_INFO,string.format("Associateing endpoint %q", funcname))
end
function teardown()
@ -146,30 +142,4 @@ function teardown()
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")