Completed first version of JSON-RPC API
This commit is contained in:
parent
5778eae369
commit
bda994c32e
2 changed files with 31 additions and 16 deletions
|
@ -16,6 +16,8 @@ $Id$
|
||||||
local require = require
|
local require = require
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local print = print
|
local print = print
|
||||||
|
local pcall = pcall
|
||||||
|
local table = table
|
||||||
|
|
||||||
module "luci.controller.rpc"
|
module "luci.controller.rpc"
|
||||||
|
|
||||||
|
@ -86,30 +88,42 @@ end
|
||||||
|
|
||||||
function rpc_fs()
|
function rpc_fs()
|
||||||
local util = require "luci.util"
|
local util = require "luci.util"
|
||||||
local fs = util.clone(require "luci.fs")
|
local io = require "io"
|
||||||
|
local fs2 = util.clone(require "luci.fs")
|
||||||
local jsonrpc = require "luci.jsonrpc"
|
local jsonrpc = require "luci.jsonrpc"
|
||||||
local http = require "luci.http"
|
local http = require "luci.http"
|
||||||
local ltn12 = require "luci.ltn12"
|
local ltn12 = require "luci.ltn12"
|
||||||
|
|
||||||
function fs.readfile(filename)
|
function fs2.readfile(filename)
|
||||||
if not pcall(require, "mime") then
|
local stat, mime = pcall(require, "mime")
|
||||||
|
if not stat then
|
||||||
error("Base64 support not available. Please install LuaSocket.")
|
error("Base64 support not available. Please install LuaSocket.")
|
||||||
end
|
end
|
||||||
|
|
||||||
return ltn12.source.chain(ltn12.source.file(filename), mime.encode("base64"))
|
local fp = io.open(filename)
|
||||||
|
if not fp then
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function fs.writefile(filename, data)
|
local output = {}
|
||||||
if not pcall(require, "mime") then
|
local sink = ltn12.sink.table(output)
|
||||||
|
local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
|
||||||
|
return ltn12.pump.all(source, sink) and table.concat(output)
|
||||||
|
end
|
||||||
|
|
||||||
|
function fs2.writefile(filename, data)
|
||||||
|
local stat, mime = pcall(require, "mime")
|
||||||
|
if not stat then
|
||||||
error("Base64 support not available. Please install LuaSocket.")
|
error("Base64 support not available. Please install LuaSocket.")
|
||||||
end
|
end
|
||||||
|
|
||||||
local sink = ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(filename))
|
local file = io.open(filename, "w")
|
||||||
return ltn12.pump.all(ltn12.source.string(data), sink)
|
local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
|
||||||
|
return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
|
||||||
end
|
end
|
||||||
|
|
||||||
http.prepare_content("application/json")
|
http.prepare_content("application/json")
|
||||||
ltn12.pump.all(jsonrpc.handle(fs, http.source()), http.write)
|
ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
|
||||||
end
|
end
|
||||||
|
|
||||||
function rpc_sys()
|
function rpc_sys()
|
||||||
|
|
|
@ -44,9 +44,10 @@ function handle(tbl, rawsource, ...)
|
||||||
if stat then
|
if stat then
|
||||||
if type(json.method) == "string"
|
if type(json.method) == "string"
|
||||||
and (not json.params or type(json.params) == "table") then
|
and (not json.params or type(json.params) == "table") then
|
||||||
if tbl[json.method] then
|
local method = resolve(tbl, json.method)
|
||||||
|
if method then
|
||||||
response = reply(json.jsonrpc, json.id,
|
response = reply(json.jsonrpc, json.id,
|
||||||
proxy(resolve(tbl, json.method), unpack(json.params or {})))
|
proxy(method, unpack(json.params or {})))
|
||||||
else
|
else
|
||||||
response = reply(json.jsonrpc, json.id,
|
response = reply(json.jsonrpc, json.id,
|
||||||
nil, {code=-32601, message="Method not found."})
|
nil, {code=-32601, message="Method not found."})
|
||||||
|
|
Loading…
Reference in a new issue