Annotate

mod_candy/mod_candy.lua @ 2440:7814a5c7fee8

mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
author Kim Alvefur <zash@zash.se>
date Tue, 10 Jan 2017 14:11:56 +0100
parent 1864:991a5f74f848
child 2449:c9372cfac3b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_candy.lua
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
2 -- Copyright (C) 2013-2017 Kim Alvefur
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local json_encode = require"util.json".encode;
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
5 local is_module_loaded = require "core.modulemanager".is_loaded;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local serve = module:depends"http_files".serve;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 module:provides("http", {
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 route = {
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 ["GET /prosody.js"] = function(event)
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 event.response.headers.content_type = "text/javascript";
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
13 local connect_path;
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
14 if is_module_loaded(module.host, "websocket") then
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
15 connect_path = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws");
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
16 else
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
17 if not is_module_loaded(module.host, "bosh") then
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
18 module:depends("bosh");
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
19 end
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
20 connect_path = module:http_url("bosh", "/http-bind");
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
21 end
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
22
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 return ("// Generated by Prosody\n"
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 .."var Prosody = %s;\n")
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 :format(json_encode({
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
26 connect_path = connect_path;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 version = prosody.version;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 host = module:get_host();
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 anonymous = module:get_option_string("authentication") == "anonymous";
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 }));
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 ["GET /*"] = serve(module:get_directory().."/www_files");
1384
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
33 GET = function(event)
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
34 event.response.headers.location = event.request.path.."/";
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
35 return 301;
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
36 end;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 }
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 });
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39