Mercurial Hosting > luan
changeset 1218:a50803fde972
http/tools cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 20 Mar 2018 16:24:59 -0600 |
parents | 4c2972f4d862 |
children | b602badc876b |
files | conv.txt examples/blog/src/lib/test.luan examples/blog/src/private/tools/lucene.html.luan examples/blog/src/private/tools/lucene.luan examples/blog/src/private/tools/shell.html.luan examples/blog/src/private/tools/shell.luan examples/blog/src/private/tools/test.luan examples/blog/test.sh src/luan/modules/http/test.luan src/luan/modules/http/tools/Shell.luan src/luan/modules/http/tools/Shell_mod.luan src/luan/modules/http/tools/java_threads.html.luan src/luan/modules/http/tools/java_threads.luan src/luan/modules/http/tools/luan_threads.html.luan src/luan/modules/http/tools/luan_threads.luan src/luan/modules/http/tools/shell.html.luan src/luan/modules/http/tools/shell.luan website/src/examples/shell.html.luan website/src/examples/shell.luan |
diffstat | 19 files changed, 282 insertions(+), 296 deletions(-) [+] |
line wrap: on
line diff
diff -r 4c2972f4d862 -r a50803fde972 conv.txt --- a/conv.txt Tue Mar 20 15:43:16 2018 -0600 +++ b/conv.txt Tue Mar 20 16:24:59 2018 -0600 @@ -1,6 +1,11 @@ -headers lower case -remove run_later -remove Thread.global +"java_threads.luan" +"luan_threads.luan" +"Shell_mod.luan" +"shell.luan" + +"Io.repr" +"String.split" +"logging/init.luan" "request.header" "response.header" @@ -9,6 +14,6 @@ "set_cookie" "assert" -"Io.repr" -"String.split" -"logging/init.luan" +headers lower case +remove run_later +remove Thread.global
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/lib/test.luan --- a/examples/blog/src/lib/test.luan Tue Mar 20 15:43:16 2018 -0600 +++ b/examples/blog/src/lib/test.luan Tue Mar 20 16:24:59 2018 -0600 @@ -28,31 +28,31 @@ get_page '/' init() -print '/new' -get_page '/new' +print '/new.html' +get_page '/new.html' init() Http.request.parameters.subject = 'test' Http.request.parameters.content = 'this is a test' Http.request.parameters.save = 'whatever' -print '/new submit' -get_page '/new' +print '/new.html submit' +get_page '/new.html' local posts = Post.get_all() #posts == 1 or error() local post_id = to_string(posts[1].id) init() Http.request.parameters.post = post_id -print '/edit' -get_page '/edit' +print '/edit.html' +get_page '/edit.html' init() Http.request.parameters.post = post_id Http.request.parameters.subject = 'test' Http.request.parameters.content = 'this is an edit' Http.request.parameters.save = 'whatever' -print '/edit submit' -get_page '/edit' +print '/edit.html submit' +get_page '/edit.html' init() print '/ again'
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/private/tools/lucene.html.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/blog/src/private/tools/lucene.html.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,4 @@ +local Web_search = require "luan:lucene/Web_search.luan" +local Db = require "site:/lib/Db.luan" + +return Web_search.of(Db)
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/private/tools/lucene.luan --- a/examples/blog/src/private/tools/lucene.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -local Web_search = require "luan:lucene/Web_search.luan" -local Db = require "site:/lib/Db.luan" - -return Web_search.of(Db)
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/private/tools/shell.html.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/blog/src/private/tools/shell.html.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,1 @@ +return require "luan:http/tools/shell.html.luan"
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/private/tools/shell.luan --- a/examples/blog/src/private/tools/shell.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -return require "luan:http/tools/shell.luan"
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/src/private/tools/test.luan --- a/examples/blog/src/private/tools/test.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -local Luan = require "luan:Luan.luan" -local do_file = Luan.do_file -local try = Luan.try -local Io = require "luan:Io.luan" -local print = Io.print -local Http = require "luan:http/Http.luan" - - -return function() - Io.stdout = Http.response.text_writer() - Http.response.content_type = "text/plain" - try { - function() - do_file "site:/lib/test.luan" - end; - catch = function(e) - print() - print("error:",e) - end; - } - local Db = require "site:/lib/Db.luan" - Db.close() -end
diff -r 4c2972f4d862 -r a50803fde972 examples/blog/test.sh --- a/examples/blog/test.sh Tue Mar 20 15:43:16 2018 -0600 +++ b/examples/blog/test.sh Tue Mar 20 16:24:59 2018 -0600 @@ -1,1 +1,1 @@ -luan luan:http/test.luan file:src site:/lib/test.luan 2>&1 | tee err +luan luan:http/test.luan src site:/lib/test.luan 2>&1 | tee err
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/test.luan --- a/src/luan/modules/http/test.luan Tue Mar 20 15:43:16 2018 -0600 +++ b/src/luan/modules/http/test.luan Tue Mar 20 16:24:59 2018 -0600 @@ -1,12 +1,16 @@ local Luan = require "luan:Luan.luan" local Io = require "luan:Io.luan" local Server = require "luan:http/Server.luan" +local Logging = require "luan:logging/Logging.luan" + if #{...} ~= 2 then - Io.stderr.write "usage: luan luan:http/serve dir-URI test-URI\n" + Io.stderr.write "usage: luan luan:http/serve dir test-URI\n" return end local dir, test = ... -Server.init(dir) + +Logging.init() +Server.init_dir(dir) Luan.do_file(test)
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/Shell.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/Shell.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,143 @@ +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" + + +local Shell = {} + +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.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.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() +%> +<!doctype html> +<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
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/Shell_mod.luan --- a/src/luan/modules/http/tools/Shell_mod.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,143 +0,0 @@ -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() -%> -<!doctype html> -<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
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/java_threads.html.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/java_threads.html.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,51 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local Io = require "luan:Io.luan" +local Time = require "luan:Time.luan" +local Http = require "luan:http/Http.luan" +java() +local Thread = require "java:java.lang.Thread" + + +return function() + Io.stdout = Http.response.text_writer() + + local threads = Thread.getAllStackTraces() + local threads = {} + for thread, trace in Luan.pairs(Thread.getAllStackTraces()) do + threads[#threads+1] = { + trace = trace + string = thread.toString() + state = thread.getState() + } + end +%> +<!doctype html> +<html> + <body> + <h1>Java Threads</h1> + <p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p> + <% + local count = 0 + for _, thread in Luan.ipairs(threads) do + %> + <p><%=thread.string%> <%=thread.state%> + <ul> + <% + local trace = thread.trace + for i in Luan.range( 0 , trace.length - 1 ) do + local line = trace[i].toString() + %><li><%=line%></li><% + end + %> + </ul></p> + <% + count = count + 1 + end + %> + <p><%=count%> threads found</p> + + </body> +</html> +<% +end
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/java_threads.luan --- a/src/luan/modules/http/tools/java_threads.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -local Luan = require "luan:Luan.luan" -local error = Luan.error -local Io = require "luan:Io.luan" -local Time = require "luan:Time.luan" -local Http = require "luan:http/Http.luan" -java() -local Thread = require "java:java.lang.Thread" - - -return function() - Io.stdout = Http.response.text_writer() - - local threads = Thread.getAllStackTraces() - local threads = {} - for thread, trace in Luan.pairs(Thread.getAllStackTraces()) do - threads[#threads+1] = { - trace = trace - string = thread.toString() - state = thread.getState() - } - end -%> -<!doctype html> -<html> - <body> - <h1>Java Threads</h1> - <p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p> - <% - local count = 0 - for _, thread in Luan.ipairs(threads) do - %> - <p><%=thread.string%> <%=thread.state%> - <ul> - <% - local trace = thread.trace - for i in Luan.range( 0 , trace.length - 1 ) do - local line = trace[i].toString() - %><li><%=line%></li><% - end - %> - </ul></p> - <% - count = count + 1 - end - %> - <p><%=count%> threads found</p> - - </body> -</html> -<% -end
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/luan_threads.html.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/luan_threads.html.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,55 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local Io = require "luan:Io.luan" +local Time = require "luan:Time.luan" +local Http = require "luan:http/Http.luan" +java() +local Thread = require "java:java.lang.Thread" +local LuanException = require "java:luan.LuanException" +local JavaLuan = require "java:luan.Luan" + + +return function() + Io.stdout = Http.response.text_writer() + + local threads = Thread.getAllStackTraces() + local threads = {} + for thread, trace in Luan.pairs(Thread.getAllStackTraces()) do + threads[#threads+1] = { + trace = trace + string = thread.toString() + state = thread.getState() + } + end +%> +<!doctype html> +<html> + <body> + <h1>Luan Threads</h1> + <p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p> + <% + local count = 0 + for _, thread in Luan.ipairs(threads) do + local luan_trace = JavaLuan.table(LuanException.justLuan(thread.trace)) + if #luan_trace > 0 then + %> + <p><%=thread.string%> <%=thread.state%> + <ul> + <% + for i, el in Luan.ipairs(luan_trace) do + local line = LuanException.toString(el); + %><li><%=line%></li><% + end + %> + </ul></p> + <% + count = count + 1 + end + end + %> + <p><%=count%> threads found</p> + + </body> +</html> +<% +end
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/luan_threads.luan --- a/src/luan/modules/http/tools/luan_threads.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -local Luan = require "luan:Luan.luan" -local error = Luan.error -local Io = require "luan:Io.luan" -local Time = require "luan:Time.luan" -local Http = require "luan:http/Http.luan" -java() -local Thread = require "java:java.lang.Thread" -local LuanException = require "java:luan.LuanException" -local JavaLuan = require "java:luan.Luan" - - -return function() - Io.stdout = Http.response.text_writer() - - local threads = Thread.getAllStackTraces() - local threads = {} - for thread, trace in Luan.pairs(Thread.getAllStackTraces()) do - threads[#threads+1] = { - trace = trace - string = thread.toString() - state = thread.getState() - } - end -%> -<!doctype html> -<html> - <body> - <h1>Luan Threads</h1> - <p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p> - <% - local count = 0 - for _, thread in Luan.ipairs(threads) do - local luan_trace = JavaLuan.table(LuanException.justLuan(thread.trace)) - if #luan_trace > 0 then - %> - <p><%=thread.string%> <%=thread.state%> - <ul> - <% - for i, el in Luan.ipairs(luan_trace) do - local line = LuanException.toString(el); - %><li><%=line%></li><% - end - %> - </ul></p> - <% - count = count + 1 - end - end - %> - <p><%=count%> threads found</p> - - </body> -</html> -<% -end
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/shell.html.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/shell.html.luan Tue Mar 20 16:24:59 2018 -0600 @@ -0,0 +1,1 @@ +return require("luan:http/tools/Shell.luan").respond
diff -r 4c2972f4d862 -r a50803fde972 src/luan/modules/http/tools/shell.luan --- a/src/luan/modules/http/tools/shell.luan Tue Mar 20 15:43:16 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -return require("luan:http/tools/Shell_mod.luan").respond