|
| 1 | +local extension = ... |
| 2 | + |
| 3 | +local StringBuffer = require('jls.lang.StringBuffer') |
| 4 | +local RestHttpHandler = require('jls.net.http.handler.RestHttpHandler') |
| 5 | + |
| 6 | +local contexts = {} |
| 7 | + |
| 8 | +local function cleanup(server) |
| 9 | + for _, context in ipairs(contexts) do |
| 10 | + server:removeContext(context) |
| 11 | + end |
| 12 | + contexts = {} |
| 13 | +end |
| 14 | + |
| 15 | +local function addContext(server, ...) |
| 16 | + local context = server:createContext(...) |
| 17 | + table.insert(contexts, context) |
| 18 | +end |
| 19 | + |
| 20 | +--[[ |
| 21 | +local function os(cmd) |
| 22 | + local f, err = io.popen(cmd) |
| 23 | + if f then |
| 24 | + local r = f:read('a') |
| 25 | + f:close() |
| 26 | + return r |
| 27 | + end |
| 28 | + return err |
| 29 | +end |
| 30 | +os('uname -a') |
| 31 | +
|
| 32 | +local m = {} |
| 33 | +for _, v in pairs(debug.getregistry()) do |
| 34 | + local t = type(v) |
| 35 | + if t == 'userdata' then |
| 36 | + local ut = tostring(v) |
| 37 | + ut = string.match(ut, '^([^:]+):.*') |
| 38 | + if ut then |
| 39 | + t = t..':'..ut |
| 40 | + end |
| 41 | + end |
| 42 | + m[t] = (m[t] or 0) + 1 |
| 43 | +end |
| 44 | +for t, c in pairs(m) do |
| 45 | + print(t, c) |
| 46 | +end |
| 47 | +
|
| 48 | +local c = 0 |
| 49 | +for cl, ex in pairs(engine:getHTTPServer().pendings) do |
| 50 | + c = c + 1 |
| 51 | +end |
| 52 | +print('pending clients', c) |
| 53 | +
|
| 54 | +local luv = require('luv') |
| 55 | +print('uname', luv.os_uname()) |
| 56 | +print('hostname', luv.os_gethostname()) |
| 57 | +print('pid', luv.os_getpid() >> 0) |
| 58 | +print('printing active handles on stdout'); luv.print_active_handles() |
| 59 | +
|
| 60 | +require('jls.util.memprof').printReport(function(data) |
| 61 | + print('report', data) |
| 62 | +end, false, false, 'csv') |
| 63 | +]] |
| 64 | +extension:subscribeEvent('startup', function() |
| 65 | + local engine = extension:getEngine() |
| 66 | + local server = engine:getHTTPServer() |
| 67 | + cleanup(server) |
| 68 | + addContext(server, '/engine/tools/(.*)', RestHttpHandler:new({ |
| 69 | + ['execute?method=POST'] = function(exchange) |
| 70 | + local script = exchange:getRequest():getBody() |
| 71 | + local fn, err = load(table.concat({ |
| 72 | + 'local b, engine = ...', |
| 73 | + 'print = function(v, ...)', |
| 74 | + 'b:append(v)', |
| 75 | + 'if ... then', |
| 76 | + 'for _, w in ipairs({...}) do', |
| 77 | + 'b:append("\\t"):append(w)', |
| 78 | + 'end', |
| 79 | + 'end', |
| 80 | + 'b:append("\\n")', |
| 81 | + 'end', |
| 82 | + script |
| 83 | + }, '\n'), 'tools/execute', 't') |
| 84 | + local b = StringBuffer:new() |
| 85 | + if fn then |
| 86 | + fn(b, engine) |
| 87 | + return b:toString() |
| 88 | + end |
| 89 | + return err or 'Error' |
| 90 | + end |
| 91 | + })) |
| 92 | + engine:onExtension('web-base', function(webBaseExtension) |
| 93 | + webBaseExtension:registerAddonExtension(extension, true) |
| 94 | + end) |
| 95 | +end) |
| 96 | + |
| 97 | +extension:subscribeEvent('shutdown', function() |
| 98 | + local engine = extension:getEngine() |
| 99 | + local server = engine:getHTTPServer() |
| 100 | + engine:onExtension('web-base', function(webBaseExtension) |
| 101 | + webBaseExtension:unregisterAddonExtension(extension) |
| 102 | + end) |
| 103 | + cleanup(server) |
| 104 | +end) |
0 commit comments