annotate mod_http_index/mod_http_index.lua @ 1825:1b5c817cb642

mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
author Kim Alvefur <zash@zash.se>
date Fri, 04 Sep 2015 00:10:51 +0200
parents 0d8cc6971cdb
children ae2235d6c3f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local url = require"socket.url";
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
2 local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape);
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 module:depends"http";
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- TODO Move templates into files
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
7 local base_template = [[
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 <!DOCTYPE html>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 <html>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 <head>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 <meta charset="utf-8">
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 <meta name="viewport" content="width=device-width, initial-scale=1">
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 <meta name="generator" value="prosody/{prosody_version} mod_{mod_name}">
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 <link rel="canonical" href="{canonical}">
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 <title>{title}</title>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 <style>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 body{background-color:#eeeeec;margin:1ex 0;padding-bottom:3em;font-family:Arial,Helvetica,sans-serif;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 header,footer{margin:1ex 1em;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 footer{font-size:smaller;color:#babdb6;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 .content{background-color:white;padding:1em;list-style-position:inside;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 nav{font-size:large;margin:1ex 1ex;clear:both;line-height:1.5em;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 nav a{padding: 1ex;text-decoration:none;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 nav a[rel="up"]{font-size:smaller;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 nav a[rel="prev"]{float:left;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 nav a[rel="next"]{float:right;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 nav a[rel="next::after"]{content:" →";}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 nav a[rel="prev::before"]{content:"← ";}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 nav a:empty::after,nav a:empty::before{content:""}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 @media screen and (min-width: 460px) {
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 nav{font-size:x-large;margin:1ex 1em;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 }
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 a:link,a:visited{color:#2e3436;text-decoration:none;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 a:link:hover,a:visited:hover{color:#3465a4;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 ul,ol{padding:0;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 li{list-style:none;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 hr{visibility:hidden;clear:both;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 br{clear:both;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 li time{float:right;font-size:small;opacity:0.2;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 li:hover time{opacity:1;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 .room-list .description{font-size:smaller;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 q.body::before,q.body::after{content:"";}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 .presence .verb{font-style:normal;color:#30c030;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 .presence.unavailable .verb{color:#c03030;}
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 </style>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 </head>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 <body>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 <header>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 <h1>{title}</h1>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 </header>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 <hr>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 <div class="content">
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
52 <nav>
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
53 <ul>{items#
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
54 <li><a href="{item.url}" title="{item.module}">{item.name}</a></li>}
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
55 </ul>
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
56 </nav>
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 </div>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 <hr>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 <footer>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 <br>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 <div class="powered-by">Prosody {prosody_version?}</div>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 </footer>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 </body>
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 </html>
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
65 ]];
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local canonical = module:http_url(nil, "/");
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 local function relative(base, link)
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 base = url.parse(base);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 link = url.parse(link);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for k,v in pairs(base) do
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if link[k] == v then
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 link[k] = nil;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 return url.build(link);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local function handler(event)
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
81 local host_items = module:get_host_items("http-provider");
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
82 local http_apps = {}
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
83 for _, item in ipairs(host_items) do
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 if module.name ~= item._provided_by then
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
85 table.insert(http_apps, {
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 name = item.name;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 module = "mod_" .. item._provided_by;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 url = relative(canonical, module:http_url(item.name, item.default_path));
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
89 });
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 event.response.headers.content_type = "text/html";
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
93 return render(base_template, {
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
94 title = "HTTP Apps";
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
95 items = http_apps;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
96 prosody_version = prosody.version;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
97 mod_name = module.name;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
98 canonical = canonical;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
99 });
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 module:provides("http", {
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 route = {
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 ["GET /"] = handler;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 };
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 default_path = "/";
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 });