Skip to content

Commit 7f7a8e0

Browse files
committed
feat: enhance CORS configuration to allow dynamic origins and improve health check commands in Docker configurations
1 parent d772073 commit 7f7a8e0

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
lines changed

api-gateway/src/main/java/com/example/apigateway/ApiGatewayApplication.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ public static void main(String[] args) {
2222
@Bean
2323
public CorsWebFilter corsWebFilter() {
2424
CorsConfiguration corsConfig = new CorsConfiguration();
25-
corsConfig.addAllowedOrigin("https://musicanalytics.netlify.app");
26-
corsConfig.addAllowedOrigin("https://music-analytics.abenezeranglo.uk");
27-
corsConfig.addAllowedOrigin("http://localhost:3000");
25+
26+
// Get allowed origins from environment or use defaults
27+
String allowedOrigins = System.getenv("CORS_ALLOWED_ORIGINS");
28+
if (allowedOrigins != null && !allowedOrigins.isEmpty()) {
29+
Arrays.stream(allowedOrigins.split(","))
30+
.forEach(corsConfig::addAllowedOrigin);
31+
} else {
32+
corsConfig.addAllowedOrigin("https://musicanalytics.netlify.app");
33+
corsConfig.addAllowedOrigin("https://music-analytics.abenezeranglo.uk");
34+
if (!isProduction()) {
35+
corsConfig.addAllowedOrigin("http://localhost:3000");
36+
}
37+
}
38+
2839
corsConfig.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"));
29-
corsConfig.setAllowedHeaders(Collections.singletonList("*"));
30-
corsConfig.setExposedHeaders(Collections.singletonList("*"));
40+
corsConfig.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "Origin"));
41+
corsConfig.setExposedHeaders(Arrays.asList("Authorization", "Content-Type"));
3142
corsConfig.setAllowCredentials(true);
3243
corsConfig.setMaxAge(3600L);
3344

@@ -36,4 +47,9 @@ public CorsWebFilter corsWebFilter() {
3647

3748
return new CorsWebFilter(source);
3849
}
50+
51+
private boolean isProduction() {
52+
String profile = System.getenv("SPRING_PROFILES_ACTIVE");
53+
return "production".equals(profile);
54+
}
3955
}

cloud-deploy/docker-compose.direct.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ services:
1414
- SPRING_CLOUD_SERVICE-REGISTRY_AUTO-REGISTRATION_ENABLED=false
1515
restart: unless-stopped
1616
healthcheck:
17-
test: ["CMD-SHELL", "wget -q --spider http://localhost:8761/actuator/health || exit 0"]
17+
test: ["CMD-SHELL", "curl -f http://localhost:8761/actuator/health || exit 1"]
1818
interval: 30s
1919
timeout: 10s
2020
retries: 5
@@ -37,6 +37,12 @@ services:
3737
eureka-server:
3838
condition: service_healthy
3939
restart: unless-stopped
40+
healthcheck:
41+
test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"]
42+
interval: 30s
43+
timeout: 10s
44+
retries: 3
45+
start_period: 60s
4046

4147
# Microservices depend on healthy Eureka server
4248
recommendation-service:
@@ -56,7 +62,7 @@ services:
5662
- recommendation-data:/data
5763
restart: unless-stopped
5864
healthcheck:
59-
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8082/actuator/health || exit 0"]
65+
test: ["CMD-SHELL", "curl -f http://localhost:8082/actuator/health || exit 1"]
6066
interval: 30s
6167
timeout: 10s
6268
retries: 3
@@ -79,7 +85,7 @@ services:
7985
- statistics-data:/data
8086
restart: unless-stopped
8187
healthcheck:
82-
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8083/actuator/health || exit 0"]
88+
test: ["CMD-SHELL", "curl -f http://localhost:8083/actuator/health || exit 1"]
8389
interval: 30s
8490
timeout: 10s
8591
retries: 3
@@ -102,7 +108,7 @@ services:
102108
- user-tracking-data:/data
103109
restart: unless-stopped
104110
healthcheck:
105-
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8084/actuator/health || exit 0"]
111+
test: ["CMD-SHELL", "curl -f http://localhost:8084/actuator/health || exit 1"]
106112
interval: 30s
107113
timeout: 10s
108114
retries: 3

docker-compose.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
- SPRING_PROFILES_ACTIVE=docker
1212
- SERVER_PORT=8761
1313
healthcheck:
14-
test: "wget -q --spider http://localhost:8761/actuator/health || exit 1"
14+
test: ["CMD-SHELL", "curl -f http://localhost:8761/actuator/health || exit 1"]
1515
interval: 10s
1616
timeout: 5s
1717
retries: 10
@@ -31,6 +31,12 @@ services:
3131
depends_on:
3232
eureka-server:
3333
condition: service_healthy
34+
healthcheck:
35+
test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"]
36+
interval: 15s
37+
timeout: 5s
38+
retries: 5
39+
start_period: 30s
3440

3541
# Microservices depend on healthy Eureka server
3642
recommendation-service:
@@ -47,6 +53,12 @@ services:
4753
condition: service_healthy
4854
volumes:
4955
- recommendation-data:/data
56+
healthcheck:
57+
test: ["CMD-SHELL", "curl -f http://localhost:8082/actuator/health || exit 1"]
58+
interval: 15s
59+
timeout: 5s
60+
retries: 5
61+
start_period: 40s
5062

5163
statistics-service:
5264
image: statistics-service:latest
@@ -62,6 +74,12 @@ services:
6274
condition: service_healthy
6375
volumes:
6476
- statistics-data:/data
77+
healthcheck:
78+
test: ["CMD-SHELL", "curl -f http://localhost:8083/actuator/health || exit 1"]
79+
interval: 15s
80+
timeout: 5s
81+
retries: 5
82+
start_period: 40s
6583

6684
user-tracking-service:
6785
image: user-tracking-service:latest
@@ -77,6 +95,12 @@ services:
7795
condition: service_healthy
7896
volumes:
7997
- user-tracking-data:/data
98+
healthcheck:
99+
test: ["CMD-SHELL", "curl -f http://localhost:8084/actuator/health || exit 1"]
100+
interval: 15s
101+
timeout: 5s
102+
retries: 5
103+
start_period: 40s
80104

81105
volumes:
82106
recommendation-data:

recommendation-service/src/main/java/com/example/recommendation/RecommendationServiceApplication.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.scheduling.annotation.EnableScheduling;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
66

77
@SpringBootApplication
8-
@EnableScheduling
8+
@EnableDiscoveryClient
9+
910
public class RecommendationServiceApplication {
1011

1112
public static void main(String[] args) {

user-tracking-service/pom.xml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,41 @@
55

66
<groupId>com.example</groupId>
77
<artifactId>user-tracking-service</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
8+
<version>1.0-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>3.0.0</version>
14+
<version>2.5.4</version>
1515
<relativePath/>
1616
</parent>
1717

1818
<properties>
19-
<java.version>17</java.version>
20-
<spring-cloud.version>2022.0.0</spring-cloud.version>
19+
<java.version>8</java.version>
20+
<spring-cloud.version>2020.0.3</spring-cloud.version>
2121
</properties>
2222

2323
<dependencies>
2424
<dependency>
2525
<groupId>org.springframework.boot</groupId>
2626
<artifactId>spring-boot-starter-web</artifactId>
27+
<version>2.5.4</version>
2728
</dependency>
2829
<dependency>
2930
<groupId>org.springframework.boot</groupId>
3031
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
<version>2.5.4</version>
3133
</dependency>
3234
<dependency>
3335
<groupId>org.springframework.boot</groupId>
3436
<artifactId>spring-boot-starter-actuator</artifactId>
37+
<version>2.5.4</version>
3538
</dependency>
3639
<dependency>
3740
<groupId>org.springframework.cloud</groupId>
3841
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
42+
<version>3.0.3</version>
3943
</dependency>
4044

4145
<!-- H2 Database -->
@@ -71,6 +75,14 @@
7175
<plugin>
7276
<groupId>org.springframework.boot</groupId>
7377
<artifactId>spring-boot-maven-plugin</artifactId>
78+
<version>2.5.4</version>
79+
<executions>
80+
<execution>
81+
<goals>
82+
<goal>repackage</goal>
83+
</goals>
84+
</execution>
85+
</executions>
7486
</plugin>
7587

7688
<!-- Maven JAR Plugin to ensure the JAR is created -->
@@ -86,6 +98,17 @@
8698
</archive>
8799
</configuration>
88100
</plugin>
101+
102+
<!-- Maven Compiler Plugin -->
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-compiler-plugin</artifactId>
106+
<version>3.8.1</version>
107+
<configuration>
108+
<source>8</source>
109+
<target>8</target>
110+
</configuration>
111+
</plugin>
89112
</plugins>
90113
</build>
91114
</project>

0 commit comments

Comments
 (0)