comparison mod_server_status/mod_server_status.lua @ 649:dfd7f1ed7782

mod_server_status: updated to current HTTP API.
author Marco Cirillo <maranda@lightwitch.org>
date Sun, 29 Apr 2012 13:06:26 +0000
parents edb4afb6a227
children 74bd7ebe5792
comparison
equal deleted inserted replaced
648:6f0e0d6790a7 649:dfd7f1ed7782
1 -- (C) 2011, Marco Cirillo (LW.Org) 1 -- (C) 2011, Marco Cirillo (LW.Org)
2 -- Display server stats in readable XML or JSON format 2 -- Display server stats in readable XML or JSON format
3 3
4 module:depends("http")
4 module:set_global() 5 module:set_global()
5 6
6 local ports = module:get_option_array("server_status_http_ports", {{ port = 5280 }}) 7 local base_path = module:get_option_array("server_status_basepath", "/server-status/")
7 local show_hosts = module:get_option_array("server_status_show_hosts", nil) 8 local show_hosts = module:get_option_array("server_status_show_hosts", nil)
8 local show_comps = module:get_option_array("server_status_show_comps", nil) 9 local show_comps = module:get_option_array("server_status_show_comps", nil)
9 local json_output = module:get_option_boolean("server_status_json", false) 10 local json_output = module:get_option_boolean("server_status_json", false)
10 11
11 local httpserver = require "net.httpserver"
12 local json_encode = require "util.json".encode 12 local json_encode = require "util.json".encode
13 13
14 -- code begin 14 -- code begin
15 15
16 if not prosody.stanza_counter and not show_hosts and not show_comps then 16 if not prosody.stanza_counter and not show_hosts and not show_comps then
17 module:log ("error", "mod_server_status requires at least one of the following things:") 17 module:log ("error", "mod_server_status requires at least one of the following things:")
18 module:log ("error", "mod_stanza_counter loaded, or either server_status_show_hosts or server_status_show_comps configuration values set.") 18 module:log ("error", "mod_stanza_counter loaded, or either server_status_show_hosts or server_status_show_comps configuration values set.")
19 module:log ("error", "check the module wiki at: http://code.google.com/p/prosody-modules/wiki/mod_server_status") 19 module:log ("error", "check the module wiki at: http://code.google.com/p/prosody-modules/wiki/mod_server_status")
20 return false 20 return false
21 end 21 end
22
23 local r_err = "\n<html>\n<head>\n<title>Prosody's Server Status - 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"
24 22
25 local response_table = {} 23 local response_table = {}
26 response_table.header = '<?xml version="1.0" encoding="UTF-8" ?>' 24 response_table.header = '<?xml version="1.0" encoding="UTF-8" ?>'
27 response_table.doc_header = '<document>' 25 response_table.doc_header = '<document>'
28 response_table.doc_closure = '</document>' 26 response_table.doc_closure = '</document>'
109 return json_encode(result) 107 return json_encode(result)
110 end 108 end
111 109
112 -- http handlers 110 -- http handlers
113 111
114 local function response(code, r, h) 112 local function request(event)
115 local response = { status = code, body = r } 113 local response = event.response
116 114 if not json_output then
117 if h then response.headers = h end 115 response.headers.content_type = "application/json"
118 return response 116 response:send(forge_response_xml())
119 end
120
121 local function request(method, body, request)
122 if method == "GET" then
123 if not json_output then return response(200, forge_response_xml()) else return response(200, forge_response_json()) end
124 else 117 else
125 local err405 = r_err:format("405", "Only GET is supported") 118 response.headers.content_type = "text/xml"
126 return response(405, err405, {["Allow"] = "GET"}) 119 response:send(forge_response_json())
127 end 120 end
128 end 121 end
129 122
130 -- initialization. 123 -- initialization.
131 -- init http interface
132 124
133 local function setup() 125 module:provides("http", {
134 httpserver.new_from_config(ports, request, { base = "server-status" }) 126 default_path = base_path,
135 end 127 route = {
128 ["GET /"] = request
129 }
130 })
136 131
137 if prosody.start_time then
138 setup()
139 else
140 module:hook("server-started", setup)
141 end