diff --git a/.gitea/workflows/docker.yml b/.gitea/workflows/docker.yml index d9b1ff3..46f7238 100644 --- a/.gitea/workflows/docker.yml +++ b/.gitea/workflows/docker.yml @@ -26,6 +26,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . + dockerfile: ./backend/Dockerfile push: true tags: | ${{ vars.REGISTRY }}/connor/calendar:latest diff --git a/Caddyfile b/Caddyfile index 9f01f7d..c40f993 100644 --- a/Caddyfile +++ b/Caddyfile @@ -5,6 +5,7 @@ } :80, :443 { + try_files {path} /index.html root * /srv/www file_server } diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index c97a613..0000000 --- a/Dockerfile +++ /dev/null @@ -1,109 +0,0 @@ -# Build stage -# ----------------------------------------------------------- -FROM rust:alpine AS builder - -RUN apk add --no-cache musl-dev pkgconfig openssl-dev openssl-libs-static nodejs npm - -# Install trunk ahead of the compilation. This may break and then you'll have to update the version. -RUN cargo install trunk@0.21.14 wasm-pack@0.13.1 wasm-bindgen-cli@0.2.100 - -RUN rustup target add wasm32-unknown-unknown -WORKDIR /app - -# Copy workspace files to maintain workspace structure -COPY Cargo.toml ./ -COPY calendar-models ./calendar-models -COPY frontend/Cargo.toml ./frontend/ -COPY frontend/Trunk.toml ./frontend/ -COPY frontend/index.html ./frontend/ -COPY frontend/styles.css ./frontend/ - -# Create empty backend directory to satisfy workspace -RUN mkdir -p backend/src && \ - printf '[package]\nname = "calendar-backend"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\n' > backend/Cargo.toml && \ - echo 'fn main() {}' > backend/src/main.rs - -# Create dummy source files to build dependencies first -RUN mkdir -p frontend/src && \ - echo "use web_sys::*; fn main() {}" > frontend/src/main.rs && \ - echo "pub fn add(a: usize, b: usize) -> usize { a + b }" > calendar-models/src/lib.rs - -# Build dependencies (this layer will be cached unless dependencies change) -RUN cargo build --release --target wasm32-unknown-unknown --bin runway - -# Copy actual source code and build the frontend application -RUN rm -rf frontend -COPY frontend ./frontend -RUN trunk build --release --config ./frontend/Trunk.toml - - - - -# Backend build stage -# ----------------------------------------------------------- -FROM rust:alpine AS backend-builder - -# Install build dependencies for backend -WORKDIR /app -RUN apk add --no-cache musl-dev pkgconfig openssl-dev openssl-libs-static - -# Install sqlx-cli for migrations -RUN cargo install sqlx-cli --no-default-features --features sqlite - -# Copy shared models -COPY calendar-models ./calendar-models - -# Create empty frontend directory to satisfy workspace -RUN mkdir -p frontend/src && \ - printf '[package]\nname = "runway"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\n' > frontend/Cargo.toml && \ - echo 'fn main() {}' > frontend/src/main.rs - -# Create dummy backend source to build dependencies first -RUN mkdir -p backend/src && \ - echo "fn main() {}" > backend/src/main.rs - -# Build dependencies (this layer will be cached unless dependencies change) -COPY Cargo.toml ./ -COPY backend/Cargo.toml ./backend/ -RUN cargo build --release - -# Build the backend -COPY backend ./backend -RUN cargo build --release --bin backend - - - - -# Runtime stage -# ----------------------------------------------------------- -FROM alpine:latest - -# Install runtime dependencies -RUN apk add --no-cache ca-certificates tzdata sqlite - -# Copy frontend files to temporary location -COPY --from=builder /app/frontend/dist /app/frontend-dist - -# Copy backend binary and sqlx-cli -COPY --from=backend-builder /app/target/release/backend /usr/local/bin/backend -COPY --from=backend-builder /usr/local/cargo/bin/sqlx /usr/local/bin/sqlx - -# Copy migrations for database setup -COPY --from=backend-builder /app/backend/migrations /migrations - -# Create startup script to copy frontend files, run migrations, and start backend -RUN mkdir -p /srv/www /db -RUN echo '#!/bin/sh' > /usr/local/bin/start.sh && \ - echo 'echo "Copying frontend files..."' >> /usr/local/bin/start.sh && \ - echo 'cp -r /app/frontend-dist/* /srv/www/' >> /usr/local/bin/start.sh && \ - echo 'echo "Ensuring database directory exists..."' >> /usr/local/bin/start.sh && \ - echo 'mkdir -p /db && chmod 755 /db' >> /usr/local/bin/start.sh && \ - echo 'echo "Running database migrations..."' >> /usr/local/bin/start.sh && \ - echo 'sqlx migrate run --database-url "sqlite:///db/calendar.db" --source /migrations || echo "Migration failed but continuing..."' >> /usr/local/bin/start.sh && \ - echo 'echo "Starting backend server..."' >> /usr/local/bin/start.sh && \ - echo 'export DATABASE_URL="sqlite:///db/calendar.db"' >> /usr/local/bin/start.sh && \ - echo '/usr/local/bin/backend' >> /usr/local/bin/start.sh && \ - chmod +x /usr/local/bin/start.sh - -# Start with script that copies frontend files then starts backend -CMD ["/usr/local/bin/start.sh"] diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..44d31f5 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,64 @@ +# Build stage +# ----------------------------------------------------------- +FROM rust:alpine AS builder + +# Install build dependencies for backend +WORKDIR /app +RUN apk add --no-cache musl-dev pkgconfig openssl-dev openssl-libs-static + +# Install sqlx-cli for migrations +RUN cargo install sqlx-cli --no-default-features --features sqlite + +# Copy workspace files to maintain workspace structure +COPY ./Cargo.toml ./ +COPY ./calendar-models ./calendar-models + +# Create empty frontend directory to satisfy workspace +RUN mkdir -p frontend/src && \ + printf '[package]\nname = "runway"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\n' > frontend/Cargo.toml && \ + echo 'fn main() {}' > frontend/src/main.rs + +# Copy backend files +COPY backend/Cargo.toml ./backend/ + +# Create dummy backend source to build dependencies first +RUN mkdir -p backend/src && \ + echo "fn main() {}" > backend/src/main.rs + +# Build dependencies (this layer will be cached unless dependencies change) +RUN cargo build --release + +# Copy actual backend source and build +COPY backend/src ./backend/src +COPY backend/migrations ./backend/migrations +RUN cargo build --release --bin backend + +# Runtime stage +# ----------------------------------------------------------- +FROM alpine:latest + +# Install runtime dependencies +RUN apk add --no-cache ca-certificates tzdata sqlite + +# Copy backend binary and sqlx-cli +COPY --from=builder /app/target/release/backend /usr/local/bin/backend +COPY --from=builder /usr/local/cargo/bin/sqlx /usr/local/bin/sqlx + +# Copy migrations for database setup +COPY backend/migrations /migrations + +# Create startup script to run migrations and start backend +RUN mkdir -p /db +RUN echo '#!/bin/sh' > /usr/local/bin/start.sh && \ + echo 'echo "Ensuring database directory exists..."' >> /usr/local/bin/start.sh && \ + echo 'mkdir -p /db && chmod 755 /db' >> /usr/local/bin/start.sh && \ + echo 'touch /db/calendar.db' >> /usr/local/bin/start.sh && \ + echo 'echo "Running database migrations..."' >> /usr/local/bin/start.sh && \ + echo 'sqlx migrate run --database-url "sqlite:///db/calendar.db" --source /migrations || echo "Migration failed but continuing..."' >> /usr/local/bin/start.sh && \ + echo 'echo "Starting backend server..."' >> /usr/local/bin/start.sh && \ + echo 'export DATABASE_URL="sqlite:///db/calendar.db"' >> /usr/local/bin/start.sh && \ + echo '/usr/local/bin/backend' >> /usr/local/bin/start.sh && \ + chmod +x /usr/local/bin/start.sh + +# Start with script that runs migrations then starts backend +CMD ["/usr/local/bin/start.sh"] diff --git a/compose.yml b/compose.yml index ad09c3e..e905e33 100644 --- a/compose.yml +++ b/compose.yml @@ -1,10 +1,11 @@ services: calendar-backend: - build: . + build: + context: . + dockerfile: ./backend/Dockerfile ports: - "3000:3000" volumes: - - ./data/site_dist:/srv/www - ./data/db:/db calendar-frontend: @@ -15,7 +16,7 @@ services: - "80:80" - "443:443" volumes: - - ./data/site_dist:/srv/www:ro + - ./frontend/dist:/srv/www:ro - ./Caddyfile:/etc/caddy/Caddyfile:ro - ./data/caddy/data:/data - ./data/caddy/config:/config diff --git a/frontend/index.html b/frontend/index.html index ba2900c..998e925 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -10,10 +10,10 @@ - \ No newline at end of file +