Files
btmx/.lua/config.lua

78 lines
2.4 KiB
Lua

-- config.lua — site configuration backed by the `settings` table.
--
-- Every value has a default here; anything set through the admin
-- Settings page (stored in SQLite) overrides it. Reads go to the DB
-- each time: redbean forks per request, so a module-level cache would
-- go stale after the admin saves new settings.
local db = require "db"
local M = {}
M.DEFAULTS = {
site_title = "btmx",
site_tagline = "",
site_author = "",
-- Absolute URL of the deployed site, no trailing slash.
-- Required for the Atom feed to produce valid absolute IDs/links.
base_url = "http://localhost:8080",
posts_per_page = "10",
-- Feature flags ("1" = on). The wiki is parked by default:
-- content is preserved in the DB but routes 404 and nav hides it.
feature_wiki = "0",
-- Markdown body of the /links page. Empty = page disabled and
-- hidden from the nav. Edited at /edit/links.
links_page = "",
-- Up to four custom nav links (label + URL pairs). Empty = unused.
nav_link1_label = "", nav_link1_url = "",
nav_link2_label = "", nav_link2_url = "",
nav_link3_label = "", nav_link3_url = "",
nav_link4_label = "", nav_link4_url = "",
-- Floating radio player (bottom-left). radio_url is an m3u playlist
-- fetched server-side and offered as a channel list.
radio_enabled = "0",
radio_url = "",
}
--- The custom nav links as a list of {label, url} pairs.
function M.nav_links()
local out = {}
for n = 1, 4 do
local label = M.get("nav_link" .. n .. "_label")
local url = M.get("nav_link" .. n .. "_url")
if label ~= "" and url ~= "" then
out[#out + 1] = { label = label, url = url }
end
end
return out
end
--- Get a setting as a string, falling back to the default.
function M.get(key)
local v = db.get_setting(key)
if v == nil or v == "" then return M.DEFAULTS[key] end
return v
end
--- Get a setting as a number (falls back to default, then 0).
function M.get_number(key)
return tonumber(M.get(key)) or tonumber(M.DEFAULTS[key]) or 0
end
--- True when a feature flag is enabled.
function M.enabled(key)
return M.get(key) == "1"
end
--- Persist a setting. Empty string clears the override (default applies).
function M.set(key, value)
db.set_setting(key, value or "")
end
--- base_url with no trailing slash (for feed/link building).
function M.base_url()
return (M.get("base_url"):gsub("/+$", ""))
end
return M