Merge pull request 'automatic-deployments' (#64) from automatic-deployments into main
Some checks failed
ci/woodpecker/push/deployment Pipeline failed

Reviewed-on: #64
This commit is contained in:
mixel 2025-04-09 22:25:08 +02:00
commit 69497713f7
8 changed files with 108 additions and 33 deletions

View file

@ -0,0 +1,23 @@
steps:
- name: build-docker
image: docker:latest
commands:
- docker build -f backend.Dockerfile -t backend .
- name: login-forgejo
image: docker:latest
commands:
- docker login -u $$FORGEJO_USERNAME -p $$FORGEJO_PASSWORD <your_forgejo_instance>
environment:
FORGEJO_USERNAME:
from_secret: FORGEJO_USERNAME
FORGEJO_PASSWORD:
from_secret: FORGEJO_PASSWORD
- name: publish-forgejo
image: docker:latest
commands:
- export IMAGE_NAME=git.mixel.cloud/Turbo/peer-group-grading:$${CI_COMMIT_SHA:0:8}
- docker tag backend $$IMAGE_NAME
- docker push $$IMAGE_NAME

30
Cargo.lock generated
View file

@ -1045,16 +1045,6 @@ dependencies = [
"cipher", "cipher",
] ]
[[package]]
name = "ctrlc"
version = "3.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
dependencies = [
"nix",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.20.10" version = "0.20.10"
@ -2088,18 +2078,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577" checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577"
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.6" version = "0.4.6"
@ -4178,14 +4156,6 @@ dependencies = [
"tap", "tap",
] ]
[[package]]
name = "xtask"
version = "0.1.0"
dependencies = [
"clap",
"ctrlc",
]
[[package]] [[package]]
name = "yansi" name = "yansi"
version = "1.0.1" version = "1.0.1"

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["crates/backend", "crates/migration", "crates/xtask"] members = ["crates/backend", "crates/migration"]
resolver = "3" resolver = "3"
[workspace.package] [workspace.package]

52
backend.Dockerfile Normal file
View file

@ -0,0 +1,52 @@
# ---- Stage 1: Build (Static Linking) ----
FROM rust:latest as builder
WORKDIR /usr/src/app
# Install the musl target and musl-tools
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update && apt-get install -y musl-tools
# Create a .cargo directory and configure for static linking with musl
RUN mkdir -p .cargo
RUN echo '[target.x86_64-unknown-linux-musl]' > .cargo/config.toml
RUN echo 'linker = "rust-lld"' >> .cargo/config.toml
# Copy configuration and lock files needed to resolve dependencies
COPY Cargo.toml ./Cargo.toml
COPY Cargo.lock ./Cargo.lock
COPY crates/backend/Cargo.toml crates/backend/Cargo.toml
COPY crates/migration/Cargo.toml crates/migration/Cargo.toml
# Fetch dependencies based on the lock file.
RUN cargo fetch
# Copy the actual source code for all workspace members
COPY crates/backend/ crates/backend/
COPY crates/migration/ crates/migration/
# Build the release binary for the musl target
RUN cargo build --release --target x86_64-unknown-linux-musl
# Debug: List the built binaries to verify what we have
RUN ls -la /usr/src/app/target/x86_64-unknown-linux-musl/release/
# ---- Stage 2: Runtime ----
FROM alpine:latest
# Install minimal runtime dependencies (ca-certificates is often needed)
RUN apk add --no-cache ca-certificates
WORKDIR /app
# Copy only the statically linked compiled binary from the builder stage
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/backend /app/backend
# Verify the binary exists and is executable
RUN ls -la /app && chmod +x /app/backend
# Expose the port the application listens on
EXPOSE 8080
# Set the command to run the application
CMD ["/app/backend"]

View file

@ -32,6 +32,9 @@ dotenvy = "0.15"
[dev-dependencies] [dev-dependencies]
temp-env = "*" temp-env = "*"
[features] [features]
serve = [] serve = []
[[bin]]
name = "backend"
path = "src/main.rs"

View file

@ -30,6 +30,10 @@ async fn login(
.verify_local_user(&login_request.username, &login_request.password) .verify_local_user(&login_request.username, &login_request.password)
.await?; .await?;
if session.get::<String>("user").is_ok() {
return Err(ApiError::AlreadyLoggedIn);
}
session.insert("user", user_id)?; session.insert("user", user_id)?;
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())

View file

@ -1,4 +1,4 @@
use actix_web::{cookie::time::error, http::StatusCode, HttpResponse, ResponseError}; use actix_web::{HttpResponse, ResponseError, cookie::time::error, http::StatusCode};
use sea_orm::TransactionError; use sea_orm::TransactionError;
use thiserror::Error; use thiserror::Error;
@ -18,6 +18,8 @@ pub enum ApiError {
Argon2Error(String), Argon2Error(String),
#[error("Session insert error: {0}")] #[error("Session insert error: {0}")]
SessionInsertError(#[from] actix_session::SessionInsertError), SessionInsertError(#[from] actix_session::SessionInsertError),
#[error("Already logged in")]
AlreadyLoggedIn,
} }
impl ResponseError for ApiError { impl ResponseError for ApiError {
@ -30,6 +32,7 @@ impl ResponseError for ApiError {
ApiError::ValidationError(..) => StatusCode::BAD_REQUEST, ApiError::ValidationError(..) => StatusCode::BAD_REQUEST,
ApiError::Argon2Error(..) => StatusCode::INTERNAL_SERVER_ERROR, ApiError::Argon2Error(..) => StatusCode::INTERNAL_SERVER_ERROR,
ApiError::SessionInsertError(..) => StatusCode::INTERNAL_SERVER_ERROR, ApiError::SessionInsertError(..) => StatusCode::INTERNAL_SERVER_ERROR,
ApiError::AlreadyLoggedIn => StatusCode::BAD_REQUEST,
} }
} }

View file

@ -52,6 +52,26 @@ services:
interval: 30s interval: 30s
retries: 3 retries: 3
backend:
build:
context: .
dockerfile: ./backend.Dockerfile
image: peer-group-grading:latest
restart: unless-stopped
environment:
DB_NAME: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_HOST: db
DB_PORT: 5432
REDIS_HOST: redis
REDIS_PORT: 6379
ports:
- "8080:8080"
depends_on:
- db
- redis
volumes: volumes:
postgres_data: postgres_data:
redis: redis: