changeset 347:612a283b3d14

improve luan/web file names and add serve.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 Apr 2015 07:17:40 -0600
parents dc21bd260690
children 6fd016d35ec1
files web/src/luan/modules/web/Server.luan web/src/luan/modules/web/Web_server.luan web/src/luan/modules/web/run.luan web/src/luan/modules/web/serve.luan web/src/luan/modules/web/shell.luan web/src/luan/modules/web/web_run.luan web/src/luan/modules/web/web_shell.luan
diffstat 7 files changed, 246 insertions(+), 237 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/Server.luan	Sun Apr 12 07:17:40 2015 -0600
@@ -0,0 +1,96 @@
+require "luan:String"
+local Io = require "luan:Io"
+local Package = require "luan:Package"
+local Http = require "luan:web/Http"
+
+java()
+local Server = require "java:org.eclipse.jetty.server.Server"
+local NCSARequestLog = require "java:org.eclipse.jetty.server.NCSARequestLog"
+local DefaultHandler = require "java:org.eclipse.jetty.server.handler.DefaultHandler"
+local HandlerList = require "java:org.eclipse.jetty.server.handler.HandlerList"
+local HandlerCollection = require "java:org.eclipse.jetty.server.handler.HandlerCollection"
+local ResourceHandler = require "java:org.eclipse.jetty.server.handler.ResourceHandler"
+local RequestLogHandler = require "java:org.eclipse.jetty.server.handler.RequestLogHandler"
+local ContextHandler = require "java:org.eclipse.jetty.server.handler.ContextHandler"
+local GzipHandler = require "java:org.eclipse.jetty.server.handler.GzipHandler"
+local HandlerWrapper = require "java:org.eclipse.jetty.server.handler.HandlerWrapper"
+local SessionHandler = require "java:org.eclipse.jetty.server.session.SessionHandler"
+local AuthenticationHandler = require "java:luan.modules.web.AuthenticationHandler"
+
+
+port = 8080
+
+private_password = "password"
+
+welcome_file = "index.html"
+
+
+authentication_handler = AuthenticationHandler.new("/private/")
+
+luan_handler = Http.new_luan_handler()
+
+resource_handler = ResourceHandler.new()
+resource_handler.setDirectoriesListed(true)
+
+handlers = HandlerList.new()
+handlers.setHandlers { authentication_handler, luan_handler, resource_handler }
+
+function add_folder(context,dir)
+	local rh = ResourceHandler.new()
+	rh.setResourceBase(dir)
+	rh.setDirectoriesListed(true)
+	local ch = ContextHandler.new(context)
+	ch.setHandler(rh)
+	handlers.addHandler(ch)
+	return rh
+end
+
+handler_wrapper = HandlerWrapper.new()
+handler_wrapper.setHandler(handlers)
+
+function zip()
+	local h = GzipHandler.new()
+	h.setHandler(handler_wrapper.getHandler())
+	handler_wrapper.setHandler(h)
+end
+
+log = NCSARequestLog.new()
+log.setExtended(false)
+log_handler = RequestLogHandler.new()
+log_handler.setRequestLog(log)
+
+function set_log_file(file_name)
+	log.setFilename(file_name)
+end
+
+local hc = HandlerCollection.new()
+hc.setHandlers { SessionHandler.new(), handler_wrapper, DefaultHandler.new(), log_handler }
+
+
+function init(dir)
+	dir = dir.gsub("/$","")  -- remove trailing '/' if any
+	Http.dir = dir
+	function Io.schemes.site(path)
+		return Io.Uri( dir..path )
+	end
+	authentication_handler.setPassword(private_password)
+	local base = dir
+	if base.match("^classpath:") ~= nil then
+		base = dir.."#"..welcome_file.."#"..welcome_file..".luan"
+	end
+	resource_handler.setResourceBase(Io.Uri(base).to_string())
+	resource_handler.setWelcomeFiles {welcome_file}
+	luan_handler.setWelcomeFile(welcome_file)
+	server = Server.new(port)
+	server.setHandler(hc)
+	Package.load("site:/init")
+end
+
+function start()
+	server.start()
+end
+
+function serve(dir)
+	init(dir)
+	start()
+end
--- a/web/src/luan/modules/web/Web_server.luan	Sun Apr 12 06:54:14 2015 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,96 +0,0 @@
-require "luan:String"
-local Io = require "luan:Io"
-local Package = require "luan:Package"
-local Http = require "luan:web/Http"
-
-java()
-local Server = require "java:org.eclipse.jetty.server.Server"
-local NCSARequestLog = require "java:org.eclipse.jetty.server.NCSARequestLog"
-local DefaultHandler = require "java:org.eclipse.jetty.server.handler.DefaultHandler"
-local HandlerList = require "java:org.eclipse.jetty.server.handler.HandlerList"
-local HandlerCollection = require "java:org.eclipse.jetty.server.handler.HandlerCollection"
-local ResourceHandler = require "java:org.eclipse.jetty.server.handler.ResourceHandler"
-local RequestLogHandler = require "java:org.eclipse.jetty.server.handler.RequestLogHandler"
-local ContextHandler = require "java:org.eclipse.jetty.server.handler.ContextHandler"
-local GzipHandler = require "java:org.eclipse.jetty.server.handler.GzipHandler"
-local HandlerWrapper = require "java:org.eclipse.jetty.server.handler.HandlerWrapper"
-local SessionHandler = require "java:org.eclipse.jetty.server.session.SessionHandler"
-local AuthenticationHandler = require "java:luan.modules.web.AuthenticationHandler"
-
-
-port = 8080
-
-private_password = "password"
-
-welcome_file = "index.html"
-
-
-authentication_handler = AuthenticationHandler.new("/private/")
-
-luan_handler = Http.new_luan_handler()
-
-resource_handler = ResourceHandler.new()
-resource_handler.setDirectoriesListed(true)
-
-handlers = HandlerList.new()
-handlers.setHandlers { authentication_handler, luan_handler, resource_handler }
-
-function add_folder(context,dir)
-	local rh = ResourceHandler.new()
-	rh.setResourceBase(dir)
-	rh.setDirectoriesListed(true)
-	local ch = ContextHandler.new(context)
-	ch.setHandler(rh)
-	handlers.addHandler(ch)
-	return rh
-end
-
-handler_wrapper = HandlerWrapper.new()
-handler_wrapper.setHandler(handlers)
-
-function zip()
-	local h = GzipHandler.new()
-	h.setHandler(handler_wrapper.getHandler())
-	handler_wrapper.setHandler(h)
-end
-
-log = NCSARequestLog.new()
-log.setExtended(false)
-log_handler = RequestLogHandler.new()
-log_handler.setRequestLog(log)
-
-function set_log_file(file_name)
-	log.setFilename(file_name)
-end
-
-local hc = HandlerCollection.new()
-hc.setHandlers { SessionHandler.new(), handler_wrapper, DefaultHandler.new(), log_handler }
-
-
-function init(dir)
-	dir = dir.gsub("/$","")  -- remove trailing '/' if any
-	Http.dir = dir
-	function Io.schemes.site(path)
-		return Io.Uri( dir..path )
-	end
-	authentication_handler.setPassword(private_password)
-	local base = dir
-	if base.match("^classpath:") ~= nil then
-		base = dir.."#"..welcome_file.."#"..welcome_file..".luan"
-	end
-	resource_handler.setResourceBase(Io.Uri(base).to_string())
-	resource_handler.setWelcomeFiles {welcome_file}
-	luan_handler.setWelcomeFile(welcome_file)
-	server = Server.new(port)
-	server.setHandler(hc)
-	Package.load("site:/init")
-end
-
-function start()
-	server.start()
-end
-
-function serve(dir)
-	init(dir)
-	start()
-end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/run.luan	Sun Apr 12 07:17:40 2015 -0600
@@ -0,0 +1,76 @@
+local Luan = require "luan:Luan"
+local load = Luan.load
+local try = Luan.try
+local Io = require "luan:Io"
+local print = Io.print
+local Http = require "luan:web/Http"
+local String = require "luan:String"
+local Html = require "luan:Html"
+
+local function lines(s)
+	local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
+	return function()
+		local m1, m2 = matcher()
+		return m1 or m2
+	end
+end
+
+local function print_with_line_numbers(s)
+	i = 1
+	for line in lines(s) do
+		print(i,line)
+		i = i + 1
+	end
+end
+
+local function form()
+	Html.simply_html_page{
+		head = function() %>
+			<title>Run Luan Code</title>
+<%		end;
+		body = function() %>
+			<center margin-top=10>
+				<h3>Run Luan Code</h3>
+			</center>
+			<form name="form0" method="post">
+				<input type="hidden" name="content_type" value="text/plain" />
+				<center>
+					<textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
+				</center>
+				<center margin-top=5>
+					<input type="submit" value="Execute Luan Code" textcolor="white" bgcolor="#337ab7"/>
+				</center>
+			</form>
+<%		end;
+	}
+end
+
+function service()
+	Io.stdout = Http.response.text_writer()
+	local code = Http.request.parameters.code
+	if code == nil then
+		form()
+		return
+	end
+	local content_type = Http.request.parameters.content_type
+	if content_type ~= nil then
+		Http.response.content_type = content_type
+	end
+	local env = {
+		request = Http.request;
+		response = Http.response;
+	}
+	try {
+		function()
+			local run = load(code,"<web_run>",env)
+			run()
+		end;
+		catch = function(e)
+			Http.response.content_type = "text/plain"
+			print(e)
+			print()
+			print()
+			print_with_line_numbers(code)
+		end;
+	}
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/serve.luan	Sun Apr 12 07:17:40 2015 -0600
@@ -0,0 +1,9 @@
+local Io = require "luan:Io"
+local Server = require "luan:web/Server"
+
+if #{...} ~= 1 then
+	Io.stderr.write "usage: luan luan:web/serve dir-URI\n"
+	return
+end
+
+Server.serve(...)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/shell.luan	Sun Apr 12 07:17:40 2015 -0600
@@ -0,0 +1,65 @@
+local Luan = require "luan:Luan"
+local ipairs = Luan.ipairs
+local load = Luan.load
+local try = Luan.try
+local Io = require "luan:Io"
+local print = Io.print
+local Debug = require "luan:Debug"
+local Http = require "luan:web/Http"
+local Html = require "luan:Html"
+
+per_session = true
+
+local history = {}
+local env = {}
+
+function service()
+	if Http.request.parameters.clear ~= nil then
+		history = {}
+	else
+		local cmd = Http.request.parameters.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 = load(cmd,"<web_shell>",env,true)
+					Debug.print_if_something( line() )
+				end;
+				catch = function(e)
+					Io.print_to(Io.stderr,e)
+					print(e)
+				end;
+			}
+		end
+	end
+
+	Io.stdout = Http.response.text_writer()
+	Html.simply_html_page{
+		head = function() %>
+			<title>Luan Shell</title>
+<%		end;
+		body = function() %>
+			<div container>
+				<h3>Luan Shell</h3>
+				<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 name='cmd' size="80" autofocus>
+					<input type="submit" value="run" textcolor="white" bgcolor="#337ab7">
+					<input type="submit" name="clear" value="clear" textcolor="white" bgcolor="#337ab7">
+				</form>
+			</div>
+<%		end;
+	}
+
+end
--- a/web/src/luan/modules/web/web_run.luan	Sun Apr 12 06:54:14 2015 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-local Luan = require "luan:Luan"
-local load = Luan.load
-local try = Luan.try
-local Io = require "luan:Io"
-local print = Io.print
-local Http = require "luan:web/Http"
-local String = require "luan:String"
-local Html = require "luan:Html"
-
-local function lines(s)
-	local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
-	return function()
-		local m1, m2 = matcher()
-		return m1 or m2
-	end
-end
-
-local function print_with_line_numbers(s)
-	i = 1
-	for line in lines(s) do
-		print(i,line)
-		i = i + 1
-	end
-end
-
-local function form()
-	Html.simply_html_page{
-		head = function() %>
-			<title>Run Luan Code</title>
-<%		end;
-		body = function() %>
-			<center margin-top=10>
-				<h3>Run Luan Code</h3>
-			</center>
-			<form name="form0" method="post">
-				<input type="hidden" name="content_type" value="text/plain" />
-				<center>
-					<textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
-				</center>
-				<center margin-top=5>
-					<input type="submit" value="Execute Luan Code" textcolor="white" bgcolor="#337ab7"/>
-				</center>
-			</form>
-<%		end;
-	}
-end
-
-function service()
-	Io.stdout = Http.response.text_writer()
-	local code = Http.request.parameters.code
-	if code == nil then
-		form()
-		return
-	end
-	local content_type = Http.request.parameters.content_type
-	if content_type ~= nil then
-		Http.response.content_type = content_type
-	end
-	local env = {
-		request = Http.request;
-		response = Http.response;
-	}
-	try {
-		function()
-			local run = load(code,"<web_run>",env)
-			run()
-		end;
-		catch = function(e)
-			Http.response.content_type = "text/plain"
-			print(e)
-			print()
-			print()
-			print_with_line_numbers(code)
-		end;
-	}
-end
--- a/web/src/luan/modules/web/web_shell.luan	Sun Apr 12 06:54:14 2015 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,65 +0,0 @@
-local Luan = require "luan:Luan"
-local ipairs = Luan.ipairs
-local load = Luan.load
-local try = Luan.try
-local Io = require "luan:Io"
-local print = Io.print
-local Debug = require "luan:Debug"
-local Http = require "luan:web/Http"
-local Html = require "luan:Html"
-
-per_session = true
-
-local history = {}
-local env = {}
-
-function service()
-	if Http.request.parameters.clear ~= nil then
-		history = {}
-	else
-		local cmd = Http.request.parameters.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 = load(cmd,"<web_shell>",env,true)
-					Debug.print_if_something( line() )
-				end;
-				catch = function(e)
-					Io.print_to(Io.stderr,e)
-					print(e)
-				end;
-			}
-		end
-	end
-
-	Io.stdout = Http.response.text_writer()
-	Html.simply_html_page{
-		head = function() %>
-			<title>Luan Shell</title>
-<%		end;
-		body = function() %>
-			<div container>
-				<h3>Luan Shell</h3>
-				<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 name='cmd' size="80" autofocus>
-					<input type="submit" value="run" textcolor="white" bgcolor="#337ab7">
-					<input type="submit" name="clear" value="clear" textcolor="white" bgcolor="#337ab7">
-				</form>
-			</div>
-<%		end;
-	}
-
-end