diff src/luan/modules/http/tools/Shell.luan @ 1218:a50803fde972

http/tools cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Mar 2018 16:24:59 -0600
parents src/luan/modules/http/tools/Shell_mod.luan@5dbb552075ff
children 2de84f128be3
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/tools/Shell.luan	Tue Mar 20 16:24:59 2018 -0600
@@ -0,0 +1,143 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local load = Luan.load or error()
+local to_string = Luan.to_string or error()
+local try = Luan.try or error()
+local String = require "luan:String.luan"
+local concat = String.concat or error()
+local Time = require "luan:Time.luan"
+local Thread = require "luan:Thread.luan"
+local Io = require "luan:Io.luan"
+local print = Io.print or error()
+local Http = require "luan:http/Http.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Shell"
+
+
+local Shell = {}
+
+local forever = Time.period{days=1000000}
+local count = 0
+local new_session = Thread.global_callable("shell.new_session",forever,{next=function()
+	count = count + 1
+	return to_string(count)
+end}).next
+
+local history = ""
+local env = {}
+Shell.env = env
+
+local fns = {}
+
+function fns.history()
+	return history
+end
+
+function fns.run(cmd)
+	Io.stdout = {}
+	Io.stdout.write = function(...)
+		history = concat(history,...)
+	end
+	print( "% "..cmd )
+	try {
+		function()
+			local line
+			try {
+				function()
+					line = load("return "..cmd,"<web_shell>",env)
+				end
+				catch = function(e)
+					line = load(cmd,"<web_shell>",env)
+				end
+			}
+			print( line() )
+		end
+		catch = function(e)
+			Io.print_to(Io.stderr,e)
+			print(e)
+		end
+	}
+end
+
+local timeout = Time.period{hours=10}
+
+local function get_session(session_id)
+	return Thread.global_callable("shell.session"..session_id,timeout,fns)
+end
+
+local function remove_session(session_id)
+	return Thread.remove_global_callable("shell.session"..session_id)
+end
+
+function Shell.respond()
+	local session_id = Http.request.cookies.session
+	if session_id == nil then
+		session_id = new_session()
+		Http.response.set_cookie("session",session_id)
+	end
+	local session = get_session(session_id)
+
+	if Http.request.parameters.clear ~= nil then
+		remove_session(session_id)
+		Http.response.send_redirect(Http.request.path)  -- reload page
+		return
+	else
+		local cmd = Http.request.parameters.cmd
+		if cmd ~= nil then
+			session.run(cmd)
+		end
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+		<title>Luan Shell</title>
+		<style>
+			body {
+				font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+				margin: 2em 5% 0 5%;
+			}
+			pre {
+				font: inherit;
+			}
+			input[type="text"] {
+				font: inherit;
+				padding: .5em .8em;
+				border-radius: 8px;
+				border-style: groove;
+			}
+			input[type="text"]:focus {
+				border-color: #66afe9;
+				outline: none;
+			}
+			input[type="submit"] {
+				color: white;
+				background: #337ab7;
+				border-color: #337ab7;
+				font: inherit;
+				padding: .5em;
+				border-radius: 4px;
+			}
+			input[type="submit"]:hover {
+				background: #236aa7 !important;
+			}
+		</style>
+	</head>
+	<body>
+		<h2>Luan Shell</h2>
+		<p>This is a command shell.  Enter commands below.</p>
+		<pre><%= session.history() %></pre>
+		<form name='form0' method='post'>
+			% <input type="text" name='cmd' size="80" autofocus>
+			<input type="submit" value="run">
+			<input type="submit" name="clear" value="clear">
+		</form>
+	</body>
+</html>
+<%
+end
+
+return Shell