--[[ 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 ]] --[[ md @name lua # Lua namespace By default, smr will run init.lua defined by smr, and then addons in order, see {{lua/addon}} for information on how smr loads addons. You can use any of the modules that ship with smr by including them, and then calling the functions defined in that module. For example, the module {{lua/db}} holdes a reference to the sqlite3 database that smr uses for data storage. If you addon needs to set up a table and prepare sql statements for an api endpoint, you might set it up like this: local db = require("db") local oldconfigure = configure -- Hold a refrence to configure() function configure(...) -- Detour the configure function db.sqlassert(db.conn:exec([=[ CREATE TABLE IF NOT EXISTS foo ( id INTEGER AUTOINCREMENT PRIMARY KEY value TEXT ); ]=])) oldconfigure(...) end Be sure to always {{doc/appendix/detourin}} ]] print("Really fast print from init.lua") --Luarocks libraries local zlib = require("zlib") local api = require("hooks") --stubs for detouring function configure(...) end --smr code require("global") local cache = require("cache") require("pages") local db = require("db") 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") -- TODO: Fill this out local http_methods = {"GET","POST"} local http_m_rev = {} for _,v in pairs(http_methods) do http_m_rev[v] = true end --Endpoints, all this stuff gets required here. for funcname, spec in pairs({ home = { GET = require("endpoints.index_get"), }, claim = { GET = require("endpoints.claim_get"), POST = require("endpoints.claim_post"), }, paste = { GET = require("endpoints.paste_get"), POST = require("endpoints.paste_post"), }, read = { GET = require("endpoints.read_get"), POST = require("endpoints.read_post"), }, login = { GET = require("endpoints.login_get"), POST = require("endpoints.login_post"), }, logout = { GET = require("endpoints.logout_get"), }, edit = { GET = require("endpoints.edit_get"), POST = require("endpoints.edit_post"), }, delete = { POST = require("endpoints.delete_post"), }, edit_bio = { GET = require("endpoints.bio_get"), POST = require("endpoints.bio_post"), }, download = { GET = require("endpoints.download_get"), }, preview = { POST = require("endpoints.preview_post"), }, search = { GET = require("endpoints.search_get"), }, archive = { GET = require("endpoints.archive_get"), }, api = { GET = require("endpoints.api_get"), }, }) do assert(_G[funcname] == nil, "Tried to overwrite an endpoint, please define endpoints exactly once") for k,v in pairs(spec) do assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'") assert(type(v) == "function", "Endpoint %s %s must be a function, but was a %s",funcname, k, type(v)) end _G[funcname] = function(req) local method = http_method_text(req) if spec[method] == nil then log(LOG_WARNING,string.format("Endpoint %s called with http method %s, but no such route defined.", funcname, method)) else log(LOG_DEBUG,string.format("Endpoint %s called with method %s",funcname,method)) end api.pre_request(req) spec[method](req) api.post_request(req) end log(LOG_INFO,string.format("Associateing endpoint %q", funcname)) end function teardown() print("Exiting...") if db then db.close() end if cache then cache.close() end api.worker_shutdown() print("Finished lua teardown") end api.worker_init() print("Done with init.lua")