diff src/private/tools/backup.html.luan @ 0:dfc36e7ed22c

init
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 12 May 2022 13:51:59 +0400
parents
children 028e74c8889d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/tools/backup.html.luan	Thu May 12 13:51:59 2022 +0400
@@ -0,0 +1,114 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local pairs = Luan.pairs or error()
+local ipairs = Luan.ipairs or error()
+local stringify = Luan.stringify or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.private_header or error()
+local new_password = Shared.new_password or error()
+local config = Shared.config or error()
+local get_raw_config = Shared.get_raw_config or error()
+local save_raw_config = Shared.save_raw_config or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "private-users.html"
+
+
+local function response(content)
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Mercurial Backup</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Backup</h1>
+			<%=content%>
+		</div>
+	</body>
+</html>
+<%
+end
+
+local function posted()
+	local raw_config = get_raw_config()
+	local change = Http.request.parameters.change ~= nil
+	local password = config.users.backup
+	if password == nil or change then
+		password = new_password()
+		raw_config.users.backup = password
+	end
+	for repo_name, repo in pairs(raw_config.repos) do
+		local users = {}
+		if Http.request.parameters["repo_"..repo_name] ~= nil then
+			users[#users+1] = "backup"
+		end
+		for _, user in ipairs(repo.users) do
+			if user ~= "backup" then
+				users[#users+1] = user
+			end
+		end
+		repo.users = users
+	end
+	if config.private ~= nil and not config.private.backup then
+		raw_config.private[#raw_config.private+1] = "backup"
+	end
+	save_raw_config(raw_config)
+	return nil
+end
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	local error_msg = ""
+	if Http.request.method == "POST" then
+		local error_msg = posted()
+		if error_msg == nil then
+			response([[<p>Backup updated</p>]])
+		else
+			response([[<p error>]]..error_msg..[[</p>]])
+		end
+		return
+	end
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Mercurial Backup</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Backup</h1>
+			<form method=post>
+				<p>
+					User <b>backup</b> password: <%=config.users.backup%>
+					<label prompt clickable><input type=checkbox name=change> change backup password</label>
+				</p>
+				<p>
+					Repos to backup:
+				</p>
+<%
+	for _, repo in pairs(config.repos) do
+		local name = repo.name
+		local checked = repo.users.backup and "checked" or ""
+%>
+				<p>
+					<label clickable><input type=checkbox name="repo_<%=name%>" <%=checked%> > <%=name%></label>
+					- <a href="/repo/<%=name%>/">repo</a>
+				</p>
+<%	end %>
+				<p>
+					<input type=submit value="Update Backup">
+				</p>
+			</form>
+		</div>
+	</body>
+</html>
+<%
+end