summaryrefslogtreecommitdiffhomepage
path: root/libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua
blob: 92dd7e2cbeaa8e8969796474b1e051d947c12440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--[[
LuCId HTTP-Slave
(c) 2009 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--

local ipairs, pcall, type = ipairs, pcall, type
local luci = require "luci.lucid.http.handler.luci"
local srv = require "luci.lucid.http.server"


module "luci.lucid.http.LuciWebPublisher"


--- Prepare a LuCI web publisher and assign it to a given Virtual Host.
-- @param server HTTP daemon object
-- @param config publisher configuration
function factory(server, config)
	pcall(function()
		require "luci.dispatcher"
		require "luci.cbi"
	end)

	config.domain = config.domain or ""
	local vhost = server:get_vhosts()[config.domain] 
	if not vhost then
		vhost = srv.VHost()
		server:set_vhost(config.domain, vhost)
	end

	local prefix
	if config.physical and #config.physical > 0 then
		prefix = {}
		for k in config.physical:gmatch("[^/]+") do
			if #k > 0 then
				prefix[#prefix+1] = k
			end
		end
	end

	local handler = luci.Luci(config.name, prefix)
	if config.exec then
		for _, r in ipairs(config.exec) do
			if r:sub(1,1) == ":" then
				handler:restrict({interface = r:sub(2)})
			else
				handler:restrict({user = r})
			end
		end
	end

	local mypath
	if type(config.virtual) == "table" then
		for _, v in ipairs(config.virtual) do
			mypath = mypath or v
			vhost:set_handler(v, handler)
		end
	else
		mypath = config.virtual
		vhost:set_handler(config.virtual, handler)
	end

	if config.home then
		vhost.default = mypath
	end
end