comparison mod_stanza_counter/mod_stanza_counter_http.lua @ 650:34e7093cf419

mod_stanza_counter_http: updated to current HTTP API.
author Marco Cirillo <maranda@lightwitch.org>
date Sun, 29 Apr 2012 13:06:53 +0000
parents fb9a2ab8458a
children f7aacd9e93de
comparison
equal deleted inserted replaced
649:dfd7f1ed7782 650:34e7093cf419
1 -- (C) 2011, Marco Cirillo (LW.Org) 1 -- (C) 2011, Marco Cirillo (LW.Org)
2 -- Exposes stats on HTTP for the stanza counter module. 2 -- Exposes stats on HTTP for the stanza counter module.
3 3
4 module:depends("http")
4 module:set_global() 5 module:set_global()
5 6
6 local ports = module:get_option_array("stanza_counter_http_ports", {{ port = 5280 }}) 7 local base_path = module:get_option_array("stanza_counter_basepath", "/stanza-counter/")
7
8 local httpserver = require "net.httpserver"
9 8
10 -- http handlers 9 -- http handlers
11 10
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" 11 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"
13 12
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" 13 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"
15 14
16 local function res(code, r, h) 15 local function res(event, code, body, extras)
17 local response = { 16 local response = event.response
18 status = code,
19 body = r
20 }
21 17
22 if h then response.headers = h end 18 if extras then
23 return response 19 for header, data in pairs(extras) do response.headers[header] = data end
20 end
21
22 response.status_code = code
23 response:send(body)
24 end 24 end
25 25
26 local function req(method, body, request) 26 local function req(event)
27 if not prosody.stanza_counter then 27 if not prosody.stanza_counter then
28 local err500 = r_err:format("500", "Stats not found, is the counter module loaded?") 28 local err500 = r_err:format(event, 500, "Stats not found, is the counter module loaded?")
29 return res(500, err500) end 29 return res(500, err500) end
30 if method == "GET" then 30 if method == "GET" then
31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], 31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
32 prosody.stanza_counter.iq["outgoing"], 32 prosody.stanza_counter.iq["outgoing"],
33 prosody.stanza_counter.message["incoming"], 33 prosody.stanza_counter.message["incoming"],
34 prosody.stanza_counter.message["outgoing"], 34 prosody.stanza_counter.message["outgoing"],
35 prosody.stanza_counter.presence["incoming"], 35 prosody.stanza_counter.presence["incoming"],
36 prosody.stanza_counter.presence["outgoing"]) 36 prosody.stanza_counter.presence["outgoing"])
37 return res(200, forge_res) 37 return res(event, 200, forge_res)
38 else 38 else
39 local err405 = r_err:format("405", "Only GET is supported") 39 local err405 = r_err:format(405, "Only GET is supported")
40 return res(405, err405, {["Allow"] = "GET"}) 40 return res(event, 405, err405, {["Allow"] = "GET"})
41 end 41 end
42 end 42 end
43 43
44 -- initialization. 44 -- initialization.
45 -- init http and cleanup interface
46 45
47 local function setup() 46 module:provides("http", {
48 httpserver.new_from_config(ports, req, { base = "stanza-counter" }) 47 default_path = base_path,
49 end 48 route = {
50 49 ["GET /"] = req,
51 -- set it 50 ["POST /"] = req
52 if prosody.start_time then setup() else module:hook("server-started", setup) end 51 }
52 })