diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e8db718 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/README.md b/README.md index 374b86e..40f073e 100644 --- a/README.md +++ b/README.md @@ -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) @@ -47,7 +47,34 @@ Create a `src/main/resources/env.properties` file with the following content: Replace ``, ``, and `` with the values you used when creating the PostgreSQL container. -### 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. + +
+ Build and run the Docker containers: + + ```bash + docker-compose up --build + ``` +This command will build the Docker images and start the containers. + +
+ +
+ Stop and remove the Docker containers: + + ```bash + docker-compose down + ``` +This command will stop and remove the Docker containers. + + Note: + +The `src/main/resources/env.properties` file contains environment variables for database configuration. Make sure to update this file with your desired values. +
+ +### 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. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b092bb6 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/src/main/java/com/activecourses/upwork/config/security/jwt/JwtService.java b/src/main/java/com/activecourses/upwork/config/security/jwt/JwtService.java index bb1ec1e..8ad36ba 100644 --- a/src/main/java/com/activecourses/upwork/config/security/jwt/JwtService.java +++ b/src/main/java/com/activecourses/upwork/config/security/jwt/JwtService.java @@ -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) { diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 63e533e..d223728 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -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: @@ -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: diff --git a/src/main/resources/env.properties b/src/main/resources/env.properties index 5cb5170..a4793e1 100644 --- a/src/main/resources/env.properties +++ b/src/main/resources/env.properties @@ -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 \ No newline at end of file +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 \ No newline at end of file