diff --git a/Dockerfile b/Dockerfile index 95ff3c1..3f0a5e6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,13 @@ # Build Stage -FROM rust:1.85-bookworm AS builder +FROM rust:1.88-bookworm AS builder WORKDIR /app +# Install sqlite3 for compile-time query checking +RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/* + # Cache dependencies -# We create a dummy project to build only dependencies -RUN cargo new --bin blog_cms +RUN cargo init --name blog_cms . COPY Cargo.toml Cargo.lock ./ RUN cargo build --release RUN rm src/*.rs @@ -14,8 +16,43 @@ RUN rm src/*.rs COPY src ./src COPY templates ./templates -# The templates are needed during compilation by askama -# Rebuild the real source +# Create a temporary SQLite DB with the schema for sqlx compile-time checks +RUN sqlite3 /tmp/build.db " \ + CREATE TABLE users ( \ + id INTEGER PRIMARY KEY AUTOINCREMENT, \ + username TEXT NOT NULL UNIQUE, \ + password_hash TEXT NOT NULL, \ + role TEXT NOT NULL DEFAULT 'readonly' \ + ); \ + CREATE TABLE sessions ( \ + id TEXT PRIMARY KEY, \ + user_id INTEGER NOT NULL, \ + expires_at INTEGER NOT NULL, \ + FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE \ + ); \ + CREATE TABLE posts ( \ + id INTEGER PRIMARY KEY AUTOINCREMENT, \ + author_id INTEGER NOT NULL, \ + title TEXT NOT NULL, \ + content TEXT NOT NULL, \ + tags TEXT NOT NULL DEFAULT '', \ + categories TEXT NOT NULL DEFAULT '', \ + visibility TEXT NOT NULL DEFAULT 'public', \ + password TEXT, \ + created_at INTEGER NOT NULL, \ + updated_at INTEGER NOT NULL, \ + FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE CASCADE \ + ); \ + CREATE TABLE attachments ( \ + id TEXT PRIMARY KEY, \ + filename TEXT NOT NULL, \ + content_type TEXT NOT NULL, \ + size INTEGER NOT NULL, \ + created_at INTEGER NOT NULL \ + );" + +# Rebuild the real source with DATABASE_URL for sqlx macros +ENV DATABASE_URL=sqlite:///tmp/build.db RUN touch src/main.rs && cargo build --release # Final Stage @@ -23,7 +60,7 @@ FROM debian:bookworm-slim WORKDIR /app -# Install runtime dependencies (like SSL certificates and sqlite) +# Install runtime dependencies RUN apt-get update && apt-get install -y \ ca-certificates \ libsqlite3-0 \ @@ -32,15 +69,13 @@ RUN apt-get update && apt-get install -y \ # Copy the binary from the builder COPY --from=builder /app/target/release/blog_cms . -# Create directory for attachments and database -RUN mkdir -p attachments +# Create directories for attachments and database +RUN mkdir -p attachments data # Default environment variables ENV PORT=3000 -ENV DATABASE_URL=sqlite:///app/data.db +ENV DATABASE_URL=sqlite:///app/data/data.db -# Expose the application port EXPOSE 3000 -# Start the application CMD ["./blog_cms"] diff --git a/docker-compose.yml b/docker-compose.yml index c1a94ed..93520b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,12 @@ -version: '3.8' services: blog: build: . ports: - - "3000:3000" + - "8080:3000" environment: - PORT=3000 - - DATABASE_URL=sqlite:///app/data.db + - DATABASE_URL=sqlite:///app/data/data.db volumes: - - ./data.db:/app/data.db + - ./data:/app/data - ./attachments:/app/attachments restart: always