Comparison

mod_register_json/mod_register_json.lua @ 560:b62f5e38f865

mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 17 Jan 2012 00:57:47 +0000
parent 553:7310ceb7564f
child 561:f2ec7149b005
comparison
equal deleted inserted replaced
559:6be3a130c810 560:b62f5e38f865
15 15
16 module.host = "*" -- HTTP/BOSH Servlets need to be global. 16 module.host = "*" -- HTTP/BOSH Servlets need to be global.
17 17
18 -- Pick up configuration. 18 -- Pick up configuration.
19 19
20 local set_realm_name = module:get_option("reg_servlet_realm") or "Restricted" 20 local set_realm_name = module:get_option_string("reg_servlet_realm", "Restricted")
21 local throttle_time = module:get_option("reg_servlet_ttime") or false 21 local throttle_time = module:get_option_number("reg_servlet_ttime", nil)
22 local whitelist = module:get_option_set("reg_servlet_wl", {}) 22 local whitelist = module:get_option_set("reg_servlet_wl", {})
23 local blacklist = module:get_option_set("reg_servlet_bl", {}) 23 local blacklist = module:get_option_set("reg_servlet_bl", {})
24 local ports = module:get_option_array("reg_servlet_ports", {{ port = 9280 }})
24 local recent_ips = {} 25 local recent_ips = {}
25 26
26 -- Begin 27 -- Begin
27 28
28 local function http_response(code, message, extra_headers) 29 local function http_response(code, message, extra_headers)
113 end 114 end
114 end 115 end
115 end 116 end
116 117
117 -- Set it up! 118 -- Set it up!
118 local function setup() 119 function cleanup() -- it could be better if module:hook("module-unloaded", ...) actually worked.
119 local ports = module:get_option("reg_servlet_ports") or { 9280 } 120 module:log("debug", "Cleaning up handlers and stuff as module is being unloaded.")
120 local port_number, base_name, ssl_table 121 for _, options in ipairs(ports) do
121 for _, opts in ipairs(ports) do 122 if options.port then
122 if type(opts) == "number" then 123 httpserver.new.http_servers[options.port].handlers[options.path or "register_account"] = nil
123 port_number, base_name = opts, "register_account"
124 elseif type(opts) == "table" then
125 port_number, base_name, ssl_table = opts.port or 9280, opts.path or "register_account", opts.ssl or nil
126 elseif type(opts) == "string" then
127 base_name, port_number = opts, 9280
128 end 124 end
129 end 125 end
130 126
131 if ssl_table == nil then 127 -- if there're no handlers left clean the socket, not sure if it works with server_select
132 ports = { { port = port_number } } 128 for _, options in ipairs(ports) do
133 httpserver.new_from_config(ports, handle_req, { base = base_name }) 129 if options.port and not next(httpserver.new.http_servers[options.port].handlers) then
134 else 130 httpserver.new.http_servers[options.port] = nil
135 if port_number == 9280 then port_number = 9443 end 131 if options.interface then
136 ports = { { port = port_number, ssl = ssl_table } } 132 for _, value in ipairs(options.interface) do
137 httpserver.new_from_config(ports, handle_req, { base = base_name }) 133 if server.getserver(value, options.port) then server.removeserver(value, options.port) end
134 end
135 else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end
136 end
138 end 137 end
138
139 prosody.events.remove_handler("module-unloaded", cleanup)
140 end
141
142 function setup()
143 for id, options in ipairs(ports) do
144 if not options.port then
145 if not options.ssl then ports[id].port = 9280
146 else ports[id].port = 9443 end
147 elseif options.port == 9280 and options.ssl then ports[id].port = 9443 end end
148 httpserver.new_from_config(ports, handle_req, { base = "register_account" })
149 prosody.events.add_handler("module-unloaded", cleanup)
139 end 150 end
140 151
141 if prosody.start_time then -- already started 152 if prosody.start_time then -- already started
142 setup() 153 setup()
143 else 154 else