Software /
code /
prosody-modules
Comparison
mod_jsxc/mod_jsxc.lua @ 4825:4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Copy of mod_conversejs adjusted for JSXC.
README TODO
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 15 Dec 2021 18:28:40 +0100 |
parent | 4209:mod_conversejs/mod_conversejs.lua@37aa50ed79c1 |
child | 4976:75b6e5df65f9 |
comparison
equal
deleted
inserted
replaced
4824:205b9d06fe6b | 4825:4bdfd83e091f |
---|---|
1 -- mod_jsxc | |
2 -- Copyright (C) 2021 Kim Alvefur | |
3 | |
4 local json_encode = require"util.json".encode; | |
5 local xml_escape = require "util.stanza".xml_escape; | |
6 local render = require "util.interpolation".new("%b{}", xml_escape, { json = json_encode }); | |
7 | |
8 module:depends"http"; | |
9 module:depends"bosh"; | |
10 module:depends"http_libjs"; | |
11 | |
12 local jquery_url = module:get_option_string("jquery_url", "/share/jquery/jquery.min.js"); | |
13 | |
14 local cdn_url = module:get_option_string("jsxc_cdn", ""); | |
15 | |
16 local version = module:get_option_string("jsxc_version", ""); | |
17 if version ~= "" then version = "/" .. version end | |
18 | |
19 local serve_dist = nil; | |
20 local resources = module:get_option_path("jsxc_resources"); | |
21 if resources then | |
22 local serve; | |
23 if not pcall(function() | |
24 -- Prosody >= trunk / 0.12 | |
25 local http_files = require "net.http.files"; | |
26 serve = http_files.serve; | |
27 end) then | |
28 -- Prosody <= 0.11 | |
29 serve = module:depends "http_files".serve; | |
30 end | |
31 local mime_map = module:shared("/*/http_files/mime").types or { css = "text/css", js = "application/javascript" }; | |
32 serve_dist = serve({ path = resources, mime_map = mime_map }); | |
33 | |
34 cdn_url = module:http_url(); | |
35 end | |
36 | |
37 local js_url = module:get_option_string("jsxc_script", cdn_url..version.."/dist/jsxc.bundle.js"); | |
38 local css_url = module:get_option_string("jsxc_css", cdn_url..version.."/dist/styles/jsxc.bundle.css"); | |
39 | |
40 local html_template; | |
41 | |
42 do | |
43 local template_filename = module:get_option_string(module.name .. "_html_template", "templates/template.html"); | |
44 local template_file, err = module:load_resource(template_filename); | |
45 if template_file then | |
46 html_template, err = template_file:read("*a"); | |
47 template_file:close(); | |
48 end | |
49 if not html_template then | |
50 module:log("error", "Error loading HTML template: %s", err); | |
51 html_template = render("<h1>mod_{module} could not read the template</h1>\ | |
52 <p>Tried to open <b>{filename}</b></p>\ | |
53 <pre>{error}</pre>", | |
54 { module = module.name, filename = template_filename, error = err }); | |
55 end | |
56 end | |
57 | |
58 local js_template; | |
59 do | |
60 local template_filename = module:get_option_string(module.name .. "_js_template", "templates/template.js"); | |
61 local template_file, err = module:load_resource(template_filename); | |
62 if template_file then | |
63 js_template, err = template_file:read("*a"); | |
64 template_file:close(); | |
65 end | |
66 if not js_template then | |
67 module:log("error", "Error loading JS template: %s", err); | |
68 js_template = render("console.log(\"mod_{module} could not read the JS template: %s\", {error|json})", | |
69 { module = module.name, filename = template_filename, error = err }); | |
70 end | |
71 end | |
72 | |
73 local function get_jsxc_options() | |
74 return { xmpp = { url = module:http_url("bosh", "/http-bind"), domain = module.host } }; | |
75 end | |
76 | |
77 local add_tags = module:get_option_array("jsxc_tags", {}); | |
78 | |
79 module:provides("http", { | |
80 title = "jsxc.js"; | |
81 route = { | |
82 GET = function (event) | |
83 local jsxc_options = get_jsxc_options(); | |
84 | |
85 event.response.headers.content_type = "text/html"; | |
86 return render(html_template, { | |
87 service_name = module:get_option_string("name"); | |
88 header_scripts = { jquery_url, js_url }; | |
89 header_style = { css_url }; | |
90 header_tags = add_tags; | |
91 jsxcjs = { | |
92 options = jsxc_options; | |
93 startup = { script = js_template:format(json_encode(jsxc_options)); } | |
94 }; | |
95 }); | |
96 end; | |
97 | |
98 ["GET /prosody-jsxc.js"] = function (event) | |
99 local jsxc_options = get_jsxc_options(); | |
100 | |
101 event.response.headers.content_type = "application/javascript"; | |
102 return js_template:format(json_encode(jsxc_options)); | |
103 end; | |
104 ["GET /dist/*"] = serve_dist; | |
105 } | |
106 }); | |
107 |