Compare commits

...

1 commit

Author SHA1 Message Date
3246203805 Add a docker build step.
All checks were successful
Docker Build / Build and Push Backend Image (push) Successful in 12m34s
Docker Build / Build and Push Frontend Image (push) Successful in 5m56s
Check / Backend (pull_request) Successful in 7m7s
Check / Frontend (pull_request) Successful in 1m47s
2025-09-30 00:51:16 -07:00
10 changed files with 205 additions and 24 deletions

View file

@ -0,0 +1,46 @@
name: Docker Build
on:
push:
branches: [ main, docker ]
jobs:
docker-backend:
name: Build and Push Backend Image
runs-on: docker
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- run: |
apt update && apt install -y docker.io
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Forgejo Container Registry
run: echo "${{ secrets.FORGEJO_TOKEN }}" | docker login ${{ forgejo.server_url }} -u ${{ forgejo.actor }} --password-stdin
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: ./backend
push: true
tags: git.tiramisu.one/drew/captains-log-backend
docker-frontend:
name: Build and Push Frontend Image
runs-on: docker
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- run: |
apt update && apt install -y docker.io
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Forgejo Container Registry
run: echo "${{ secrets.FORGEJO_TOKEN }}" | docker login ${{ forgejo.server_url }} -u ${{ forgejo.actor }} --password-stdin
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: ./frontend
push: true
tags: git.tiramisu.one/drew/captains-log-frontend

15
backend/Cargo.lock generated
View file

@ -289,6 +289,7 @@ dependencies = [
"serde", "serde",
"sqlx", "sqlx",
"tokio", "tokio",
"tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"uuid", "uuid",
@ -3490,6 +3491,20 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "tower-http"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
dependencies = [
"bitflags 2.9.3",
"bytes",
"http 1.3.1",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.3" version = "0.3.3"

View file

@ -12,6 +12,7 @@ chrono = { version = "0.4.41", features = ["serde"] }
serde = "1.0.219" serde = "1.0.219"
sqlx = { version = "0.8.6", features = ["sqlite", "runtime-tokio", "uuid", "chrono"] } sqlx = { version = "0.8.6", features = ["sqlite", "runtime-tokio", "uuid", "chrono"] }
tokio = { version = "1.47.1", features = ["rt-multi-thread", "tracing"] } tokio = { version = "1.47.1", features = ["rt-multi-thread", "tracing"] }
tower-http = { version = "0.6.0", features = ["cors"] }
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = "0.3.19" tracing-subscriber = "0.3.19"
uuid = { version = "1.18.0", features = ["serde", "v4"] } uuid = { version = "1.18.0", features = ["serde", "v4"] }

47
backend/Dockerfile Normal file
View file

@ -0,0 +1,47 @@
# Multi-stage Dockerfile for Rust backend
FROM rust:1.90-alpine as builder
# Install build dependencies
RUN apk add --no-cache \
musl-dev \
pkgconfig \
openssl-dev \
clang
# Set working directory
WORKDIR /app
# Copy Cargo files for dependency caching
COPY Cargo.toml Cargo.lock ./
# Create a dummy main.rs to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs
# Build dependencies (cached layer)
RUN cargo build --release && rm -rf src/
# Copy source code
COPY src/ src/
COPY migrations/ migrations/
# Build the application
RUN cargo build --release
# Runtime stage
FROM alpine:latest
# Install runtime dependencies
RUN apk add --no-cache ca-certificates curl
# Copy the binary from builder stage
COPY --from=builder /app/target/release/backend /usr/local/bin/app
# Expose port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Run the application
CMD ["app"]

View file

@ -1,4 +1,5 @@
use axum::{Router, routing::get}; use axum::{Router, routing::get};
use tower_http::cors::{Any, CorsLayer};
mod database; mod database;
mod models; mod models;
@ -13,13 +14,33 @@ async fn main() {
let pool = database::create_pool(&binding) let pool = database::create_pool(&binding)
.await .await
.expect("Failed to create database pool"); .expect("Failed to create database pool");
let cors = if std::env::var("RUST_ENV").unwrap_or_default() == "production" {
CorsLayer::new()
.allow_origin([
"https://tiramisu.one"
.parse::<axum::http::HeaderValue>()
.unwrap(),
"https://*.tiramisu.one"
.parse::<axum::http::HeaderValue>()
.unwrap(),
])
.allow_methods(Any)
.allow_headers(Any)
} else {
CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any)
};
let app = Router::new() let app = Router::new()
.route("/health", get(health)) .route("/health", get(health))
.nest("/api/tasks", services::create_task_router()) .nest("/api/tasks", services::create_task_router())
.layer(cors)
.with_state(pool); .with_state(pool);
let port = std::env::var("PORT").unwrap_or("3000".to_string()); let port = std::env::var("PORT").unwrap_or("3000".to_string());
let addr = format!("127.0.0.1:{}", port); let addr = format!("0.0.0.0:{}", port);
let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(addr).await.unwrap();

36
docker-compose.yml Normal file
View file

@ -0,0 +1,36 @@
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- DATABASE_URL=sqlite:///app/data/local.db
- RUST_LOG=info
volumes:
- backend_data:/app/data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "5173:5173"
depends_on:
backend:
condition: service_healthy
environment:
- VITE_API_URL=http://localhost:3000
- API_URL=http://backend:3000
volumes:
backend_data:

View file

@ -52,9 +52,12 @@ describe('API Client', () => {
const result = await apiClient.listTasks() const result = await apiClient.listTasks()
expect(mockFetch).toHaveBeenCalledWith('/api/tasks', { expect(mockFetch).toHaveBeenCalledWith(
'http://localhost:3000/api/tasks',
{
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}) }
)
expect(result).toEqual(mockTasks) expect(result).toEqual(mockTasks)
}) })
@ -94,9 +97,12 @@ describe('API Client', () => {
const result = await apiClient.getTask(mockTask.id) const result = await apiClient.getTask(mockTask.id)
expect(mockFetch).toHaveBeenCalledWith(`/api/tasks/${mockTask.id}`, { expect(mockFetch).toHaveBeenCalledWith(
`http://localhost:3000/api/tasks/${mockTask.id}`,
{
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}) }
)
expect(result).toEqual(mockTask) expect(result).toEqual(mockTask)
}) })
@ -130,11 +136,14 @@ describe('API Client', () => {
const result = await apiClient.createTask(newTaskData) const result = await apiClient.createTask(newTaskData)
expect(mockFetch).toHaveBeenCalledWith('/api/tasks', { expect(mockFetch).toHaveBeenCalledWith(
'http://localhost:3000/api/tasks',
{
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newTaskData), body: JSON.stringify(newTaskData),
}) }
)
expect(result).toEqual(mockTask) expect(result).toEqual(mockTask)
}) })
@ -174,11 +183,14 @@ describe('API Client', () => {
const result = await apiClient.updateTask(mockTask.id, updateData) const result = await apiClient.updateTask(mockTask.id, updateData)
expect(mockFetch).toHaveBeenCalledWith(`/api/tasks/${mockTask.id}`, { expect(mockFetch).toHaveBeenCalledWith(
`http://localhost:3000/api/tasks/${mockTask.id}`,
{
method: 'PUT', method: 'PUT',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updateData), body: JSON.stringify(updateData),
}) }
)
expect(result).toEqual(updatedTask) expect(result).toEqual(updatedTask)
}) })
@ -211,10 +223,13 @@ describe('API Client', () => {
const result = await apiClient.deleteTask(mockTask.id) const result = await apiClient.deleteTask(mockTask.id)
expect(result).toBeNull() expect(result).toBeNull()
expect(mockFetch).toHaveBeenCalledWith(`/api/tasks/${mockTask.id}`, { expect(mockFetch).toHaveBeenCalledWith(
`http://localhost:3000/api/tasks/${mockTask.id}`,
{
method: 'DELETE', method: 'DELETE',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}) }
)
}) })
it('should handle delete errors', async () => { it('should handle delete errors', async () => {

View file

@ -6,7 +6,7 @@ import type {
ApiError, ApiError,
} from '~/types/task' } from '~/types/task'
const API_BASE_URL = '/api' const API_BASE_URL = `${import.meta.env.VITE_API_URL || 'http://localhost:3000'}/api`
class ApiClient { class ApiClient {
private async fetchWrapper<T>( private async fetchWrapper<T>(

View file

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"build": "react-router build", "build": "react-router build",
"dev": "react-router dev", "dev": "react-router dev",
"start": "react-router-serve ./build/server/index.js", "start": "PORT=5173 react-router-serve ./build/server/index.js",
"test": "vitest", "test": "vitest",
"test:coverage": "vitest --coverage", "test:coverage": "vitest --coverage",
"test:run": "vitest run", "test:run": "vitest run",

View file

@ -7,7 +7,7 @@ export default defineConfig({
server: { server: {
proxy: { proxy: {
"/api": { "/api": {
target: "http://localhost:3000", target: process.env.VITE_API_URL || "http://127.0.0.1:3000",
changeOrigin: true, changeOrigin: true,
}, },
}, },