/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.thinker0.mesos;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;

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

import java.io.Closeable;
import java.io.IOException;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;

import com.sun.net.httpserver.HttpHandler;

/**
 * new HealthCheckServe(8080, 8081, () -> true);
 */
public class HealthCheckServe extends HTTPServer implements Closeable {
    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private final Set<BooleanSupplier> healthCheckers = new HashSet<>();
    private final Optional<Runnable> quit;
    private final Optional<Runnable> abort;
    private final String body;

    public HealthCheckServe() throws IOException {
        this(9990);
    }

    public HealthCheckServe(int port) throws IOException {
        this(port, "OK", () -> {
        });
    }

    public HealthCheckServe(int port, String ok) throws IOException {
        this(port, ok, () -> {
        });
    }

    public HealthCheckServe(int port, String ok, Runnable quit) throws IOException {
        this(port, ok, quit, () -> {
        });
    }

    public HealthCheckServe(int port, String ok, Runnable quit, Runnable abort) throws IOException {
        super(port, false);
        this.body = ok;
        this.quit = Optional.of(quit);
        this.abort = Optional.of(abort);
        logger.info("Starting health check server on port {}", port);
        HttpHandler mHandler = new HTTPMetricHandler(CollectorRegistry.defaultRegistry, null);
        server.createContext("/metrics", mHandler);
        server.createContext("/health", httpExchange -> {
            final Optional<BooleanSupplier> health = health();
            if (health.isPresent()) {
                httpExchange.sendResponseHeaders(500, 0);
                httpExchange.getResponseBody().write(health.get().toString().getBytes());
            } else {
                byte[] bodyBytes = body.getBytes();
                httpExchange.sendResponseHeaders(200, bodyBytes.length);
                httpExchange.getResponseBody().write(bodyBytes);
            }
            httpExchange.close();
        });
        server.createContext("/quitquitquit", httpExchange -> {
            httpExchange.sendResponseHeaders(200, 0);
            httpExchange.getResponseBody().write("OK".getBytes());
            httpExchange.close();
            quitQuitQuit();
        });
        server.createContext("/abortabortabort", httpExchange -> {
            httpExchange.sendResponseHeaders(200, 0);
            httpExchange.getResponseBody().write("OK".getBytes());
            httpExchange.close();
            abortAbortAbort();
        });
    }

    public HealthCheckServe addHealthCheck(BooleanSupplier checker) {
        healthCheckers.add(checker);
        return this;
    }

    public Optional<BooleanSupplier> health() {
        return healthCheckers.stream().parallel().filter(b -> !b.getAsBoolean()).findFirst();
    }

    public HealthCheckServe quitQuitQuit() {
        this.quit.ifPresent(Runnable::run);
        return this;
    }

    public HealthCheckServe abortAbortAbort() {
        this.abort.ifPresent(Runnable::run);
        return this;
    }
}

@@ -2496,16 +2496,17 @@
 /metrics
+2
 %22, mHand
@@ -2507,24 +2507,24 @@
  mHandler);%0A
-
         serv
@@ -3145,32 +3145,112 @@
 tpExchange -%3E %7B%0A
+            if (%22POST%22.equalsIgnoreCase(httpExchange.getRequestMethod())) %7B%0A    
             http
@@ -3279,32 +3279,36 @@
 eaders(200, 0);%0A
+    
             http
@@ -3350,32 +3350,195 @@
 K%22.getBytes());%0A
+                httpExchange.close();%0A                quitQuitQuit();%0A            %7D else %7B%0A                httpExchange.sendResponseHeaders(404, 0);%0A            %7D%0A
             http
@@ -3559,36 +3559,8 @@
 ();%0A
-            quitQuitQuit();%0A
     
@@ -3626,32 +3626,112 @@
 tpExchange -%3E %7B%0A
+            if (%22POST%22.equalsIgnoreCase(httpExchange.getRequestMethod())) %7B%0A    
             http
@@ -3760,32 +3760,36 @@
 eaders(200, 0);%0A
+    
             http
@@ -3855,57 +3855,154 @@
     
-httpExchange.close();%0A            abortAbortAbort
+    abortAbortAbort();%0A            %7D else %7B%0A                httpExchange.sendResponseHeaders(404, 0);%0A            %7D%0A            httpExchange.close
 ();%0A

Check method of Request