Mercurial Hosting > disearch
changeset 3:43814e9f5802
add config
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 21 Oct 2023 22:52:41 -0600 |
parents | 5ae5fbce0d75 |
children | 8896ffd7b152 |
files | .hgignore src/index.html.luan src/lib/Config.luan src/lib/Db.luan src/private/tools/config.html.luan src/private/tools/config_save.txt.luan src/private/tools/lucene.html.luan src/tools/links.html src/tools/tools.css |
diffstat | 9 files changed, 128 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Sat Oct 21 21:29:56 2023 -0600 +++ b/.hgignore Sat Oct 21 22:52:41 2023 -0600 @@ -3,3 +3,4 @@ err rev.txt push-*.sh +local/
--- a/src/index.html.luan Sat Oct 21 21:29:56 2023 -0600 +++ b/src/index.html.luan Sat Oct 21 22:52:41 2023 -0600 @@ -6,6 +6,7 @@ local head = Shared.head or error() local header = Shared.header or error() local footer = Shared.footer or error() +local Config = require "site:/lib/Config.luan" return function()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/Config.luan Sat Oct 21 22:52:41 2023 -0600 @@ -0,0 +1,35 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local parse = Luan.parse or error() +local Db = require "site:/lib/Db.luan" + + +local Config = {} + +local function get() + local doc = Db.get_document("type:config") + return doc and parse(doc.config) +end + +function Config.get() + return get() or error "config not set" +end + +function Config.get_for_config() + local config = get() or {} + config.discord = config.discord or {} + config.discord.client_id = config.discord.client_id or "replace" + config.discord.client_secret = config.discord.client_secret or "replace" + return config +end + +function Config.set(text) + parse(text) -- validate + Db.run_in_transaction( function() + local doc = Db.get_document("type:config") or {type="config"} + doc.config = text + Db.save(doc) + end ) +end + +return Config
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/Db.luan Sat Oct 21 22:52:41 2023 -0600 @@ -0,0 +1,29 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local new_error = Luan.new_error or error() +local Lucene = require "luan:lucene/Lucene.luan" +local Io = require "luan:Io.luan" +local uri = Io.uri or error() +local Http = require "luan:http/Http.luan" +local Thread = require "luan:Thread.luan" +local Time = require "luan:Time.luan" +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "Db" + + +local dir = uri("site:/private/local/lucene") + +local Db = Lucene.index( dir, { + log_dir = uri("site:/private/local/lucene_log") + name = "lucene" +} ) + +function Db.not_in_transaction() + logger.error(new_error("not in transaction")) +end + +if Http.is_serving then + Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } ) +end + +return Db
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/private/tools/config.html.luan Sat Oct 21 22:52:41 2023 -0600 @@ -0,0 +1,36 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local stringify = Luan.stringify or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local Config = require "site:/lib/Config.luan" + + +return function() + local config = Config.get_for_config() + config = stringify(config) + Io.stdout = Http.response.text_writer() +%> +<!doctype html> +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <style> + @import "/tools/tools.css"; + + textarea { + width: 100%; + height: 200px; + } + </style> + </head> + <body> + <h1>Config</h1> + <form method=post action="config_save.txt"> + <p><textarea name=config><%=config%></textarea></p> + <p><input type=submit></p> + </form> + </body> +</html> +<% +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/private/tools/config_save.txt.luan Sat Oct 21 22:52:41 2023 -0600 @@ -0,0 +1,14 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local stringify = Luan.stringify or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local Config = require "site:/lib/Config.luan" + + +return function() + local config = Http.request.parameters.config or error() + Config.set(config) + Io.stdout = Http.response.text_writer() + %>saved<% +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/private/tools/lucene.html.luan Sat Oct 21 22:52:41 2023 -0600 @@ -0,0 +1,6 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local Web_search = require "luan:lucene/Web_search.luan" +local Db = require "site:/lib/Db.luan" + +return Web_search.of(Db)
--- a/src/tools/links.html Sat Oct 21 21:29:56 2023 -0600 +++ b/src/tools/links.html Sat Oct 21 22:52:41 2023 -0600 @@ -20,5 +20,7 @@ <h3>private</h3> <p><a href="/private/local/logs/">logs</a></p> + <p><a href="/private/tools/config.html">config</a></p> + <p><a href="/private/tools/lucene.html">lucene</a></p> </body> </html>