<commit_before>package com.progressoft.brix.domino.api.server;

import com.progressoft.brix.domino.api.server.config.HttpServerConfigurator;
import com.progressoft.brix.domino.api.server.config.ServerConfigurationLoader;
import com.progressoft.brix.domino.api.server.config.VertxConfiguration;
import com.progressoft.brix.domino.api.server.entrypoint.VertxContext;
import com.progressoft.brix.domino.service.discovery.VertxServiceDiscovery;
import io.vertx.config.ConfigRetriever;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;

import java.nio.file.Paths;
import java.util.ServiceLoader;

import static java.util.Objects.nonNull;

public class DominoLoader {

    private static final Logger LOGGER = LoggerFactory.getLogger(DominoLoader.class);

    public static final int DEFAULT_PORT = 0;
    public static final String HTTP_PORT_KEY = "http.port";
    public static final int AROUND_6_MONTHS = 15768000;
    public static final String WEBROOT = "webroot";
    public static final int NOT_FOUND = 404;

    private final Vertx vertx;
    private final Router router;
    private final JsonObject config;


    public DominoLoader(Vertx vertx, Router router, JsonObject config) {
        this.vertx = vertx;
        this.router = router;
        this.config = config;
    }

    public void start() {
        start(event -> {
            if (event.succeeded())
                LOGGER.info("Server started on port : " + event.result().actualPort());
            else
                LOGGER.error("Failed to start server", event.cause());
        });
    }

    public void start(Handler<AsyncResult<HttpServer>> serverStartupHandler) {
        ImmutableHttpServerOptions immutableHttpServerOptions = new ImmutableHttpServerOptions();
        VertxContext vertxContext = initializeContext(immutableHttpServerOptions);

        Future<HttpServerOptions> future = Future.future();
        future.setHandler(
                options -> onHttpServerConfigurationCompleted(immutableHttpServerOptions, vertxContext, options,
                        serverStartupHandler));

        configureHttpServer(vertxContext, future);
    }

    private VertxContext initializeContext(ImmutableHttpServerOptions immutableHttpServerOptions) {
        return VertxContext.VertxContextBuilder.vertx(vertx)
                .router(router)
                .serverConfiguration(new VertxConfiguration(config))
                .httpServerOptions(immutableHttpServerOptions)
                .vertxServiceDiscovery(new VertxServiceDiscovery(vertx))
                .configRetriever(ConfigRetriever.create(vertx)).build();
    }

    private void onHttpServerConfigurationCompleted(ImmutableHttpServerOptions immutableHttpServerOptions,
                                                    VertxContext vertxContext, AsyncResult<HttpServerOptions> options,
                                                    Handler<AsyncResult<HttpServer>> serverStartupHandler) {
        immutableHttpServerOptions.init(options.result(), options.result().getPort(), options.result().getHost());

        new ServerConfigurationLoader(vertxContext).loadModules();

        initStaticResourceHandler();

        if (options.result().isSsl())
            addSecurityHeadersHandler(router);
        vertx.createHttpServer(options.result()).requestHandler(router::accept)
                .listen(options.result().getPort(), serverStartupHandler);
    }

    private void initStaticResourceHandler() {
        StaticHandler staticHandler = StaticHandler.create();
        if (nonNull(System.getProperty("domino.webroot.location"))) {
            staticHandler.setAllowRootFileSystemAccess(true);
            staticHandler.setWebRoot(getWebRoot());
        } else {
            staticHandler.setWebRoot(config.getString("webroot", "app"));
        }

        router.route("/").order(Integer.MAX_VALUE - 2)
                .handler(this::serveIndexPage);

        router.route("/static/*").order(Integer.MAX_VALUE-1)
                .handler(staticHandler)
                .failureHandler(this::serveResource);

        router.route("/*").order(Integer.MAX_VALUE)
                .handler(this::serveResource)
                .failureHandler(this::serveIndexPage);

    }

    private void serveResource(RoutingContext context) {
        context.response().sendFile(WEBROOT + context.request().path().replace("/static", ""), event -> {
            if (event.failed()) {
                context.fail(NOT_FOUND);
            }
        });
    }

    private String getWebRoot() {
        return Paths.get(System.getProperty("domino.webroot.location")).toAbsolutePath().toString();
    }

    private HttpServerResponse serveIndexPage(RoutingContext event) {<commit_msg>remove failureHandler from /* route<commit_after>package com.progressoft.brix.domino.api.server;

import com.progressoft.brix.domino.api.server.config.HttpServerConfigurator;
import com.progressoft.brix.domino.api.server.config.ServerConfigurationLoader;
import com.progressoft.brix.domino.api.server.config.VertxConfiguration;
import com.progressoft.brix.domino.api.server.entrypoint.VertxContext;
import com.progressoft.brix.domino.service.discovery.VertxServiceDiscovery;
import io.vertx.config.ConfigRetriever;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;

import java.nio.file.Paths;
import java.util.ServiceLoader;

import static java.util.Objects.nonNull;

public class DominoLoader {

    private static final Logger LOGGER = LoggerFactory.getLogger(DominoLoader.class);

    public static final int DEFAULT_PORT = 0;
    public static final String HTTP_PORT_KEY = "http.port";
    public static final int AROUND_6_MONTHS = 15768000;
    public static final String WEBROOT = "webroot";
    public static final int NOT_FOUND = 404;

    private final Vertx vertx;
    private final Router router;
    private final JsonObject config;


    public DominoLoader(Vertx vertx, Router router, JsonObject config) {
        this.vertx = vertx;
        this.router = router;
        this.config = config;
    }

    public void start() {
        start(event -> {
            if (event.succeeded())
                LOGGER.info("Server started on port : " + event.result().actualPort());
            else
                LOGGER.error("Failed to start server", event.cause());
        });
    }

    public void start(Handler<AsyncResult<HttpServer>> serverStartupHandler) {
        ImmutableHttpServerOptions immutableHttpServerOptions = new ImmutableHttpServerOptions();
        VertxContext vertxContext = initializeContext(immutableHttpServerOptions);

        Future<HttpServerOptions> future = Future.future();
        future.setHandler(
                options -> onHttpServerConfigurationCompleted(immutableHttpServerOptions, vertxContext, options,
                        serverStartupHandler));

        configureHttpServer(vertxContext, future);
    }

    private VertxContext initializeContext(ImmutableHttpServerOptions immutableHttpServerOptions) {
        return VertxContext.VertxContextBuilder.vertx(vertx)
                .router(router)
                .serverConfiguration(new VertxConfiguration(config))
                .httpServerOptions(immutableHttpServerOptions)
                .vertxServiceDiscovery(new VertxServiceDiscovery(vertx))
                .configRetriever(ConfigRetriever.create(vertx)).build();
    }

    private void onHttpServerConfigurationCompleted(ImmutableHttpServerOptions immutableHttpServerOptions,
                                                    VertxContext vertxContext, AsyncResult<HttpServerOptions> options,
                                                    Handler<AsyncResult<HttpServer>> serverStartupHandler) {
        immutableHttpServerOptions.init(options.result(), options.result().getPort(), options.result().getHost());

        new ServerConfigurationLoader(vertxContext).loadModules();

        initStaticResourceHandler();

        if (options.result().isSsl())
            addSecurityHeadersHandler(router);
        vertx.createHttpServer(options.result()).requestHandler(router::accept)
                .listen(options.result().getPort(), serverStartupHandler);
    }

    private void initStaticResourceHandler() {
        StaticHandler staticHandler = StaticHandler.create();
        if (nonNull(System.getProperty("domino.webroot.location"))) {
            staticHandler.setAllowRootFileSystemAccess(true);
            staticHandler.setWebRoot(getWebRoot());
        } else {
            staticHandler.setWebRoot(config.getString("webroot", "app"));
        }

        router.route("/").order(Integer.MAX_VALUE - 2)
                .handler(this::serveIndexPage);

        router.route("/static/*").order(Integer.MAX_VALUE-1)
                .handler(staticHandler)
                .failureHandler(this::serveResource);

        router.route("/*").order(Integer.MAX_VALUE)
                .handler(this::serveResource);

    }

    private void serveResource(RoutingContext context) {
        context.response().sendFile(WEBROOT + context.request().path().replace("/static", ""), event -> {
            if (event.failed())
                serveIndexPage(context);
        });
    }

    private String getWebRoot() {
        return Paths.get(System.getProperty("domino.webroot.location")).toAbsolutePath().toString();
    }

    private HttpServerResponse serveIndexPage(RoutingContext event) {
{'fraction_non_alphanumeric': 0.10200506131983648, 'fraction_numerical': 0.0029199922133540976, 'mean_word_length': 3.1248494580489763, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144423', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2694, 'n_words': 454}