Skip to content

Commit 9bb1a23

Browse files
Merge pull request #49 from AhmedMohamedAbdelaty/docker-compose
Add Docker and Docker Compose Support
2 parents d51626b + 61fb00e commit 9bb1a23

File tree

6 files changed

+124
-19
lines changed

6 files changed

+124
-19
lines changed

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build stage
2+
FROM eclipse-temurin:21-jdk-alpine AS builder
3+
WORKDIR /build
4+
# Copy gradle files
5+
COPY gradlew .
6+
COPY gradle gradle
7+
COPY build.gradle .
8+
COPY settings.gradle .
9+
# Make gradlew executable
10+
RUN chmod +x gradlew
11+
# Download dependencies
12+
RUN ./gradlew dependencies
13+
14+
# Copy source code and resources
15+
COPY src src
16+
# Build the application
17+
RUN ./gradlew build -x test
18+
19+
# Run stage
20+
FROM eclipse-temurin:21-jre-alpine
21+
WORKDIR /app
22+
# Copy the built artifact from builder stage
23+
COPY --from=builder /build/build/libs/*.jar app.jar
24+
# Copy the env.properties file
25+
COPY src/main/resources/env.properties /app/resources/env.properties
26+
EXPOSE 8080
27+
ENTRYPOINT ["java", "-jar", "app.jar"]

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A platform connecting freelancers and clients for job postings, proposals, and r
77
## Table of Contents
88
- [Setup](#setup)
99
- [Database: PostgreSQL](#database-postgresql)
10-
- [Flyway](#flyway)
10+
- [Docker Compose](#docker-compose)
1111
- [API Endpoints](#api-endpoints)
1212
- [Authentication](#authentication)
1313
- [User Management](#user-management)
@@ -47,7 +47,34 @@ Create a `src/main/resources/env.properties` file with the following content:
4747
Replace `<username>`, `<password>`, and `<database-name>` with the values you used when creating the PostgreSQL container.
4848
</details>
4949

50-
### Flyway
50+
### Docker Compose
51+
52+
Docker Compose is used to manage multi-container Docker applications. The `docker-compose.yml` file contains the configuration for the PostgreSQL and application services.
53+
54+
<details>
55+
<summary>Build and run the Docker containers:</summary>
56+
57+
```bash
58+
docker-compose up --build
59+
```
60+
This command will build the Docker images and start the containers.
61+
62+
</details>
63+
64+
<details>
65+
<summary>Stop and remove the Docker containers:</summary>
66+
67+
```bash
68+
docker-compose down
69+
```
70+
This command will stop and remove the Docker containers.
71+
72+
<summary>Note:</summary>
73+
74+
The `src/main/resources/env.properties` file contains environment variables for database configuration. Make sure to update this file with your desired values.
75+
</details>
76+
77+
### Note on Flyway
5178

5279
Flyway is used to manage database migrations. The SQL scripts are located in `src/main/resources/db/migration`. When you run the application, Flyway will automatically create the necessary tables in the database.
5380

docker-compose.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
services:
2+
postgres:
3+
image: postgres:16-alpine
4+
container_name: upwork_postgres
5+
environment:
6+
POSTGRES_USER: ${DB_USER:-postgres}
7+
POSTGRES_PASSWORD: ${DB_PASSWORD:-root}
8+
POSTGRES_DB: ${DB_NAME:-upwork}
9+
ports:
10+
- "5432:5432"
11+
volumes:
12+
- postgres_data:/var/lib/postgresql/data
13+
- ./init-scripts:/docker-entrypoint-initdb.d
14+
healthcheck:
15+
test: ["CMD-SHELL", "pg_isready -U postgres"]
16+
interval: 10s
17+
timeout: 5s
18+
retries: 5
19+
restart: unless-stopped
20+
networks:
21+
- app-network
22+
23+
app:
24+
build:
25+
context: .
26+
dockerfile: Dockerfile
27+
container_name: upwork_app
28+
depends_on:
29+
postgres:
30+
condition: service_healthy
31+
environment:
32+
SPRING_CONFIG_LOCATION: file:/app/resources/env.properties
33+
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${DB_NAME:-upwork}
34+
SPRING_DATASOURCE_USERNAME: ${DB_USER:-postgres}
35+
SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD:-root}
36+
SPRING_PROFILES_ACTIVE: ${SPRING_PROFILE:-dev}
37+
ports:
38+
- "8080:8080"
39+
volumes:
40+
- ./logs:/app/logs
41+
restart: unless-stopped
42+
networks:
43+
- app-network
44+
45+
volumes:
46+
postgres_data:
47+
driver: local
48+
49+
networks:
50+
app-network:
51+
driver: bridge

src/main/java/com/activecourses/upwork/config/security/jwt/JwtService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
public class JwtService {
2727
private static final Logger logger = LoggerFactory.getLogger(JwtService.class);
2828

29-
@Value("${application.security.jwt.secret-key}")
29+
@Value("${application.security.jwt.secret_key}")
3030
private String secretKey;
3131

3232
@Value("${application.security.jwt.expiration}")
3333
private long jwtExpirationTime;
3434

35-
@Value("${application.security.jwt.refresh-token.expiration}")
35+
@Value("${application.security.jwt.refresh_token.expiration}")
3636
private long refreshJwtExpirationTime;
3737

38-
@Value("${application.security.jwt.cookie.jwt-cookie-name}")
38+
@Value("${application.security.jwt.cookie.jwt_cookie_name}")
3939
private String jwtCookie;
4040

41-
@Value("${application.security.jwt.cookie.jwt-refresh-cookie-name}")
41+
@Value("${application.security.jwt.cookie.jwt_refresh_cookie_name}")
4242
private String jwtRefreshCookie;
4343

44-
@Value("${application.security.jwt.cookie.max-age}")
44+
@Value("${application.security.jwt.cookie.max_age}")
4545
private int maxAge;
4646

4747
public ResponseCookie generateJwtCookie(User userPrincipal) {

src/main/resources/application-dev.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spring:
88
driver-class-name: "org.postgresql.Driver"
99
jpa:
1010
hibernate:
11-
ddl-auto: validate # validate to let Flyway manage the schema
11+
ddl-auto: validate
1212
show-sql: true
1313
properties:
1414
hibernate:
@@ -21,14 +21,14 @@ spring:
2121
application:
2222
security:
2323
jwt:
24-
secret-key: ${JWT_SECRET_KEY}
25-
expiration: 600000 # 10 minutes
24+
secret_key: ${application.security.jwt.secret_key}
25+
expiration: ${application.security.jwt.expiration}
2626
refresh-token:
27-
expiration: 604800000 # 7 days
27+
expiration: ${application.security.jwt.refresh_token.expiration}
2828
cookie:
29-
jwt-cookie-name: ${JWT_COOKIE_NAME}
30-
jwt-refresh-cookie-name: ${JWT_REFRESH_COOKIE_NAME}
31-
max-age: ${JWT_COOKIE_MAX_AGE}
29+
jwt_cookie_name: ${application.security.jwt.cookie.jwt_cookie_name}
30+
jwt_refresh_cookie_name: ${application.security.jwt.cookie.jwt_refresh_cookie_name}
31+
max_age: ${application.security.jwt.cookie.max_age}
3232

3333
springdoc:
3434
api-docs:

src/main/resources/env.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ spring.mail.password=test
1010
spring.mail.properties.mail.smtp.auth=true
1111
spring.mail.properties.mail.smtp.starttls.enable=true
1212

13-
application.security.jwt.secret-key=382b27314b5b3b637a2c766f41596c50636b25755b2b3f4e7721494f61
13+
application.security.jwt.secret_key=382b27314b5b3b637a2c766f41596c50636b25755b2b3f4e7721494f61
1414
application.security.jwt.expiration=3600000
15-
application.security.jwt.refresh-token.expiration=7200000
16-
application.security.jwt.cookie.jwt-cookie-name=jwt_token
17-
application.security.jwt.cookie.jwt-refresh-cookie-name=jwt_refresh_token
18-
application.security.jwt.cookie.max-age=86400
15+
application.security.jwt.refresh_token.expiration=7200000
16+
application.security.jwt.cookie.jwt_cookie_name=jwt_token
17+
application.security.jwt.cookie.jwt_refresh_cookie_name=jwt_refresh_token
18+
application.security.jwt.cookie.max_age=86400

0 commit comments

Comments
 (0)