Skip to content

Commit bd52ace

Browse files
committed
- Renamed docker compose service app to api
- Added CORS config.
1 parent f57baf2 commit bd52ace

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

compose-dev.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ services:
55
restart: always
66
user: postgres
77
secrets:
8+
- db-user
89
- db-password
910
volumes:
1011
- db-data:/var/lib/postgresql/data
1112
env_file:
12-
- ./db/.env
13+
- .env
1314
ports:
1415
- "5432:5432"
1516
healthcheck:
@@ -32,7 +33,7 @@ services:
3233
SPRING_PROFILES_ACTIVE: docker
3334
MAVEN_OPTS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
3435
env_file:
35-
- ./app/.env
36+
- .env
3637
ports:
3738
- "8080:8080"
3839
- "5005:5005"
@@ -50,4 +51,6 @@ volumes:
5051
db-data:
5152
secrets:
5253
db-password:
53-
file: db/password.txt
54+
file: secrets/db-password.txt
55+
db-user:
56+
file: ./secrets/db-user.txt

compose.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ services:
188188

189189
# Dependencies
190190
depends_on:
191-
app:
191+
api:
192192
condition: service_started # Changed from service_healthy because it doesn't work properly with multiple replicas
193193

194194
# Healthcheck
@@ -227,7 +227,7 @@ services:
227227
# ==========================================================================
228228
# SPRING-BOOT APPLICATION
229229
# ==========================================================================
230-
app:
230+
api:
231231
build:
232232
context: .
233233
dockerfile: Dockerfile
@@ -251,6 +251,10 @@ services:
251251
networks:
252252
- backend
253253

254+
# Ports: DO NOT expose externally in production
255+
# ports:
256+
# - "8080:8080" # ❌ For debug only, remove in production
257+
254258
# Ports (not externally exposed, only through nginx)
255259
expose:
256260
- "8080"
@@ -305,10 +309,10 @@ services:
305309

306310
# Labels
307311
labels:
308-
service: "app"
312+
service: "api"
309313
tier: "backend"
310314
environment: "${ENVIRONMENT:-production}"
311-
version: "${APP_VERSION:-1.0.0}"
315+
version: "${API_VERSION:-1.0.0}"
312316

313317
# ==========================================================================
314318
# POSTGRESQL - Database

nginx/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ http {
8585
# 3 replicas of the app service
8686
# Docker Compose automatically creates DNS round-robin for "app"
8787
# but defining it explicitly gives better control
88-
server app:8080 max_fails=3 fail_timeout=30s weight=1;
88+
server api:8080 max_fails=3 fail_timeout=30s weight=1;
8989

9090
# NOTE: With deploy.replicas=2, Docker resolves "app" to the 2 instances
9191
# via internal DNS round-robin. Each request to "app:8080"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mcqueide.dockertest.infrastructure.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.cors.CorsConfiguration;
7+
import org.springframework.web.cors.CorsConfigurationSource;
8+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
@Configuration
15+
public class CorsConfig {
16+
17+
@Value("${cors.allowed-origins}")
18+
private String allowedOrigins;
19+
20+
@Value("${cors.allow-credentials}")
21+
private boolean allowCredentials;
22+
23+
@Bean
24+
public WebMvcConfigurer corsConfigurer() {
25+
return new WebMvcConfigurer() {
26+
@Override
27+
public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
28+
registry.addMapping("/**")
29+
.allowedOrigins(allowedOrigins.split(","))
30+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
31+
.allowedHeaders("*")
32+
.allowCredentials(allowCredentials)
33+
.maxAge(3600);
34+
}
35+
};
36+
}
37+
38+
}

src/main/resources/application.yml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
spring:
22
application:
33
name: people-api
4+
45
datasource:
5-
url: jdbc:postgresql://localhost:5432/people
6-
username: people
7-
password: people
6+
url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/people}
7+
username: ${SPRING_DATASOURCE_USERNAME:people}
8+
password: ${SPRING_DATASOURCE_PASSWORD:people}
9+
810
jpa:
911
hibernate:
1012
ddl-auto: validate
13+
show-sql: false
1114
properties:
1215
hibernate.format_sql: true
1316
hibernate.jdbc.time_zone: UTC
17+
1418
flyway:
1519
enabled: true
1620
locations: classpath:db/migration
1721

22+
# CORS Configuration
23+
cors:
24+
allowed-origins: ${CORS_ALLOWED_ORIGINS:http://localhost:3000,http://localhost:4200}
25+
allowcredentials: ${CORS_ALLOWCREDENTIALS:true}
26+
27+
# Server Configuration
28+
server:
29+
port: 8080
30+
error:
31+
include-message: always
32+
include-binding-errors: always
33+
34+
# Actuator Configuration
1835
management:
1936
endpoints:
2037
web:
2138
exposure:
22-
include: '*'
39+
include: health,info,metrics
40+
base-path: /actuator
41+
endpoint:
42+
health:
43+
show-details: always
2344

24-
server:
25-
port: 8080
45+
# API Documentation
46+
springdoc:
47+
api-docs:
48+
path: /api-docs
49+
swagger-ui:
50+
path: /swagger-ui.html
51+
operations-sorter: method
2652

2753
---
2854

0 commit comments

Comments
 (0)