Comparison

mod_persisthosts/mod_persisthosts.lua @ 2446:c563f4d64302

mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
author Kim Alvefur <zash@zash.se>
date Wed, 18 Jan 2017 01:38:17 +0100
child 2447:366fadb5c6e5
comparison
equal deleted inserted replaced
2445:e822900c87d4 2446:c563f4d64302
1 -- mod_persisthosts
2 module:set_global();
3
4 local set = require"util.set";
5 local stat = require"lfs".attributes;
6 local resolve_relative_path = require"core.configmanager".resolve_relative_path;
7
8 local vhost_path = module:get_option_string("persisthosts_path", "conf.d");
9 local path_pattern = resolve_relative_path(prosody.paths.config, vhost_path) .. "/%s.cfg.lua";
10
11 local original = set.new();
12 original:include(prosody.hosts);
13
14 module:hook("host-activated", function(host)
15 if not original:contains(host) then
16 local path = path_pattern:format(host);
17 if not stat(path) then
18 local fh, err = io.open(path, "w");
19 if fh then
20 fh:write(("VirtualHost%q\n"):format(host));
21 fh:close();
22 module:log("info", "Config file for host '%s' created", host);
23 else
24 module:log("error", "Could not open '%s' for writing: %s", path, err or "duno");
25 end
26 else
27 module:log("debug", "File '%s' existed already", path);
28 end
29 else
30 module:log("debug", "VirtualHost '%s' existed already", host);
31 end
32 end);
33