package com.img.host;

import com.google.inject.Guice;
import com.google.inject.Inject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import spark.TemplateEngine;

import static spark.Spark.*;

@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class Application {

    private final Filters filters;
    private final TemplateEngine templateEngine;
    private final Routes routes;

    private void run() {
        port(8888);
        threadPool(10, 2, 30_000);
        initExceptionHandler((e) -> log.error("Error during initialization", e));

        before("/*", filters.beforeAll());

        get("/", routes.home(), templateEngine);
        post("/", routes.upload());
        get("/image/:id/thumbnail", routes.thumbnail());

        after("/*", filters.afterAll());
    }

    public static void main(final String... args) {
        Guice.createInjector(new ApplicationModule())
                .getInstance(Application.class)
                .run();
    }
}
