GSoC Commit #1: LuCId + HTTP-Server
[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 function factory(server, config)
22 pcall(function()
23 require "luci.dispatcher"
24 require "luci.cbi"
25 end)
26
27 config.domain = config.domain or ""
28 local vhost = server:get_vhosts()[config.domain]
29 if not vhost then
30 vhost = srv.VHost()
31 server:set_vhost(config.domain, vhost)
32 end
33
34 local prefix
35 if config.physical and #config.physical > 0 then
36 prefix = {}
37 for k in config.physical:gmatch("[^/]+") do
38 if #k > 0 then
39 prefix[#prefix+1] = k
40 end
41 end
42 end
43
44 local handler = luci.Luci(config.name, prefix)
45 if config.exec then
46 for _, r in ipairs(config.exec) do
47 if r:sub(1,1) == ":" then
48 handler:restrict({interface = r:sub(2)})
49 else
50 handler:restrict({user = r})
51 end
52 end
53 end
54
55 if type(config.virtual) == "table" then
56 for _, v in ipairs(config.virtual) do
57 vhost:set_handler(v, handler)
58 end
59 else
60 vhost:set_handler(config.virtual, handler)
61 end
62 end