Search in sources :

Example 1 with ConfigParser

use of runwar.options.ConfigParser in project runwar by cfmlprojects.

the class Start method main.

public static void main(String[] args) throws Exception {
    System.setProperty("log4j.configuration", "log4j.filelog.xml");
    ServerOptions serverOptions = CommandLineHandler.parseLogArguments(args);
    LoggerFactory.init(serverOptions);
    if (args.length == 0) {
        if (new File("server.json").exists()) {
            serverOptions = new ConfigParser(new File("server.json")).getServerOptions();
        } else {
            // print usage
            serverOptions = CommandLineHandler.parseArguments(args);
        }
    } else {
        serverOptions = CommandLineHandler.parseArguments(args);
    }
    if (serverOptions.getLoadBalance() != null && serverOptions.getLoadBalance().length > 0) {
        final List<String> balanceHosts = new ArrayList<String>();
        RunwarLogger.LOG.info("Initializing...");
        final LoadBalancingProxyClient loadBalancer = new LoadBalancingProxyClient();
        for (String balanceHost : serverOptions.getLoadBalance()) {
            if (serverOptions.getWarFile() != null) {
                RunwarLogger.LOG.info("Starting instance of " + serverOptions.getWarFile().getParent() + "...");
                launchServer(balanceHost, serverOptions);
            }
            loadBalancer.addHost(new URI(balanceHost));
            balanceHosts.add(balanceHost);
            RunwarLogger.LOG.info("Added balanced host: " + balanceHost);
            Thread.sleep(3000);
        }
        int port = serverOptions.getPortNumber();
        loadBalancer.setConnectionsPerThread(20);
        RunwarLogger.LOG.info("Hosts loaded");
        RunwarLogger.LOG.info("Starting load balancer on 127.0.0.1 port " + port + "...");
        ProxyHandler proxyHandler = ProxyHandler.builder().setProxyClient(loadBalancer).setMaxRequestTime(30000).setNext(ResponseCodeHandler.HANDLE_404).build();
        Undertow reverseProxy = Undertow.builder().addHttpListener(port, "localhost").setIoThreads(4).setHandler(proxyHandler).build();
        reverseProxy.start();
        RunwarLogger.LOG.info("View balancer admin on http://127.0.0.1:9080");
        Undertow adminServer = Undertow.builder().addHttpListener(9080, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
                if (exchange.getQueryParameters().get("addHost") != null) {
                    String balanceHost = URLDecoder.decode(exchange.getQueryParameters().get("addHost").toString(), "UTF-8");
                    balanceHost = balanceHost.replaceAll("]|\\[", "");
                    loadBalancer.addHost(new URI(balanceHost));
                    balanceHosts.add(balanceHost);
                }
                if (exchange.getQueryParameters().get("removeHost") != null) {
                    String balanceHost = URLDecoder.decode(exchange.getQueryParameters().get("removeHost").toString(), "UTF-8");
                    balanceHost = balanceHost.replaceAll("]|\\[", "");
                    loadBalancer.removeHost(new URI(balanceHost));
                    balanceHosts.remove(balanceHost);
                }
                String response = "";
                // response += URLDecoder.decode(exchange.getQueryString(),"UTF-8");
                for (String balanceHost : balanceHosts) {
                    String[] schemeHostAndPort = balanceHost.split(":");
                    String host = schemeHostAndPort[1].replaceAll("^//", "");
                    int port = schemeHostAndPort.length > 2 ? Integer.parseInt(schemeHostAndPort[2]) : 80;
                    response += balanceHost + " <a href='?removeHost=" + balanceHost + "'>remove</a> Listening: " + serverListening(host, port) + "<br/>";
                }
                exchange.getResponseSender().send("<h3>Balanced Hosts</h3><form action='?'> Add Host:<input type='text' name='addHost' placeholder='http://127.0.0.1:7070'><input type='submit'></form>" + response);
            }
        }).build();
        adminServer.start();
        RunwarLogger.LOG.info("Started load balancer.");
    } else {
        Server server = new Server();
        try {
            server.startServer(serverOptions);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) ArrayList(java.util.ArrayList) ConfigParser(runwar.options.ConfigParser) URI(java.net.URI) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient) HttpServerExchange(io.undertow.server.HttpServerExchange) ServerOptions(runwar.options.ServerOptions) File(java.io.File) Undertow(io.undertow.Undertow)

Aggregations

Undertow (io.undertow.Undertow)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)1 ProxyHandler (io.undertow.server.handlers.proxy.ProxyHandler)1 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ConfigParser (runwar.options.ConfigParser)1 ServerOptions (runwar.options.ServerOptions)1