2023-07-20 11:41:49 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
move port/title/log_level from DB config to CLI args
diff --git a/cogit/cfg.lua b/cogit/cfg.lua index a5a0c7c..53f7358 100644 --- a/cogit/cfg.lua +++ b/cogit/cfg.lua @@ -6,11 +6,8 @@ local schema = [[ CREATE TABLE IF NOT EXISTS Config ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, - title TEXT NOT NULL, - port INTEGER NOT NULL, pg_size INTEGER NOT NULL, - ses_age INTEGER NOT NULL, - log_lvl INTEGER NOT NULL + ses_age INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS User ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -30,8 +27,8 @@ function CFG:add_defaults() local salt = hash.get_salt() local hash = hash.hash_pass(pass, salt) self.db:execute[[ - INSERT INTO Config(id, name, title, port, pg_size, ses_age, log_lvl) VALUES - (1, "default", "cogit", 8080, 20, 72*60*60, 2); + INSERT INTO Config(id, name, pg_size, ses_age) VALUES + (1, "default", 20, 72*60*60); ]] self.db:execute([[ INSERT INTO User(id, nick, name, salt, hash, is_admin) VALUES @@ -56,11 +53,8 @@ function CFG:get_config(col) return self.db:execute("SELECT * FROM Config WHERE id = ?;", self.cid)[1][col] end -function CFG:title() return self:get_config("title") end -function CFG:port() return self:get_config("port") end function CFG:pg_size() return self:get_config("pg_size") end function CFG:ses_age() return self:get_config("ses_age") end -function CFG:log_lvl() return self:get_config("log_lvl") end function CFG:get_user(nick) return self.db:execute("SELECT * FROM User WHERE nick = ?;", nick)[1] diff --git a/cogit/cogit.lua b/cogit/cogit.lua index 146cb73..0be9f1e 100644 --- a/cogit/cogit.lua +++ b/cogit/cogit.lua @@ -82,9 +82,8 @@ end function Cogit:run() self:init() - local port = self.cfg:port() - self:log(LOG_INFO, "server running on port "..port) - self.app:run(port) + self:log(LOG_INFO, "server running on port "..self.port) + self.app:run(self.port) end function Cogit:scan() @@ -92,10 +91,8 @@ function Cogit:scan() self.groups = scan.scanrepos(self.path) end -local LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG = 0, 1, 2, 3 -local level_str = {"ERROR", "WARN", "INFO", "DEBUG"} - function Cogit:log(level, msg) + local level_str = {"ERROR", "WARN", "INFO", "DEBUG"} if self.log_level >= level then io.stderr:write(("[%s] %s\n"):format(level_str[level+1], msg)) end @@ -114,7 +111,7 @@ function Cogit:get_user(cookies) end function Cogit:routes() - local title = self.cfg:title() + local title = self.title local limit = self.cfg:pg_size() return { {"GET", "/?", @@ -268,13 +265,18 @@ function Cogit:routes() end}, } end -local function new_cogit(path) - local self = setmetatable({}, Cogit) - self.path = path - self.sessions = {} - self.initialized = false - self.cfg = cfg.new_cfg(path.."/conf.db") - self.log_level = self.cfg:log_lvl() +local function new_cogit(path, port, title, log_level) + local log_levels = {ERROR=0, WARN=1, INFO=2, DEBUG=3} + local self = { + path=path or ".", + port=tonumber(port) or 8080, + title=title or "cogit", + log_level=log_levels[log_level or "INFO"], + sessions={}, + initialized=false, + } + self = setmetatable(self, Cogit) + self.cfg = cfg.new_cfg(self.path.."/conf.db") self:init() self:scan() self.app = lud.app.new_app(self:routes()) diff --git a/main.lua b/main.lua index 94ad8f6..419a9e8 100644 --- a/main.lua +++ b/main.lua @@ -1,3 +1,3 @@ local cogit = require "cogit.cogit" -cogit.new_cogit(arg[1]):run() +cogit.new_cogit(unpack(arg)):run()