From 3431daee0be12529c6118f1b657341173675e7e2 Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Mon, 19 Sep 2022 22:07:35 +0000 Subject: [PATCH] Refactoring --- src/lua/endpoints/claim_post.lua | 4 + src/lua/init.lua | 164 +++++++++++++------------------ 2 files changed, 71 insertions(+), 97 deletions(-) diff --git a/src/lua/endpoints/claim_post.lua b/src/lua/endpoints/claim_post.lua index 71b2624..a83df10 100644 --- a/src/lua/endpoints/claim_post.lua +++ b/src/lua/endpoints/claim_post.lua @@ -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(...) diff --git a/src/lua/init.lua b/src/lua/init.lua index 1cc5abe..78de6fb 100644 --- a/src/lua/init.lua +++ b/src/lua/init.lua @@ -59,80 +59,76 @@ function configure(...) end print("Created configure function") -function home(req) - local method = http_method_text(req) - if method == "GET" then - endpoints.index_get(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 -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) + for k,v in pairs(spec) do + assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'") 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") + _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() @@ -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")