package leigh.esp.kabe.http;

import leigh.esp.kabe.KabeServer;
import leigh.mecha.fabric.HandlerStatus;
import leigh.mecha.http.server.PrefixedHandler;
import leigh.mecha.http.server.WebTransaction;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.VelocityWatch;

import java.util.Set;

public class Link23Door extends PrefixedHandler {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(Link23Door.class);
    private final KabeServer engine;
    private final VelocityWatch vw = new VelocityWatch(logger);

    public Link23Door(Set<String> pathPrefixes, KabeServer kabe) {
        super(pathPrefixes);
        this.engine = kabe;
    }

    @Override
    public HandlerStatus handlePrefixedWebRequest(WebTransaction request) throws Throwable {
        vw.event("link32_txn");
        try {
            engine.handle(request);
            return HandlerStatus.BREAK;
        } catch (Exception e) {
            logger.warn("Request failed: {}", e);
            throw e;
        }
    }
}
