Annotate

mod_candy/mod_candy.lua @ 2449:c9372cfac3b7

mod_candy: Break out connect path into a function
author Kim Alvefur <zash@zash.se>
date Fri, 20 Jan 2017 01:09:36 +0100
parent 2440:7814a5c7fee8
child 2450:36ffe9d11132
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
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
9 local function get_connect_path()
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
10 if is_module_loaded(module.host, "websocket") then
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
11 return module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
12 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
13 if not is_module_loaded(module.host, "bosh") then
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
14 module:depends("bosh");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
15 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
16 return module:http_url("bosh", "/http-bind");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
17 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
18
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
19 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
20 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
21 ["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
22 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
23
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
24 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
25 .."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
26 :format(json_encode({
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
27 connect_path = get_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
28 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
29 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
30 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
31 }));
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 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
33 ["GET /*"] = serve(module:get_directory().."/www_files");
1384
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
34 GET = function(event)
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
35 event.response.headers.location = event.request.path.."/";
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
36 return 301;
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
37 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
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 });
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40