Software /
code /
prosody-modules
Comparison
mod_stanza_counter/mod_stanza_counter_http.lua @ 436:e4a1f0425380
mod_stanza_counter: splitted plugin :/
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Tue, 20 Sep 2011 23:16:32 +0000 |
child | 437:78a2a6b2bea3 |
comparison
equal
deleted
inserted
replaced
435:b6abe463b4fc | 436:e4a1f0425380 |
---|---|
1 -- (C) 2011, Marco Cirillo (LW.Org) | |
2 -- Exposes stats on HTTP for the stanza counter module. | |
3 | |
4 module:set_global() | |
5 | |
6 local ports = module:get_option_array("stanza_counter_http_ports" or {{ port = 5280 }}) | |
7 | |
8 local httpserver = require "net.httpserver" | |
9 | |
10 -- http handlers | |
11 | |
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" | |
13 | |
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" | |
15 | |
16 local function res(code, r, h) | |
17 local response = { | |
18 status = code; | |
19 body = r; | |
20 } | |
21 | |
22 if h then response.headers = h; end | |
23 return response | |
24 end | |
25 | |
26 local function req(method, body, request) | |
27 if not prosody.stanza_counter then | |
28 local err500 = r_err:format("500", "Stats not found, is the counter module loaded?") | |
29 return res(500, err500) | |
30 if method == "GET" then | |
31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], | |
32 prosody.stanza_counter.iq["outgoing"], | |
33 prosody.stanza_counter.message["incoming"], | |
34 prosody.stanza_counter.message["outgoing"], | |
35 prosody.stanza_counter.presence["incoming"], | |
36 prosody.stanza_counter.presence["outgoing"]); | |
37 return res(200, forge_res) | |
38 else | |
39 local err405 = r_err:format("405", "Only GET is supported") | |
40 return res(405, err405, {["Allow"] = "GET"}) | |
41 end | |
42 end | |
43 | |
44 -- initialization. | |
45 -- init http interface | |
46 local function setup() | |
47 httpserver.new_from_config(ports, req, { base = "stanza-counter" }) | |
48 end | |
49 | |
50 -- hook server started | |
51 module:hook("server-started", setup) |