package com.stubbornjava.examples.undertow.helloworld;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.undertow.Undertow;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;

public class HelloWorldServer {
    private static final Logger logger = LoggerFactory.getLogger(HelloWorldServer.class);

    // {{start:helloworld}}
    /*
     * Creating HttpHandlers as a method and passing it as a method reference is pretty clean.
     * This also helps reduce accidentally adding state to handlers.
     */
    public static void helloWorldHandler(HttpServerExchange exchange) {
        exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, "text/plain");
        exchange.getResponseSender().send("Hello World!");
    }

    public static void main(String[] args) {
        int port = 8080;
        /*
         *  "localhost" will ONLY listen on local host.
         *  If you want the server reachable from the outside you need to set "0.0.0.0"
         */
        String host = "localhost";

        /*
         * If you really want to abstract out the server construction you can.
         * StubbornJava recommends to only use abstractions / wrappers on very simple APIs.
         * When you abstract over something like an HTTP server its very difficult to have
         * an abstraction that covers the nuances of each webserver. Instead accept it and let
         * the framework bleed out a bit. Realistically you shouldn't be changing your web server
         * all that often. Even if you did fully abstract it out your custom logic tends to bleed
         * out eventually, embrace it and move on. You could also just have a static method or
         * factory that created the server with your common configurations and allows further
         * customization later.
         *
         * This web server has a single handler with no routing. All urls will respond the same way.
         */
        Undertow server = Undertow.builder()
            // Add the helloWorldHandler as a method reference.
            .addHttpListener(port, host, HelloWorldServer::helloWorldHandler)
            .build();
        logger.debug("starting on http://" + host + ":" + port);
        server.start();
    }
    // {{end:helloworld}}
}

@@ -1062,881 +1062,118 @@
   * 
-If you really want to abstract out the server construction you can.%0A         * StubbornJava recommends to only use abstractions / wrappers on very simple APIs.%0A         * When you abstract over something lik
+This web server has a singl
 e 
+h
 an
- HTTP server its very difficult to have%0A         * an abstraction that covers the nuances of each webserver. Instead accept it and let%0A         * the framework bleed out a bit. Realistically you shouldn't be changing your web server%0A         * all that often. Even if you did fully abstract it out your custom logic tends to bleed%0A         * out eventually, embrace it and move on. You could also just have a static method or%0A         * factory that created the server with your common configurations and allows further%0A         * customization later.%0A         *%0A         * This web server has a single handler with no routing. All urls will respond the same way
+dler with no routing.%0A         * ALL urls are handled by the helloWorldHandler
 .%0A  

Remove comments