Software /
code /
prosody-modules
Annotate
mod_stanza_counter/mod_stanza_counter_http.lua @ 568:3d66b0af9a15
mod_stanza_counter_http: revert to use module:get_option_boolean
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Tue, 17 Jan 2012 13:58:13 +0000 |
parent | 562:b3f8435e661c |
child | 600:03be4622454f |
rev | line source |
---|---|
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
1 -- (C) 2011, Marco Cirillo (LW.Org) |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
2 -- Exposes stats on HTTP for the stanza counter module. |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
3 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
4 module:set_global() |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
5 |
518
d4adda950546
mod_stanza_counter: corrected syntax mistake in the http plugin.
Marco Cirillo <maranda@lightwitch.org>
parents:
437
diff
changeset
|
6 local ports = module:get_option_array("stanza_counter_http_ports", {{ port = 5280 }}) |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
7 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
8 local httpserver = require "net.httpserver" |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
9 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
10 -- http handlers |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
11 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
12 local r_200 = "\n<html>\n<head>\n<title>Prosody's Stanza Counter</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>Incoming and Outgoing stanzas divided per type</h3>\n<p><strong>Incoming IQs</strong>: %d<br/>\n<strong>Outgoing IQs</strong>: %d<br/>\n<strong>Incoming Messages</strong>: %d<br/>\n<strong>Outgoing Messages</strong>: %d<br/>\n<strong>Incoming Presences</strong>: %d<br/>\n<strong>Outgoing Presences</strong>: %d<p>\n</body>\n\n</html>\n" |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
13 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
14 local r_err = "\n<html>\n<head>\n<title>Prosody's Stanza Counter - Error %s</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>%s</h3>\n</body>\n\n</html>\n" |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
15 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
16 local function res(code, r, h) |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
17 local response = { |
535
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
18 status = code, |
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
19 body = r |
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
20 } |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
21 |
535
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
22 if h then response.headers = h end |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
23 return response |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
24 end |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
25 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
26 local function req(method, body, request) |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
27 if not prosody.stanza_counter then |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
28 local err500 = r_err:format("500", "Stats not found, is the counter module loaded?") |
437
78a2a6b2bea3
mod_stanza_counter: missing end.
Marco Cirillo <maranda@lightwitch.org>
parents:
436
diff
changeset
|
29 return res(500, err500) end |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
30 if method == "GET" then |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
32 prosody.stanza_counter.iq["outgoing"], |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
33 prosody.stanza_counter.message["incoming"], |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
34 prosody.stanza_counter.message["outgoing"], |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
35 prosody.stanza_counter.presence["incoming"], |
535
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
36 prosody.stanza_counter.presence["outgoing"]) |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
37 return res(200, forge_res) |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
38 else |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
39 local err405 = r_err:format("405", "Only GET is supported") |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
40 return res(405, err405, {["Allow"] = "GET"}) |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
41 end |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
42 end |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
43 |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
44 -- initialization. |
562
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
45 -- init http and cleanup interface |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
46 |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
47 function cleanup() -- recycled from mod_register_json, it's handy |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
48 module:log("debug", "Cleaning up handlers and stuff as module is being unloaded.") |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
49 for _, options in ipairs(ports) do |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
50 if options.port then |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
51 httpserver.new.http_servers[options.port].handlers[options.path or "stanza-counter"] = nil |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
52 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
53 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
54 |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
55 -- if there are no handlers left clean and close the socket, doesn't work with server_event |
568
3d66b0af9a15
mod_stanza_counter_http: revert to use module:get_option_boolean
Marco Cirillo <maranda@lightwitch.org>
parents:
562
diff
changeset
|
56 local event = module:get_option_boolen("use_libevent", false) |
562
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
57 |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
58 if not event then |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
59 for _, options in ipairs(ports) do |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
60 if options.port and not next(httpserver.new.http_servers[options.port].handlers) then |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
61 httpserver.new.http_servers[options.port] = nil |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
62 if options.interface then |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
63 for _, value in ipairs(options.interface) do |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
64 if server.getserver(value, options.port) then server.removeserver(value, options.port) end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
65 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
66 else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
67 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
68 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
69 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
70 |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
71 prosody.events.remove_handler("module-unloaded", cleanup) |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
72 end |
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
73 |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
74 local function setup() |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
75 httpserver.new_from_config(ports, req, { base = "stanza-counter" }) |
562
b3f8435e661c
mod_stanza_counter_http: added cleanup function (from mod_register_json)
Marco Cirillo <maranda@lightwitch.org>
parents:
535
diff
changeset
|
76 prosody.events.add_handler("module-unloaded", cleanup) |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
77 end |
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
78 |
535
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
79 -- set it |
39c7115be370
mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents:
518
diff
changeset
|
80 if prosody.start_time then setup() else module:hook("server-started", setup) end |