Simple web server working (although slow 35ms instead of 6ms using uhttpd)

This commit is contained in:
Martin Schröder 2015-08-26 11:08:31 +02:00
parent cb024c1cb8
commit c11cfa3253
3 changed files with 36 additions and 20 deletions

View File

@ -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

View File

@ -50,3 +50,11 @@ function ubus(_calls, arg)
io.write("Unknown method!\n");
end
end
return {
readfile = readfile,
log = log,
shell = shell,
ubus = ubus
};

View File

@ -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)