build: split into luci and luci-addons packages
[project/luci.git] / libs / lucid-rpc / luasrc / lucid / rpc.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]
14
15 local require, ipairs, pcall = require, ipairs, pcall
16 local srv = require "luci.lucid.rpc.server"
17
18 module "luci.lucid.rpc"
19
20 --- Prepare the RPC-daemon and its associated publishers.
21 -- @param publisher Table of publishers
22 -- @return factory callback or nil, error message
23 function factory(publisher)
24         local root = srv.Module()
25         local server = srv.Server(root)
26
27         for _, r in ipairs(publisher) do
28                 for _, m in ipairs(r.export) do
29                         local s, mod = pcall(require, r.namespace .. "." .. m)
30                         if s and mod then
31                                 local module = mod._factory()
32                                 
33                                 if r.exec then
34                                         for _, x in ipairs(r.exec) do
35                                                 if x:sub(1,1) == ":" then
36                                                         module:restrict({interface = x:sub(2)})
37                                                 else
38                                                         module:restrict({user = x})
39                                                 end
40                                         end
41                                 end
42                                 
43                                 root:add(m, module)
44                         else
45                                 return nil, mod
46                         end
47                 end
48         end
49
50         return function(...) return server:process(...) end
51 end