changeset 2:fc2383eb48a9

set config
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 16 Jun 2022 20:52:24 -0600
parents 7c64173643c8
children fc3ee39d7764
files .hgignore src/config.html.luan src/lib/Config.luan src/lib/Db.luan src/private/admin/config.html.luan src/private/tools/lucene.html.luan
diffstat 6 files changed, 97 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Jun 15 20:59:13 2022 -0600
+++ b/.hgignore	Thu Jun 16 20:52:24 2022 -0600
@@ -1,3 +1,4 @@
 syntax: glob
 
 err
+local/
--- a/src/config.html.luan	Wed Jun 15 20:59:13 2022 -0600
+++ b/src/config.html.luan	Thu Jun 16 20:52:24 2022 -0600
@@ -32,7 +32,9 @@
 		<div content>
 			<h1>Configuration</h2>
 
-			<textarea><%= html_encode(Config.text) %></textarea>
+			<p>Configuration in <a href="http://www.luan.software/">Luan</a></p>
+			<p><textarea><%= html_encode(Config.text) %></textarea></p>
+			<p><a href="/private/admin/config.html">Edit configuration</a></p>
 		</div>
 <%		footer() %>
 	</body>
--- a/src/lib/Config.luan	Wed Jun 15 20:59:13 2022 -0600
+++ b/src/lib/Config.luan	Thu Jun 16 20:52:24 2022 -0600
@@ -1,5 +1,7 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local Db = require "site:/lib/Db.luan"
+
 
 local Config = {}
 
@@ -9,6 +11,13 @@
 Forum.title = "Your Forum"
 ]]
 
-Config.text = Config.default_text
+local doc = Db.get_document("type:config")
+Config.text = doc and doc.config or Config.default_text
+
+function Config.set(text)
+	local doc = Db.get_document("type:config") or {type="config"}
+	doc.config = text
+	Db.save(doc)
+end
 
 return Config
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Db.luan	Thu Jun 16 20:52:24 2022 -0600
@@ -0,0 +1,12 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Lucene = require "luan:lucene/Lucene.luan"
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+
+
+local dir = uri("site:/private/local/lucene")
+
+local Db = Lucene.index( dir, {} )
+
+return Db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/admin/config.html.luan	Thu Jun 16 20:52:24 2022 -0600
@@ -0,0 +1,65 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local reset_luan = Http.reset_luan or error()
+local test_as_init = Http.test_as_init or error()
+local Run = require "luan:http/tools/Run.luan"
+local Config = require "site:/lib/Config.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "admin/Config.html"
+
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	if Http.request.method == "POST" then
+		local config = Http.request.parameters.config or error()
+		try
+			test_as_init(config,"config")
+		catch e
+			Run.print_error(e,config)
+			return
+		end
+		Config.set(config)
+		reset_luan()
+		%>Updated<%
+		return
+	end
+%>
+<!doctype html>
+<html>
+	<head>
+		<title>FreedIt - Configure Forum</title>
+		<style>
+			@import "/site.css";
+
+			textarea {
+				width: 90%;
+				height: 20em;
+			}
+		</style>
+		<script>
+			function restoreDefault() {
+				document.querySelector('textarea').value = <%=json_string(Config.default_text,{compressed=true})%>;
+			}
+		</script>
+	</head>
+	<body>
+		<h1>Configure Forum</h1>
+
+		<form method=post>
+			<p>
+				Configuration in <a href="http://www.luan.software/">Luan</a>
+				- <a href="javascript:restoreDefault()">restore default</a>
+			</p>
+			<p><textarea name=config autofocus><%= html_encode(Config.text) %></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/lucene.html.luan	Thu Jun 16 20:52:24 2022 -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)