mirror of
https://github.com/mkschreder/juci.git
synced 2025-01-07 03:16:39 +08:00
Simple web server working (although slow 35ms instead of 6ms using uhttpd)
This commit is contained in:
parent
cb024c1cb8
commit
c11cfa3253
@ -21,11 +21,30 @@ local mime_types = {
|
||||
jpg = "image/jpeg",
|
||||
jpeg = "image/jpeg",
|
||||
png = "image/png",
|
||||
woff = "application/x-font-woff",
|
||||
text = "text/plain"
|
||||
};
|
||||
|
||||
local rpcid = 0;
|
||||
|
||||
local objects = {};
|
||||
local namespaces = conn:objects()
|
||||
for i, n in ipairs(namespaces) do
|
||||
local signatures = conn:signatures(n)
|
||||
local methods = {};
|
||||
--print("object="..n);
|
||||
for p, s in pairs(signatures) do
|
||||
local meth = {};
|
||||
--print("\tprocedure=" .. p)
|
||||
for k, v in pairs(s) do
|
||||
meth[k] = v;
|
||||
--print("\t\tattribute=" .. k .. " type=" .. v)
|
||||
end
|
||||
methods[p] = meth;
|
||||
end
|
||||
objects[n] = methods;
|
||||
end
|
||||
|
||||
http.createServer({}, function(req, res)
|
||||
--print(json.stringify(req));
|
||||
local path = req.url;
|
||||
@ -49,32 +68,21 @@ http.createServer({}, function(req, res)
|
||||
local result = { jsonrpc = "2.0", id = rpcid };
|
||||
rpcid = rpcid +1;
|
||||
if post.method == "list" then
|
||||
local objects = {};
|
||||
local namespaces = conn:objects()
|
||||
for i, n in ipairs(namespaces) do
|
||||
local signatures = conn:signatures(n)
|
||||
local methods = {};
|
||||
print("object="..n);
|
||||
for p, s in pairs(signatures) do
|
||||
local meth = {};
|
||||
print("\tprocedure=" .. p)
|
||||
for k, v in pairs(s) do
|
||||
print("\t\tattribute=" .. k .. " type=" .. v)
|
||||
end
|
||||
methods[p] = meth;
|
||||
end
|
||||
objects[n] = methods;
|
||||
end
|
||||
|
||||
result["result"] = objects;
|
||||
elseif post.method == "call" then
|
||||
print("Call: "..post.params[2].." "..post.params[3].." "..json.stringify(post.params[4]));
|
||||
local r,code = conn:call(post.params[2], post.params[3], post.params[4]);
|
||||
local obj_id, meth_id, params = post.params[2], post.params[3], post.params[4];
|
||||
print("JSON CALL: "..obj_id.." "..meth_id.." "..json.stringify(params));
|
||||
if(objects[obj_id] and objects[obj_id][meth_id] and objects[obj_id][meth_id]["ubus_rpc_session"]) then
|
||||
params["ubus_rpc_session"] = post.params[1];
|
||||
end
|
||||
local r,code = conn:call(obj_id, meth_id, params);
|
||||
result["result"] = {code or 0, r or {}};
|
||||
end
|
||||
local body = json.stringify(result);
|
||||
res:setHeader("Content-Type", "text/json");
|
||||
res:setHeader("Content-Length", #body)
|
||||
print("Result: "..body);
|
||||
print("JSON RESP: "..body);
|
||||
res:finish(body);
|
||||
end)
|
||||
else
|
||||
|
@ -50,3 +50,11 @@ function ubus(_calls, arg)
|
||||
io.write("Unknown method!\n");
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
readfile = readfile,
|
||||
log = log,
|
||||
shell = shell,
|
||||
ubus = ubus
|
||||
};
|
||||
|
||||
|
@ -53,7 +53,7 @@ exports.start = function(thread_func, ...)
|
||||
-- Start new event loop for thread.
|
||||
require('uv').run()
|
||||
end
|
||||
return uv.new_thread(thread_entry, dumped, table.concat(bundlePaths, ";"), ...)
|
||||
return uv.new_thread(thread_entry, dumped, ...)
|
||||
end
|
||||
|
||||
exports.join = function(thread)
|
||||
|
Loading…
Reference in New Issue
Block a user