Libraryless. Click here for Pure Java version (3247L/24K/64K).
1 | !747 |
2 | |
3 | m {
|
4 | static L<S> whiteList = litlist("#1001782", "#1001785", "#1001861");
|
5 | static new Map<S, Class> bots; |
6 | static long hitCount; |
7 | |
8 | p {
|
9 | readLocally("hitCount");
|
10 | serveHttp(80); |
11 | } |
12 | |
13 | static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method, |
14 | Map<S,S> header, Map<S,S> parms, Map<S,S> files) {
|
15 | print("Serving HTTP " + quote(uri));
|
16 | ++hitCount; |
17 | saveLocally("hitCount");
|
18 | uri = dropPrefixMandatory("/", uri);
|
19 | if (eq(uri, "")) |
20 | ret serveHomePage(); |
21 | |
22 | int i = uri.indexOf('/');
|
23 | S firstPart = i >= 0 ? uri.substring(0, i) : uri; |
24 | S rest = i >= 0 ? substr(uri, i) : "/"; // rest always starts with "/" |
25 | ret serveBot(firstPart, rest); |
26 | } |
27 | |
28 | static NanoHTTPD.Response serveBot(S botID, S subUri) {
|
29 | if (isSnippetID(botID)) {
|
30 | botID = formatSnippetID(botID); |
31 | if (!containsSnippetID(whiteList, botID)) |
32 | ret serve404("Bot not white listed: " + botID);
|
33 | |
34 | if (eq(subUri, "reload")) {
|
35 | unloadBot(botID); |
36 | ret serveHTML("OK, bot unloaded.");
|
37 | } |
38 | //ret serveHTML("Calling bot: " + botID + " on: " + subUri);
|
39 | Class bot = loadBot(botID); |
40 | S s = (S) callOpt(bot, "html", subUri); |
41 | if (s == null) |
42 | s = (S) callOpt(bot, "html"); |
43 | if (s != null) |
44 | ret serveHTML(s); |
45 | } |
46 | |
47 | ret serve404(); |
48 | } |
49 | |
50 | static NanoHTTPD.Response serveHomePage() {
|
51 | new StringBuilder buf; |
52 | buf.append("<html>");
|
53 | buf.append("<head><title>TinyBrain Bots</title></head>");
|
54 | buf.append("<body>");
|
55 | buf.append("<h3><a href=" + htmlQuote("http://tinybrain.de") + ">TinyBrain</a> Bots</h3>");
|
56 | buf.append("<ul>");
|
57 | for (S botID : whiteList) {
|
58 | botID = formatSnippetID(botID); |
59 | boolean loaded = isBotLoaded(botID); |
60 | S parsedID = "" + parseSnippetID(botID); |
61 | buf.append("<li><a href=" + htmlQuote(parsedID) + ">" + botID + "</a> (");
|
62 | if (loaded) buf.append("loaded, ");
|
63 | buf.append("<a href=" + htmlQuote("http://tinybrain.de/" + parsedID) + ">source</a>)</li>");
|
64 | } |
65 | buf.append("</ul>");
|
66 | buf.append("<p>Hit count: " + hitCount + "</p>");
|
67 | buf.append("</body>");
|
68 | buf.append("</html>");
|
69 | ret serveHTML(buf); |
70 | } |
71 | |
72 | static synchronized Class loadBot(S botID) {
|
73 | botID = formatSnippetID(botID); |
74 | Class c = bots.get(botID); |
75 | if (c == null) {
|
76 | c = hotwire(botID); |
77 | callMain(c); |
78 | bots.put(botID, c); |
79 | } |
80 | ret c; |
81 | } |
82 | |
83 | static synchronized void unloadBot(S botID) {
|
84 | botID = formatSnippetID(botID); |
85 | // TODO: program clean-up |
86 | bots.remove(botID); |
87 | } |
88 | |
89 | static synchronized boolean isBotLoaded(S botID) {
|
90 | botID = formatSnippetID(botID); |
91 | ret bots.containsKey(botID); |
92 | } |
93 | } |
Began life as a copy of #1001784
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1001849 |
| Snippet name: | Web server for all HTML bots (port 80, with white list, LIVE) |
| Eternal ID of this version: | #1001849/1 |
| Text MD5: | 4b0ec6580483942ff5528dc9873b09ac |
| Transpilation MD5: | 2640a574dc7ce386362516cfbcca6f5d |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-11-27 20:51:48 |
| Source code size: | 2799 bytes / 93 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 974 / 1105 |
| Referenced in: | [show references] |