// Copyright 2014, Kevin Ko <kevin@faveset.com>

package com.faveset.khttpserver;

import java.io.IOException;
import sun.misc.Signal;
import sun.misc.SignalHandler;

import com.faveset.khttp.HttpHandler;
import com.faveset.khttp.HttpRequest;
import com.faveset.khttp.HttpResponseWriter;

import com.faveset.log.OutputStreamLog;

public class HttpServer {
    public static void main(String[] args) throws IOException {
        if (args.length < 1) {
            System.out.println("<port>");
            return;
        }

        int port = new Integer(args[0]);

        OutputStreamLog log = new OutputStreamLog(System.out);

        final com.faveset.khttp.HttpServer server = new com.faveset.khttp.HttpServer();
        server.setLog(log);

        // Set up the signal handler for exiting the server.
        Signal.handle(new Signal("INT"), new SignalHandler() {
            @Override
            public void handle(Signal sig) {
                server.stop();
            }
        });

        // Register handlers.

        server.registerHandler("/", new HttpHandler() {
            public void onRequest(HttpRequest req, HttpResponseWriter w) {
                w.getHeadersBuilder().set("Content-Type", "text/html");
                w.write("<html><body>Hello</body></html>");
            }
        });
        server.listenAndServe("::", port);

        log.close();
    }
}

@@ -305,418 +305,956 @@
 set.
-log.OutputStreamLog;%0A%0Apublic class HttpServer %7B%0A    public static void main(String%5B%5D args) throws IOException %7B%0A        if (args.length %3C 1) %7B%0A            System.out.println(%22%3Cport%3E%22);%0A            return;%0A        %7D%0A%0A        int port = new Integer(args%5B0%5D);%0A%0A        OutputStreamLog log = new OutputStreamLog(System.out);%0A%0A        final com.faveset.khttp.HttpServer server = new com.faveset.khttp.HttpServer();%0A
+flags.BoolFlag;%0Aimport com.faveset.flags.Flags;%0Aimport com.faveset.log.OutputStreamLog;%0A%0Apublic class HttpServer %7B%0A    private static BoolFlag mLogFlag =%0A        Flags.registerBool(%22log%22, false, %22enable logging to stdout%22);%0A%0A    public static void main(String%5B%5D args) throws IOException, IllegalArgumentException %7B%0A        Flags.parse(args);%0A%0A        Flags flags = Flags.get();%0A        if (flags.getArgSize() %3C 1) %7B%0A            StringBuilder builder = new StringBuilder();%0A            builder.append(%22%3Cport%3E%5Cn%22);%0A            flags.writeHelp(builder);%0A%0A            System.out.println(builder.toString());%0A            return;%0A        %7D%0A%0A        int port = new Integer(flags.getArg(0));%0A%0A        final com.faveset.khttp.HttpServer server = new com.faveset.khttp.HttpServer();%0A%0A        OutputStreamLog log = null;%0A        if (mLogFlag.get()) %7B%0A            System.out.println(%22logging to stdout%22);%0A            log = new OutputStreamLog(System.out);%0A    
     
@@ -1276,16 +1276,26 @@
 og(log);
+%0A        %7D
 %0A%0A      
@@ -1897,24 +1897,55 @@
 :%22, port);%0A%0A
+        if (log != null) %7B%0A    
         log.
@@ -1949,20 +1949,30 @@
 og.close();%0A
+        %7D%0A
     %7D%0A%7D%0A

Update to use flags for logging.