Skip to content

Commit

Permalink
Merge pull request #49 from AhmedMohamedAbdelaty/docker-compose
Browse files Browse the repository at this point in the history
Add Docker and Docker Compose Support
  • Loading branch information
AhmedFatthy1040 authored Oct 25, 2024
2 parents d51626b + 61fb00e commit 9bb1a23
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 19 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Build stage
FROM eclipse-temurin:21-jdk-alpine AS builder
WORKDIR /build
# Copy gradle files
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .
# Make gradlew executable
RUN chmod +x gradlew
# Download dependencies
RUN ./gradlew dependencies

# Copy source code and resources
COPY src src
# Build the application
RUN ./gradlew build -x test

# Run stage
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
# Copy the built artifact from builder stage
COPY --from=builder /build/build/libs/*.jar app.jar
# Copy the env.properties file
COPY src/main/resources/env.properties /app/resources/env.properties
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A platform connecting freelancers and clients for job postings, proposals, and r
## Table of Contents
- [Setup](#setup)
- [Database: PostgreSQL](#database-postgresql)
- [Flyway](#flyway)
- [Docker Compose](#docker-compose)
- [API Endpoints](#api-endpoints)
- [Authentication](#authentication)
- [User Management](#user-management)
Expand Down Expand Up @@ -47,7 +47,34 @@ Create a `src/main/resources/env.properties` file with the following content:
Replace `<username>`, `<password>`, and `<database-name>` with the values you used when creating the PostgreSQL container.
</details>

### Flyway
### Docker Compose

Docker Compose is used to manage multi-container Docker applications. The `docker-compose.yml` file contains the configuration for the PostgreSQL and application services.

<details>
<summary>Build and run the Docker containers:</summary>

```bash
docker-compose up --build
```
This command will build the Docker images and start the containers.

</details>

<details>
<summary>Stop and remove the Docker containers:</summary>

```bash
docker-compose down
```
This command will stop and remove the Docker containers.

<summary>Note:</summary>

The `src/main/resources/env.properties` file contains environment variables for database configuration. Make sure to update this file with your desired values.
</details>

### Note on Flyway

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.

Expand Down
51 changes: 51 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
services:
postgres:
image: postgres:16-alpine
container_name: upwork_postgres
environment:
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:-root}
POSTGRES_DB: ${DB_NAME:-upwork}
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init-scripts:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
networks:
- app-network

app:
build:
context: .
dockerfile: Dockerfile
container_name: upwork_app
depends_on:
postgres:
condition: service_healthy
environment:
SPRING_CONFIG_LOCATION: file:/app/resources/env.properties
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${DB_NAME:-upwork}
SPRING_DATASOURCE_USERNAME: ${DB_USER:-postgres}
SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD:-root}
SPRING_PROFILES_ACTIVE: ${SPRING_PROFILE:-dev}
ports:
- "8080:8080"
volumes:
- ./logs:/app/logs
restart: unless-stopped
networks:
- app-network

volumes:
postgres_data:
driver: local

networks:
app-network:
driver: bridge
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
public class JwtService {
private static final Logger logger = LoggerFactory.getLogger(JwtService.class);

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

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

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

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

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

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

public ResponseCookie generateJwtCookie(User userPrincipal) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spring:
driver-class-name: "org.postgresql.Driver"
jpa:
hibernate:
ddl-auto: validate # validate to let Flyway manage the schema
ddl-auto: validate
show-sql: true
properties:
hibernate:
Expand All @@ -21,14 +21,14 @@ spring:
application:
security:
jwt:
secret-key: ${JWT_SECRET_KEY}
expiration: 600000 # 10 minutes
secret_key: ${application.security.jwt.secret_key}
expiration: ${application.security.jwt.expiration}
refresh-token:
expiration: 604800000 # 7 days
expiration: ${application.security.jwt.refresh_token.expiration}
cookie:
jwt-cookie-name: ${JWT_COOKIE_NAME}
jwt-refresh-cookie-name: ${JWT_REFRESH_COOKIE_NAME}
max-age: ${JWT_COOKIE_MAX_AGE}
jwt_cookie_name: ${application.security.jwt.cookie.jwt_cookie_name}
jwt_refresh_cookie_name: ${application.security.jwt.cookie.jwt_refresh_cookie_name}
max_age: ${application.security.jwt.cookie.max_age}

springdoc:
api-docs:
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/env.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ spring.mail.password=test
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

application.security.jwt.secret-key=382b27314b5b3b637a2c766f41596c50636b25755b2b3f4e7721494f61
application.security.jwt.secret_key=382b27314b5b3b637a2c766f41596c50636b25755b2b3f4e7721494f61
application.security.jwt.expiration=3600000
application.security.jwt.refresh-token.expiration=7200000
application.security.jwt.cookie.jwt-cookie-name=jwt_token
application.security.jwt.cookie.jwt-refresh-cookie-name=jwt_refresh_token
application.security.jwt.cookie.max-age=86400
application.security.jwt.refresh_token.expiration=7200000
application.security.jwt.cookie.jwt_cookie_name=jwt_token
application.security.jwt.cookie.jwt_refresh_cookie_name=jwt_refresh_token
application.security.jwt.cookie.max_age=86400

0 comments on commit 9bb1a23

Please sign in to comment.