diff http/src/luan/modules/http/run.luan @ 494:2b9bc97f0439

change luan:web to luan:http
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 May 2015 17:43:13 -0600
parents http/src/luan/modules/web/run.luan@1d082a0812e0
children 598123096772
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/src/luan/modules/http/run.luan	Fri May 15 17:43:13 2015 -0600
@@ -0,0 +1,78 @@
+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:http/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>
+	<head>
+		<% Html.simply_html_head() %>
+		<title>Run Luan Code</title>
+	</head>
+	<body>
+		<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>
+		<% Html.simply_html_body_bottom() %>
+	</body>
+</html>
+<% 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