comparison http/src/luan/modules/web/run.luan @ 493:1d082a0812e0

move web to http
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 May 2015 17:29:59 -0600
parents web/src/luan/modules/web/run.luan@2f5cc9c2cbf0
children
comparison
equal deleted inserted replaced
492:b36cc406d3d2 493:1d082a0812e0
1 local Luan = require "luan:Luan"
2 local load = Luan.load
3 local try = Luan.try
4 local Io = require "luan:Io"
5 local print = Io.print
6 local Http = require "luan:web/Http"
7 local String = require "luan:String"
8 local Html = require "luan:Html"
9
10 local function lines(s)
11 local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
12 return function()
13 local m1, m2 = matcher()
14 return m1 or m2
15 end
16 end
17
18 local function print_with_line_numbers(s)
19 i = 1
20 for line in lines(s) do
21 print(i,line)
22 i = i + 1
23 end
24 end
25
26 local function form() %>
27 <html>
28 <head>
29 <% Html.simply_html_head() %>
30 <title>Run Luan Code</title>
31 </head>
32 <body>
33 <center margin-top=10>
34 <h3>Run Luan Code</h3>
35 </center>
36 <form name="form0" method="post">
37 <input type="hidden" name="content_type" value="text/plain" />
38 <center>
39 <textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
40 </center>
41 <center margin-top=5>
42 <input type="submit" value="Execute Luan Code" textcolor="white" bgcolor="#337ab7"/>
43 </center>
44 </form>
45 <% Html.simply_html_body_bottom() %>
46 </body>
47 </html>
48 <% end
49
50 function service()
51 Io.stdout = Http.response.text_writer()
52 local code = Http.request.parameters.code
53 if code == nil then
54 form()
55 return
56 end
57 local content_type = Http.request.parameters.content_type
58 if content_type ~= nil then
59 Http.response.content_type = content_type
60 end
61 local env = {
62 request = Http.request;
63 response = Http.response;
64 }
65 try {
66 function()
67 local run = load(code,"<web_run>",env)
68 run()
69 end;
70 catch = function(e)
71 Http.response.content_type = "text/plain"
72 print(e)
73 print()
74 print()
75 print_with_line_numbers(code)
76 end;
77 }
78 end