package cc.fascinated.bat; import cc.fascinated.bat.config.Config; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.service.EventService; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.mongock.runner.springboot.EnableMongock; import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.scheduling.annotation.EnableScheduling; import java.io.File; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Objects; @EnableScheduling @SpringBootApplication(scanBasePackages = "cc.fascinated.bat") @EnableMongock @Log4j2(topic = "Bat") public class BatApplication { public static Gson GSON = new GsonBuilder().create(); @SneakyThrows public static void main(@NonNull String[] args) { // Handle loading of our configuration file File config = new File("application.yml"); if (!config.exists()) { // Saving the default config if it doesn't exist locally Files.copy(Objects.requireNonNull(BatApplication.class.getResourceAsStream("/application.yml")), config.toPath(), StandardCopyOption.REPLACE_EXISTING); log.info("Saved the default configuration to '{}', please re-launch the application", // Log the default config being saved config.getAbsolutePath() ); return; } log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config // Start the application SpringApplication app = new SpringApplication(BatApplication.class); app.setRegisterShutdownHook(false); // Disable the default shutdown hook ConfigurableApplicationContext context = app.run(args); // Register the shutdown hook Runtime.getRuntime().addShutdownHook(new Thread(() -> { log.info("Shutting down..."); for (EventListener listener : EventService.LISTENERS) { listener.onShutdown(); } context.close(); })); log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT")); } }