All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.noear.solon.health.HealthHandler Maven / Gradle / Ivy

There is a newer version: 3.0.0-M4
Show newest version
package org.noear.solon.health;

import org.noear.snack.ONode;
import org.noear.snack.core.Feature;
import org.noear.snack.core.Options;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;

/**
 * 检健检测代理(方便复用于别的检测路径)
 *
 * @author iYarnFog
 * @since 1.5
 */
public class HealthHandler implements Handler {

    /**
     * 参考代理路径
     */
    public static final String HANDLER_PATH = "/healthz";


    private static final HealthHandler instance = new HealthHandler();

    /**
     * 获取实例
     */
    public static HealthHandler getInstance() {
        return instance;
    }


    private static final Options options = Options.def().add(Feature.EnumUsingName);

    @Override
    public void handle(Context ctx) throws Throwable {
        HealthCheckResult result = HealthChecker.check();

        switch (result.getStatus()) {
            case DOWN:
                ctx.status(503);
                break;
            case ERROR:
                ctx.status(500);
                break;
            default:
                ctx.status(200);
        }

        ctx.outputAsJson(ONode.stringify(result, options));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy