Software /
code /
prosody-modules
Comparison
mod_rest/apidemo.lib.lua @ 4488:eea62d30ae08
mod_rest: Add option for serving interactive openapi documentation
Swagger not included.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 02 Mar 2021 18:38:13 +0100 |
child | 4498:1776831d0fab |
comparison
equal
deleted
inserted
replaced
4487:f877a4d3770b | 4488:eea62d30ae08 |
---|---|
1 | |
2 local _M = {}; | |
3 | |
4 local api_demo = module:get_option_path("rest_demo_resources", nil); | |
5 local http_files = require "net.http.files"; | |
6 | |
7 local mime_map = module:shared("/*/http_files/mime").types or {css = "text/css"; js = "application/javascript"}; | |
8 _M.resources = http_files.serve({ | |
9 path = api_demo; | |
10 mime_map = mime_map; | |
11 }); | |
12 | |
13 local index do | |
14 local f = assert(io.open(api_demo.."/index.html"), "'api_demo_resources' should point to the 'dist' directory"); | |
15 index = f:read("*a"); | |
16 f:close(); | |
17 | |
18 -- SUCH HACK, VERY GSUB, WOW! | |
19 index = index:gsub("(%s?url%s*:%s*)%b\"\"", string.format("%%1%q", module:http_url().."/demo/openapi.yaml"), 1); | |
20 end | |
21 | |
22 do | |
23 local f = module:load_resource("openapi.yaml"); | |
24 _M.schema = { | |
25 body = f:read("*a"); | |
26 } | |
27 f:close(); | |
28 end | |
29 | |
30 _M.redirect = { | |
31 status_code = 303; | |
32 headers = { | |
33 location = module:http_url().."/demo/"; | |
34 }; | |
35 }; | |
36 | |
37 _M.main_page = { | |
38 headers = { | |
39 content_type = "text/html"; | |
40 }; | |
41 body = index; | |
42 } | |
43 | |
44 return _M |