Libraryless. Click here for Pure Java version (3247L/24K/64K).
!747 m { static L<S> whiteList = litlist("#1001782", "#1001785", "#1001861"); static new Map<S, Class> bots; static long hitCount; p { readLocally("hitCount"); serveHttp(80); } static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method, Map<S,S> header, Map<S,S> parms, Map<S,S> files) { print("Serving HTTP " + quote(uri)); ++hitCount; saveLocally("hitCount"); uri = dropPrefixMandatory("/", uri); if (eq(uri, "")) ret serveHomePage(); int i = uri.indexOf('/'); S firstPart = i >= 0 ? uri.substring(0, i) : uri; S rest = i >= 0 ? substr(uri, i) : "/"; // rest always starts with "/" ret serveBot(firstPart, rest); } static NanoHTTPD.Response serveBot(S botID, S subUri) { if (isSnippetID(botID)) { botID = formatSnippetID(botID); if (!containsSnippetID(whiteList, botID)) ret serve404("Bot not white listed: " + botID); if (eq(subUri, "reload")) { unloadBot(botID); ret serveHTML("OK, bot unloaded."); } //ret serveHTML("Calling bot: " + botID + " on: " + subUri); Class bot = loadBot(botID); S s = (S) callOpt(bot, "html", subUri); if (s == null) s = (S) callOpt(bot, "html"); if (s != null) ret serveHTML(s); } ret serve404(); } static NanoHTTPD.Response serveHomePage() { new StringBuilder buf; buf.append("<html>"); buf.append("<head><title>TinyBrain Bots</title></head>"); buf.append("<body>"); buf.append("<h3><a href=" + htmlQuote("http://tinybrain.de") + ">TinyBrain</a> Bots</h3>"); buf.append("<ul>"); for (S botID : whiteList) { botID = formatSnippetID(botID); boolean loaded = isBotLoaded(botID); S parsedID = "" + parseSnippetID(botID); buf.append("<li><a href=" + htmlQuote(parsedID) + ">" + botID + "</a> ("); if (loaded) buf.append("loaded, "); buf.append("<a href=" + htmlQuote("http://tinybrain.de/" + parsedID) + ">source</a>)</li>"); } buf.append("</ul>"); buf.append("<p>Hit count: " + hitCount + "</p>"); buf.append("</body>"); buf.append("</html>"); ret serveHTML(buf); } static synchronized Class loadBot(S botID) { botID = formatSnippetID(botID); Class c = bots.get(botID); if (c == null) { c = hotwire(botID); callMain(c); bots.put(botID, c); } ret c; } static synchronized void unloadBot(S botID) { botID = formatSnippetID(botID); // TODO: program clean-up bots.remove(botID); } static synchronized boolean isBotLoaded(S botID) { botID = formatSnippetID(botID); ret bots.containsKey(botID); } }
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