diff src/luan/modules/http/Shell_mod.luan @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents http/src/luan/modules/http/Shell_mod.luan@b620b8e1010f
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/Shell_mod.luan	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,108 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local load = Luan.load or error()
+local try = Luan.try or error()
+local Io = require "luan:Io.luan"
+local print = Io.print or error()
+local Http = require "luan:http/Http.luan"
+
+
+local M = {}
+
+local history = {}
+M.env = {}
+
+function M.respond()
+	if Http.request.parameter.clear ~= nil then
+		Http.clear_session()
+		Http.response.send_redirect(Http.request.path)  -- reload page
+		return
+	else
+		local cmd = Http.request.parameter.cmd
+		if cmd ~= nil then
+			Io.stdout = {}
+			function Io.stdout.write(...)
+				for v in Luan.values(...) do
+					history[#history+1] = v
+				end
+			end
+			print( "% "..cmd )
+			try {
+				function()
+					local line
+					try {
+						function()
+							line = load("return "..cmd,"<web_shell>",M.env)
+						end
+						catch = function(e)
+							line = load(cmd,"<web_shell>",M.env)
+						end
+					}
+					print( line() )
+				end
+				catch = function(e)
+					Io.print_to(Io.stderr,e)
+					print(e)
+				end
+			}
+		end
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<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><%
+		for _,v in ipairs(history) do
+			Io.stdout.write(v)
+		end
+		%></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
+
+Http.per_session(M.respond)
+
+return M