diff mod_http_muc_log/mod_http_muc_log.lua @ 5104:d4b0a995e5e3

mod_http_muc_log: Move CSS and JS out of template This allows applying different cache and security policies to the static resources, while reducing the size of all pages.
author Kim Alvefur <zash@zash.se>
date Sun, 04 Dec 2022 22:10:07 +0100
parents f36d15107c15
children 7bce75e74f86
line wrap: on
line diff
--- a/mod_http_muc_log/mod_http_muc_log.lua	Fri Dec 02 22:06:40 2022 +0100
+++ b/mod_http_muc_log/mod_http_muc_log.lua	Sun Dec 04 22:10:07 2022 +0100
@@ -40,6 +40,8 @@
 	end
 end
 
+local resources = module:get_option_path(module.name .. "_resources", "static");
+
 -- local base_url = module:http_url() .. '/'; -- TODO: Generate links in a smart way
 local get_link do
 	local link, path = { path = '/' }, { "", "", is_directory = true };
@@ -248,6 +250,7 @@
 	response.headers.content_type = "text/html; charset=utf-8";
 	local room_obj = get_room(room);
 	return render(template, {
+		static = "../@static";
 		room = room_obj._data;
 		jid = room_obj.jid;
 		jid_node = jid_split(room_obj.jid);
@@ -467,6 +470,7 @@
 	response.headers.content_type = "text/html; charset=utf-8";
 	local room_obj = get_room(room);
 	return render(template, {
+		static = "../@static";
 		date = date;
 		room = room_obj._data;
 		jid = room_obj.jid;
@@ -517,6 +521,7 @@
 
 	response.headers.content_type = "text/html; charset=utf-8";
 	return render(template, {
+		static = "./@static";
 		title = module:get_option_string("name", "Prosody Chatrooms");
 		jid = module.host;
 		hide_presence = hide_presence(request);
@@ -526,6 +531,20 @@
 	});
 end
 
+local serve_static
+do
+	if prosody.process_type == "prosody" then
+		-- Prosody >= 0.12
+		local http_files = require "net.http.files";
+		serve = http_files.serve;
+	else
+		-- Prosody <= 0.11
+		serve = module:depends "http_files".serve;
+	end
+	local mime_map = module:shared("/*/http_files/mime").types or { css = "text/css"; js = "application/javascript" };
+	serve_static = serve({ path = resources; mime_map = mime_map });
+end
+
 module:provides("http", {
 	title = module:get_option_string("name", "Chatroom logs");
 	route = {
@@ -535,6 +554,10 @@
 		-- thus:
 		-- GET /room --> years_page (via logs_page)
 		-- GET /room/yyyy-mm-dd --> logs_page (for real)
+
+		["GET /@static/*"] = serve_static;
+		-- There are not many ASCII characters that are safe to use in URLs but not
+		-- valid in JID localparts, '@' seemed the only option.
 	};
 });