From 586539d810dff21e14ede67a3b3fceba3c311f8e Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 13 Apr 2024 14:07:54 +0100 Subject: [PATCH] move cors to the Config --- src/main/java/cc/fascinated/Main.java | 14 -------------- .../java/cc/fascinated/config/Config.java | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/cc/fascinated/Main.java b/src/main/java/cc/fascinated/Main.java index a55a529..7b5727e 100644 --- a/src/main/java/cc/fascinated/Main.java +++ b/src/main/java/cc/fascinated/Main.java @@ -40,18 +40,4 @@ public class Main { SpringApplication.run(Main.class, args); // Start the application } - - @Bean - public WebMvcConfigurer configureCors() { - return new WebMvcConfigurer() { - @Override - public void addCorsMappings(@NonNull CorsRegistry registry) { - // Allow all origins to access the API - registry.addMapping("/**") - .allowedOrigins("*") // Allow all origins - .allowedMethods("*") // Allow all methods - .allowedHeaders("*"); // Allow all headers - } - }; - } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/config/Config.java b/src/main/java/cc/fascinated/config/Config.java index e7af0d0..1a22ba0 100644 --- a/src/main/java/cc/fascinated/config/Config.java +++ b/src/main/java/cc/fascinated/config/Config.java @@ -2,11 +2,15 @@ package cc.fascinated.config; import jakarta.annotation.PostConstruct; import lombok.Getter; +import lombok.NonNull; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Getter @Log4j2 @Configuration @@ -27,8 +31,23 @@ public class Config { @PostConstruct public void onInitialize() { INSTANCE = this; + String environmentProperty = environment.getProperty("ENVIRONMENT", "development"); production = environmentProperty.equalsIgnoreCase("production"); // Set the production mode log.info("Server is running in {} mode", production ? "production" : "development"); } + + @Bean + public WebMvcConfigurer configureCors() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(@NonNull CorsRegistry registry) { + // Allow all origins to access the API + registry.addMapping("/**") + .allowedOrigins("*") // Allow all origins + .allowedMethods("*") // Allow all methods + .allowedHeaders("*"); // Allow all headers + } + }; + } } \ No newline at end of file