changeset 505:7bc63886d4f2

web page modules now return a function
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 May 2015 01:45:49 -0600
parents dbdf4b8193a8
children 342964519194
files http/src/luan/modules/http/HttpServicer.java http/src/luan/modules/http/HttpTest.luan http/src/luan/modules/http/ShellMod.luan http/src/luan/modules/http/run.luan http/src/luan/modules/http/shell.luan lucene/src/luan/modules/lucene/Ab_testing.luan lucene/src/luan/modules/lucene/Web_search.luan scripts/test.luan website/src/Shared.luan website/src/diff.html.luan website/src/docs.html.luan website/src/examples/hi.luan website/src/examples/hi2.luan website/src/examples/hi2_simply_html.luan website/src/index.html.luan website/src/manual.html.luan website/src/pil.html.luan website/src/tutorial.html.luan
diffstat 18 files changed, 101 insertions(+), 145 deletions(-) [+]
line wrap: on
line diff
diff -r dbdf4b8193a8 -r 7bc63886d4f2 http/src/luan/modules/http/HttpServicer.java
--- a/http/src/luan/modules/http/HttpServicer.java	Thu May 21 01:09:35 2015 -0600
+++ b/http/src/luan/modules/http/HttpServicer.java	Thu May 21 01:45:49 2015 -0600
@@ -52,9 +52,8 @@
 			Object mod = PackageLuan.load(luan,modName);
 			if( mod==null )
 				return false;
-			if( !(mod instanceof LuanTable) )
-				throw luan.exception( "module '"+modName+"' must return a table" );
-			LuanTable tbl = (LuanTable)mod;
+			if( !(mod instanceof LuanFunction) )
+				throw luan.exception( "module '"+modName+"' must return a function" );
 			if( Boolean.TRUE.equals(per_session_pages.rawGet(mod)) ) {
 				HttpSession session = request.getSession();
 				LuanState sessionLuan  = (LuanState)session.getValue("luan");
@@ -65,13 +64,11 @@
 					luan = (LuanState)cloner.deepClone(luan);
 					session.putValue("luan",luan);
 				}
-				tbl = (LuanTable)PackageLuan.require(luan,modName);
-				fn = getService(luan,tbl);
+				fn = (LuanFunction)PackageLuan.require(luan,modName);
 			} else {
-				fn = getService(luan,tbl);
 				DeepCloner cloner = new DeepCloner();
 				luan = (LuanState)cloner.deepClone(luan);
-				fn = (LuanFunction)cloner.get(fn);
+				fn = (LuanFunction)cloner.get(mod);
 			}
 		}
 
@@ -164,17 +161,6 @@
 		return true;
 	}
 
-	private static LuanFunction getService(LuanState luan,LuanTable tbl)
-		throws LuanException
-	{
-		Object respond = tbl.get(luan,"respond");
-		if( respond == null )
-			throw luan.exception( "function 'respond' is not defined" );
-		if( !(respond instanceof LuanFunction) )
-			throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) );
-		return (LuanFunction)respond;
-	}
-
 	private static void setResponse(LuanTable module,HttpServletResponse response) throws LuanException {
 		LuanTable responseTbl = (LuanTable)module.rawGet("response");
 		int status = Luan.asInteger(responseTbl.rawGet("status"));
diff -r dbdf4b8193a8 -r 7bc63886d4f2 http/src/luan/modules/http/HttpTest.luan
--- a/http/src/luan/modules/http/HttpTest.luan	Thu May 21 01:09:35 2015 -0600
+++ b/http/src/luan/modules/http/HttpTest.luan	Thu May 21 01:45:49 2015 -0600
@@ -12,7 +12,7 @@
 	end
 	local old_out = Io.stdout
 	local mod = require("site:"..path)
-	mod.respond()
+	mod()
 	M.text_writer.close()
 	Io.stdout = old_out
 	return M.result.read_text()
diff -r dbdf4b8193a8 -r 7bc63886d4f2 http/src/luan/modules/http/ShellMod.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/src/luan/modules/http/ShellMod.luan	Thu May 21 01:45:49 2015 -0600
@@ -0,0 +1,72 @@
+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:http/Http"
+local Html = require "luan:Html"
+
+local M = {}
+
+local history = {}
+M.env = {}
+
+function M.respond()
+	if Http.request.parameter.clear ~= nil then
+		history = {}
+	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 = load(cmd,"<web_shell>",M.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>
+	<head>
+		<% Html.simply_html_head() %>
+		<title>Luan Shell</title>
+	</head>
+	<body>
+		<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>
+		<% Html.simply_html_body_bottom() %>
+	</body>
+</html>
+<%
+end
+
+Http.per_session(M.respond)
+
+return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 http/src/luan/modules/http/run.luan
--- a/http/src/luan/modules/http/run.luan	Thu May 21 01:09:35 2015 -0600
+++ b/http/src/luan/modules/http/run.luan	Thu May 21 01:45:49 2015 -0600
@@ -7,7 +7,6 @@
 local String = require "luan:String"
 local Html = require "luan:Html"
 
-local M = {}
 
 local function lines(s)
 	local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
@@ -49,7 +48,7 @@
 </html>
 <% end
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 	local code = Http.request.parameter.code
 	if code == nil then
@@ -78,5 +77,3 @@
 		end;
 	}
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 http/src/luan/modules/http/shell.luan
--- a/http/src/luan/modules/http/shell.luan	Thu May 21 01:09:35 2015 -0600
+++ b/http/src/luan/modules/http/shell.luan	Thu May 21 01:45:49 2015 -0600
@@ -1,72 +1,1 @@
-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:http/Http"
-local Html = require "luan:Html"
-
-local M = {}
-
-Http.per_session(M)
-
-local history = {}
-M.env = {}
-
-function M.respond()
-	if Http.request.parameter.clear ~= nil then
-		history = {}
-	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 = load(cmd,"<web_shell>",M.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>
-	<head>
-		<% Html.simply_html_head() %>
-		<title>Luan Shell</title>
-	</head>
-	<body>
-		<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>
-		<% Html.simply_html_body_bottom() %>
-	</body>
-</html>
-<%
-end
-
-return M
+return require("luan:http/ShellMod").respond
diff -r dbdf4b8193a8 -r 7bc63886d4f2 lucene/src/luan/modules/lucene/Ab_testing.luan
--- a/lucene/src/luan/modules/lucene/Ab_testing.luan	Thu May 21 01:09:35 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Ab_testing.luan	Thu May 21 01:45:49 2015 -0600
@@ -133,7 +133,7 @@
 	end
 
 	function ab_testing.web_page(test_names)
-		return { respond = function()
+		return function()
 			local results = {}
 			for _, name in ipairs(test_names) do
 				local test = ab_testing.test_map[name]
@@ -142,7 +142,7 @@
 			end
 			Io.stdout = Http.response.text_writer()
 			M.html(test_names,ab_testing.test_map,results)
-		end }
+		end
 	end
 
 	return ab_testing
diff -r dbdf4b8193a8 -r 7bc63886d4f2 lucene/src/luan/modules/lucene/Web_search.luan
--- a/lucene/src/luan/modules/lucene/Web_search.luan	Thu May 21 01:09:35 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Web_search.luan	Thu May 21 01:45:49 2015 -0600
@@ -122,7 +122,7 @@
 
 function M.of(index)
 
-	return { respond = function()
+	return function()
 		Io.stdout = Http.response.text_writer()
 		local query_string = Http.request.parameter.query
 		if query_string == nil then
@@ -145,7 +145,7 @@
 			end
 			result(query,sort,headers,table)
 		end )
-	end }
+	end
 
 end
 
diff -r dbdf4b8193a8 -r 7bc63886d4f2 scripts/test.luan
--- a/scripts/test.luan	Thu May 21 01:09:35 2015 -0600
+++ b/scripts/test.luan	Thu May 21 01:45:49 2015 -0600
@@ -69,19 +69,19 @@
 end
 
 init()
-ab_testing.web_page{"All","null"}.respond()
+ab_testing.web_page{"All","null"}()
 
 local Web_search = require "luan:lucene/Web_search"
 local web_search = Web_search.of(db)
 
 init()
-web_search.respond()
+web_search()
 
 init()
 Http.request.parameter.query = "Query.all_docs"
 Http.request.parameter.rows = "100"
 Http.request.parameter.sort = ""
-web_search.respond()
+web_search()
 
 
 -- website
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/Shared.luan
--- a/website/src/Shared.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/Shared.luan	Thu May 21 01:45:49 2015 -0600
@@ -4,4 +4,6 @@
 	<div><small><a href="/">Luan</a></small></div>
 <% end
 
+M.heading_options = [[margin-top="2em" margin-bottom=".6em" textcolor="#233E93"]]
+
 return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/diff.html.luan
--- a/website/src/diff.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/diff.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -3,12 +3,10 @@
 local Http = require "luan:http/Http"
 local Shared = require "site:/Shared"
 local Manual = require "site:/manual.html"
-
-local M = {}
+local heading_options = Shared.heading_options
 
-local heading_options = Manual.heading_options
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -208,5 +206,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/docs.html.luan
--- a/website/src/docs.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/docs.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -3,9 +3,8 @@
 local Http = require "luan:http/Http"
 local Shared = require "site:/Shared"
 
-local M = {}
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -33,5 +32,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/examples/hi.luan
--- a/website/src/examples/hi.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/examples/hi.luan	Thu May 21 01:45:49 2015 -0600
@@ -1,9 +1,8 @@
 local Io = require "luan:Io"
 local Http = require "luan:http/Http"
 
-local M = {}
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 	%>
 	<html>
@@ -13,5 +12,3 @@
 	</html>
 	<%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/examples/hi2.luan
--- a/website/src/examples/hi2.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/examples/hi2.luan	Thu May 21 01:45:49 2015 -0600
@@ -1,7 +1,6 @@
 local Io = require "luan:Io"
 local Http = require "luan:http/Http"
 
-local M = {}
 
 local function form()
 %>
@@ -29,7 +28,7 @@
 <%
 end
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 	local name = Http.request.parameter.name
 	if name == nil then
@@ -38,5 +37,3 @@
 		hello(name)
 	end
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/examples/hi2_simply_html.luan
--- a/website/src/examples/hi2_simply_html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/examples/hi2_simply_html.luan	Thu May 21 01:45:49 2015 -0600
@@ -2,7 +2,6 @@
 local Html = require "luan:Html"
 local Http = require "luan:http/Http"
 
-local M = {}
 
 local function form() %>
 			<form>
@@ -18,7 +17,7 @@
 <% end
 
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 	local name = Http.request.parameter.name
 %>
@@ -43,5 +42,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/index.html.luan
--- a/website/src/index.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/index.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -2,9 +2,8 @@
 local Html = require "luan:Html"
 local Http = require "luan:http/Http"
 
-local M = {}
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -32,5 +31,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/manual.html.luan
--- a/website/src/manual.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/manual.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -3,13 +3,10 @@
 local Html = require "luan:Html"
 local Http = require "luan:http/Http"
 local Shared = require "site:/Shared"
-
-local M = {}
-
-local heading_options = [[margin-top="2em" margin-bottom=".6em" textcolor="#233E93"]]
-M.heading_options = heading_options
-
-function M.respond()
+local heading_options = Shared.heading_options
+
+
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -5132,5 +5129,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/pil.html.luan
--- a/website/src/pil.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/pil.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -3,9 +3,8 @@
 local Http = require "luan:http/Http"
 local Shared = require "site:/Shared"
 
-local M = {}
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -26,5 +25,3 @@
 </html>
 <%
 end
-
-return M
diff -r dbdf4b8193a8 -r 7bc63886d4f2 website/src/tutorial.html.luan
--- a/website/src/tutorial.html.luan	Thu May 21 01:09:35 2015 -0600
+++ b/website/src/tutorial.html.luan	Thu May 21 01:45:49 2015 -0600
@@ -3,9 +3,8 @@
 local Http = require "luan:http/Http"
 local Shared = require "site:/Shared"
 
-local M = {}
 
-function M.respond()
+return function()
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -110,5 +109,3 @@
 </html>
 <%
 end
-
-return M