view mod_rest/apidemo.lib.lua @ 4523:6e20e6bf96f0

mod_rest: Add XEP-0066 to mapping schema (breaking from previous jsonmap) before: oob_url: http://example.com/ after: oob: url: http://example.com/ desc: optional description Deals with the fact that the name and namespace differs based on whether it's in an iq or message stanza, unlike oob_url
author Kim Alvefur <zash@zash.se>
date Mon, 22 Mar 2021 23:14:25 +0100
parents 1776831d0fab
children fd15e7f00ff5
line wrap: on
line source


local _M = {};

local api_demo = module:get_option_path("rest_demo_resources", nil);
local http_files = require "net.http.files";

local mime_map = module:shared("/*/http_files/mime").types or {css = "text/css"; js = "application/javascript"};
_M.resources = http_files.serve({
		path = api_demo;
		mime_map = mime_map;
	});

local index do
	local f = assert(io.open(api_demo.."/index.html"), "'api_demo_resources' should point to the 'dist' directory");
	index = f:read("*a");
	f:close();

	-- SUCH HACK, VERY GSUB, WOW!
	index = index:gsub("(%s?url%s*:%s*)%b\"\"", string.format("%%1%q", module:http_url().."/demo/openapi.yaml"), 1);
end

do
	local f = module:load_resource("openapi.yaml");
	_M.schema = {
		headers = {
			content_type = "text/x-yaml";
		};
		body = f:read("*a");
	}
	f:close();
end

_M.redirect = {
	status_code = 303;
	headers = {
		location = module:http_url().."/demo/";
	};
};

_M.main_page = {
	headers = {
		content_type = "text/html";
	};
	body = index;
}

return _M