Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Environment variables for docker-compose
# Copy this file to .env and fill in the actual values
# DO NOT commit .env to version control

# Database Configuration
DB_NAME=applyflow
DB_USERNAME=postgres
DB_PASSWORD=your_secure_password_here

# Email Configuration (Gmail SMTP)
# You must use an App Password, not your regular password
# Generate at: https://myaccount.google.com/apppasswords
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your_gmail_app_password_here

# Optional: Backend Spring Profile
# prod, staging, dev
SPRING_PROFILES_ACTIVE=prod

# Public URLs used by the browser and email links
APP_FRONTEND_URL=https://applyflow.example.com
CORS_ALLOWED_ORIGINS=https://applyflow.example.com
VITE_API_BASE_URL=https://api.applyflow.example.com

# Optional published host ports for docker-compose.prod.yml
FRONTEND_PORT=80
BACKEND_PORT=8080

# Optional image names; GitHub Actions can publish these to GHCR
FRONTEND_IMAGE=applyflow-frontend:prod
BACKEND_IMAGE=applyflow-backend:prod
103 changes: 103 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read
security-events: write

jobs:
frontend:
name: Frontend
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: frontend/package-lock.json

- name: Install dependencies
run: npm ci

- name: Audit dependencies
run: npm audit --audit-level=moderate

- name: Build
run: npm run build

backend:
name: Backend
runs-on: ubuntu-latest
defaults:
run:
working-directory: backend
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
cache: gradle

- name: Test
run: ./gradlew --no-daemon test

docker:
name: Docker build
runs-on: ubuntu-latest
needs: [frontend, backend]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Build backend image
uses: docker/build-push-action@v6
with:
context: backend
file: backend/Dockerfile.prod
load: true
push: false
tags: applyflow-backend:ci

- name: Build frontend image
uses: docker/build-push-action@v6
with:
context: frontend
file: frontend/Dockerfile.prod
load: true
push: false
tags: applyflow-frontend:ci
build-args: |
VITE_API_BASE_URL=http://localhost:8080

- name: Scan backend image
uses: aquasecurity/trivy-action@0.24.0
with:
scan-type: image
image-ref: applyflow-backend:ci
severity: CRITICAL,HIGH
ignore-unfixed: true
exit-code: 1

- name: Scan frontend image
uses: aquasecurity/trivy-action@0.24.0
with:
scan-type: image
image-ref: applyflow-frontend:ci
severity: CRITICAL,HIGH
ignore-unfixed: true
exit-code: 1
63 changes: 63 additions & 0 deletions .github/workflows/publish-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Publish Images

on:
push:
branches: [main]
tags: ["v*.*.*"]
workflow_dispatch:

permissions:
contents: read
packages: write

env:
REGISTRY: ghcr.io
BACKEND_IMAGE: ${{ github.repository }}/backend
FRONTEND_IMAGE: ${{ github.repository }}/frontend

jobs:
publish:
name: Build and publish
runs-on: ubuntu-latest
strategy:
matrix:
include:
- name: backend
context: backend
dockerfile: backend/Dockerfile.prod
image: ${{ github.repository }}/backend
- name: frontend
context: frontend
dockerfile: frontend/Dockerfile.prod
image: ${{ github.repository }}/frontend
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ matrix.image }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=sha,prefix=sha-

- name: Build and push
uses: docker/build-push-action@v6
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VITE_API_BASE_URL=${{ vars.VITE_API_BASE_URL || 'http://localhost:8080' }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
.vscode/
.gradle/
bin
build
build
.env
.env.prod
secrets/
.gradle-home/
9 changes: 9 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.gradle
build
bin
*.iml
.idea
.vscode
.DS_Store
.env
.env.*
17 changes: 17 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM eclipse-temurin:21-jdk

WORKDIR /app

COPY gradlew gradlew
COPY gradle gradle
COPY build.gradle settings.gradle ./
RUN chmod +x gradlew

# Pre-fetch dependencies for faster startup
RUN ./gradlew --no-daemon dependencies || true

COPY src src

EXPOSE 8080

CMD ["./gradlew", "--no-daemon", "bootRun"]
44 changes: 44 additions & 0 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Stage 1: Build
# Use Eclipse Temurin JDK 21 to build the Spring Boot application
FROM eclipse-temurin:21-jdk-jammy AS builder

WORKDIR /app

# Copy Gradle wrapper and configuration
COPY gradlew gradlew
COPY gradle gradle
COPY build.gradle settings.gradle ./
RUN chmod +x gradlew

# Pre-fetch dependencies for faster builds
RUN ./gradlew --no-daemon --quiet dependencies >/dev/null || true

# Copy source code
COPY src src

# Build the application JAR
RUN ./gradlew --no-daemon clean bootJar -x test

# Stage 2: Runtime
FROM eclipse-temurin:21-jre-jammy

WORKDIR /app

RUN groupadd --system applyflow && useradd --system --gid applyflow --home-dir /app applyflow

# Copy the built JAR from builder stage
COPY --from=builder --chown=applyflow:applyflow /app/build/libs/*.jar application.jar

# Expose port 8080
EXPOSE 8080

# Set Java options for production (e.g., memory limits)
ENV JAVA_OPTS="-XX:+UseG1GC -XX:MaxRAMPercentage=75 -XX:InitialRAMPercentage=25"

USER applyflow

HEALTHCHECK --interval=30s --timeout=5s --start-period=45s --retries=3 \
CMD bash -c '</dev/tcp/127.0.0.1/8080' || exit 1

# Run the application
CMD ["sh", "-c", "java $JAVA_OPTS -jar application.jar"]
5 changes: 5 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ dependencies {
implementation 'org.hibernate.validator:hibernate-validator:8.0.1.Final'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// Database migrations
implementation 'org.springframework.boot:spring-boot-flyway'
implementation 'org.flywaydb:flyway-core'
runtimeOnly 'org.flywaydb:flyway-database-postgresql'

// PostgreSQL
runtimeOnly 'org.postgresql:postgresql'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package com.casey.applyflow.config;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfiguration {

@Value("${app.cors.allowed-origins:http://localhost:3000,http://localhost:5173}")
private String allowedOrigins;

@Bean
public CorsConfigurationSource corsConfigurationSource() {
org.springframework.web.cors.CorsConfiguration config = new org.springframework.web.cors.CorsConfiguration();
config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:5173"));
config.setAllowedOrigins(Arrays.stream(allowedOrigins.split(","))
.map(String::trim)
.filter(origin -> !origin.isBlank())
.toList());
config.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
config.setAllowedHeaders(List.of("*"));
config.setAllowCredentials(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class SecurityConfiguration {

private final RsaKeyProperties rsaKeys;

@Value("${app.security.allow-h2-console:false}")
private boolean allowH2Console;

public SecurityConfiguration(RsaKeyProperties rsaKeys) {
this.rsaKeys = rsaKeys;
}
Expand All @@ -45,7 +49,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.csrf(AbstractHttpConfigurer::disable) // Disable CSRF for stateless API
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/api/auth/**").permitAll(); // Public auth endpoints
auth.requestMatchers("/h2-console/**").permitAll(); // H2 console (dev only)
if (allowH2Console) {
auth.requestMatchers("/h2-console/**").permitAll();
}
auth.anyRequest().authenticated(); // Everything else requires auth
})
.sessionManagement(session ->
Expand All @@ -54,8 +60,11 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
oauth2
.bearerTokenResolver(bearerTokenResolver())
.jwt(Customizer.withDefaults())) // Enable JWT validation
.headers(headers ->
headers.frameOptions(frame -> frame.sameOrigin())) // For H2 console
.headers(headers -> {
if (allowH2Console) {
headers.frameOptions(frame -> frame.sameOrigin());
}
})
.build();
}

Expand Down
Loading
Loading