net.morimekta.tiny.http.TinyReadyHttpHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tiny-http Show documentation
Show all versions of tiny-http Show documentation
Smallest footprint of HTTP server helper library.
/*
* 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 net.morimekta.tiny.http;
import com.sun.net.httpserver.HttpExchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import static net.morimekta.tiny.http.TinyHealth.Status.ERROR;
import static net.morimekta.tiny.http.TinyHealthHttpHandler.MAPPER;
import static net.morimekta.tiny.http.TinyHttpStatus.SC_INTERNAL;
import static net.morimekta.tiny.http.TinyHttpStatus.SC_OK;
/**
* Simple HTTP handler for getting the readiness of the entire tiny server.
*/
public final class TinyReadyHttpHandler extends TinyHttpHandler {
private final static Logger LOGGER = LoggerFactory.getLogger(TinyReadyHttpHandler.class);
private final Map readyChecks;
/**
* @param readyChecks Map of readiness checks to be checked by the service.
*/
public TinyReadyHttpHandler(Map readyChecks) {
this.readyChecks = readyChecks;
}
@Override
protected void doGet(HttpExchange exchange) throws IOException {
var params = TinyHttpUtil.getExchangeQuery(exchange);
var out = new TreeMap();
var status = new AtomicInteger(SC_OK);
readyChecks.forEach((name, check) -> {
try {
var result = check.readyCheck();
if (result.getStatus() != TinyHealth.Status.OK) {
LOGGER.warn("Non OK result: {}", result);
status.set(SC_INTERNAL);
}
out.put(name, result);
} catch (Exception e) {
LOGGER.warn("Exception in readyCheck(): {}", e.getMessage(), e);
out.put(name,
new TinyHealth.Result(ERROR, e.getClass().getSimpleName() + ": " + e.getMessage()));
status.set(SC_INTERNAL);
}
});
byte[] data;
if (params.get("pretty") != null) {
data = (MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(out)
+ "\n").getBytes(StandardCharsets.UTF_8);
} else {
data = MAPPER.writeValueAsBytes(out);
}
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(status.get(), data.length);
try (var os = exchange.getResponseBody()) {
os.write(data);
}
}
}