changeset 4:8896ffd7b152

start login
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 22 Oct 2023 23:53:59 -0600
parents 43814e9f5802
children aa1920665f98
files .hgignore src/error_log.js.luan src/lib/Shared.luan src/login.red.luan src/site.js src/tools/cookies.html.luan src/tools/links.html
diffstat 7 files changed, 164 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
diff -r 43814e9f5802 -r 8896ffd7b152 .hgignore
--- a/.hgignore	Sat Oct 21 22:52:41 2023 -0600
+++ b/.hgignore	Sun Oct 22 23:53:59 2023 -0600
@@ -4,3 +4,4 @@
 rev.txt
 push-*.sh
 local/
+mine/
diff -r 43814e9f5802 -r 8896ffd7b152 src/error_log.js.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/error_log.js.luan	Sun Oct 22 23:53:59 2023 -0600
@@ -0,0 +1,19 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Http = require "luan:http/Http.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "error_log.js"
+
+
+local function priority(err)
+	return "error"
+end
+
+return function()
+	local err = Http.request.parameters.err
+	if err == nil then
+		return  -- stupid bots
+	end
+	local call = priority(err)
+	logger[call](trim(err).."\n"..trim(Http.request.raw_head).."\n")
+end
diff -r 43814e9f5802 -r 8896ffd7b152 src/lib/Shared.luan
--- a/src/lib/Shared.luan	Sat Oct 21 22:52:41 2023 -0600
+++ b/src/lib/Shared.luan	Sun Oct 22 23:53:59 2023 -0600
@@ -1,18 +1,25 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local Time = require "luan:Time.luan"
+local Http = require "luan:http/Http.luan"
 
 
 local Shared = {}
 
 local started = Time.now()
 
+local function get_user()
+	return Http.request.cookies.user
+end
+Shared.get_user = get_user
+
 function Shared.head()
 %>
 		<meta name="viewport" content="width=device-width, initial-scale=1">
 		<style>
 			@import "/site.css?s=<%=started%>";
 		</style>
+		<script src="/site.js?s=<%=started%>"></script>
 <%
 end
 
@@ -20,7 +27,11 @@
 %>
 		<div header>
 			<h1><a href="/">Disearch</a></h1>
-			<span>login</span>
+<%	if get_user() == nil then %>
+			<a href="login.red">login</a>
+<%	else %>
+			<a href="javascript:logout()">logout</a>
+<%	end %>
 		</div>
 		<hr>
 <%
diff -r 43814e9f5802 -r 8896ffd7b152 src/login.red.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/login.red.luan	Sun Oct 22 23:53:59 2023 -0600
@@ -0,0 +1,9 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Http = require "luan:http/Http.luan"
+
+
+return function()
+	Http.response.set_persistent_cookie("user","whatever")
+	Http.response.send_redirect "/"
+end
diff -r 43814e9f5802 -r 8896ffd7b152 src/site.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/site.js	Sun Oct 22 23:53:59 2023 -0600
@@ -0,0 +1,49 @@
+'use strict';
+
+function ajax(url,postData,context) {
+	let request = new XMLHttpRequest();
+	let method = postData ? 'POST' : 'GET';
+	request.open( method, url );
+	if( postData )
+		request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
+	request.onload = function() {
+		if( request.status !== 200 ) {
+			console.log( 'ajax failed: ' + request.status );
+			if( request.responseText )
+				document.write('<pre>'+request.responseText+'</pre>');
+			return;
+		}
+		try {
+			eval( request.responseText );
+		} catch(e) {
+			console.log( request.responseText );
+			window.err = '\najax-url = ' + url;
+			throw e;
+		}
+	};
+	request.send(postData);
+}
+
+window.onerror = function(msg, url, line, col, error) {
+	if (!url)
+		return;
+	let err = msg;
+	err += '\nurl = ' + url;
+	if (url != window.location)
+		err += '\npage = ' + window.location;
+	err += '\nline = '+line;
+	if (col)
+		err += '\ncolumn = ' + col;
+	if (error && error.stack)
+		err += '\nstack = ' + error.stack;
+	if( window.err ) {
+		err += window.err;
+		window.err = null;
+	}
+	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
+};
+
+function logout() {
+	document.cookie = 'user=; Max-Age=0; path=/;';
+	location = '/';
+}
diff -r 43814e9f5802 -r 8896ffd7b152 src/tools/cookies.html.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tools/cookies.html.luan	Sun Oct 22 23:53:59 2023 -0600
@@ -0,0 +1,62 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local pairs = Luan.pairs or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "cookies.html"
+
+
+return function()
+	local name = Http.request.parameters.name
+	if name ~= nil then
+		local value = Http.request.parameters.value
+		local persistent = Http.request.parameters.persistent
+		if #value == 0 then
+			Http.response.remove_cookie(name)
+		elseif persistent ~= nil then
+			Http.response.set_persistent_cookie(name,value)
+		else
+			Http.response.set_cookie(name,value)
+		end
+		Http.response.send_redirect "cookies.html"
+		return
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+		<meta name="viewport" content="width=device-width, initial-scale=1">
+		<style>
+			form {
+				margin-bottom: 16px;
+			}
+		</style>
+	</head>
+	<body>
+		<h2>Cookies Tool</h2>
+<%
+	for name, value in pairs(Http.request.cookies) do
+		name = html_encode(name)
+		value = html_encode(value)
+%>
+		<form>
+			<input name=name value="<%=name%>" type=hidden>
+			<%=name%> = <input name=value value="<%=value%>" size=100>
+			<label><input type=checkbox name=persistent>persistent</label>
+		</form>
+<%	end %>
+		<form>
+			<input name=name>
+			= <input name=value size=100>
+			<label><input type=checkbox name=persistent>persistent</label>
+			<input type=submit>
+		</form>
+	</body>
+</html>
+<%
+end
diff -r 43814e9f5802 -r 8896ffd7b152 src/tools/links.html
--- a/src/tools/links.html	Sat Oct 21 22:52:41 2023 -0600
+++ b/src/tools/links.html	Sun Oct 22 23:53:59 2023 -0600
@@ -8,19 +8,24 @@
 	</head>
 	<body>
 		<h1>Links</h1>
+
+		<h3>public</h3>
 		<p><a href="/">home</a></p>
-		<p><a href="https://discord.gg/tCwTJDSatu">Discord</a></p>
-		<p><a href="https://hg.reactionary.software/repo/disearch/">source</a></p>
+		<p><a href="cookies.html">cookies</a></p>
+
+		<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>
+
+		<h3>global</h3>
 		<p>
 			links:
 			<a href="http://test.disearch.org/tools/links.html">test</a>
 			- <a href="http://local.disearch.org:8080/tools/links.html">local</a>
 		</p>
+		<p><a href="https://discord.gg/tCwTJDSatu">Discord</a></p>
+		<p><a href="https://hg.reactionary.software/repo/disearch/">source</a></p>
 		<p><a href="https://discord.com/developers/docs/intro">Discord API</a></p>
-
-		<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>