build: split into luci and luci-addons packages
[project/luci.git] / libs / lucid-http / luasrc / lucid / http / LuciWebPublisher.lua
1 --[[
2 LuCId HTTP-Slave
3 (c) 2009 Steven Barth <steven@midlink.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12 ]]--
13
14 local ipairs, pcall, type = ipairs, pcall, type
15 local luci = require "luci.lucid.http.handler.luci"
16 local srv = require "luci.lucid.http.server"
17
18
19 module "luci.lucid.http.LuciWebPublisher"
20
21
22 --- Prepare a LuCI web publisher and assign it to a given Virtual Host.
23 -- @param server HTTP daemon object
24 -- @param config publisher configuration
25 function factory(server, config)
26         pcall(function()
27                 require "luci.dispatcher"
28                 require "luci.cbi"
29         end)
30
31         config.domain = config.domain or ""
32         local vhost = server:get_vhosts()[config.domain] 
33         if not vhost then
34                 vhost = srv.VHost()
35                 server:set_vhost(config.domain, vhost)
36         end
37
38         local prefix
39         if config.physical and #config.physical > 0 then
40                 prefix = {}
41                 for k in config.physical:gmatch("[^/]+") do
42                         if #k > 0 then
43                                 prefix[#prefix+1] = k
44                         end
45                 end
46         end
47
48         local handler = luci.Luci(config.name, prefix)
49         if config.exec then
50                 for _, r in ipairs(config.exec) do
51                         if r:sub(1,1) == ":" then
52                                 handler:restrict({interface = r:sub(2)})
53                         else
54                                 handler:restrict({user = r})
55                         end
56                 end
57         end
58
59         local mypath
60         if type(config.virtual) == "table" then
61                 for _, v in ipairs(config.virtual) do
62                         mypath = mypath or v
63                         vhost:set_handler(v, handler)
64                 end
65         else
66                 mypath = config.virtual
67                 vhost:set_handler(config.virtual or "", handler)
68         end
69
70         if config.home then
71                 vhost.default = mypath
72         end
73 end