view src/luan/modules/http/tools/Shell_mod.luan @ 1177:26533dd4cd09

add Shell_mod.env
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Feb 2018 19:30:27 -0700
parents 3ef883468fd0
children 5dbb552075ff
line wrap: on
line source

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_mod"


local Shell_mod = {}

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_mod.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_mod.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()
%>
<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_mod