Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1001849 // Web server for all HTML bots (port 80, with white list, LIVE)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

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);
  }
}

Author comment

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: 626 / 669
Referenced in: [show references]