Compare commits

..

2 Commits

Author SHA1 Message Date
Mathias Wagner
439578434e Create Dockerfile 2025-09-09 10:39:24 +02:00
Mathias Wagner
5a9e1e2e2b Implement shutdown signal in main.rs 2025-09-09 10:38:24 +02:00
4 changed files with 121 additions and 3 deletions

35
.dockerignore Normal file
View File

@@ -0,0 +1,35 @@
# Git
.git
.gitignore
README.md
LICENSE
# Rust
server/target/
**/*.rs.bk
# Node.js
webui/node_modules/
webui/dist/
webui/.vite/
# IDE
.vscode/
.idea/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Logs
*.log
server/data/logs/
# Database (for development)
server/data/db/*.db
server/data/backups/
# Cache
.cache/

53
Dockerfile Normal file
View File

@@ -0,0 +1,53 @@
FROM node:20-alpine AS webui-builder
WORKDIR /app/webui
COPY webui/package.json webui/pnpm-lock.yaml ./
RUN npm install -g pnpm && pnpm install --frozen-lockfile
COPY webui/ .
RUN pnpm build
FROM rust:1.89-alpine AS rust-builder
RUN apk add --no-cache musl-dev sqlite-dev pkgconfig openssl-dev
WORKDIR /app/server
COPY server/Cargo.toml server/Cargo.lock ./
COPY server/src ./src
RUN cargo build --release
FROM alpine:latest
RUN apk add --no-cache sqlite
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
WORKDIR /app
RUN mkdir -p data/db data/logs data/backups && \
chown -R appuser:appgroup /app
COPY --from=rust-builder /app/server/target/release/server ./server
COPY --from=webui-builder /app/webui/dist ./dist
RUN chown -R appuser:appgroup /app
USER appuser
EXPOSE 8379
ENV RUST_LOG=info
VOLUME ["/app/data/db"]
VOLUME ["/app/data/logs"]
VOLUME ["/app/data/backups"]
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8379/ || exit 1
CMD ["./server"]

View File

@@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.8.4" axum = "0.8.4"
tokio = { version = "1.47.1", features = ["full"] } tokio = { version = "1.47.1", features = ["full", "signal"] }
sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "sqlite", "chrono", "uuid"] } sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "sqlite", "chrono", "uuid"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@@ -8,6 +8,7 @@ use axum::{routing::{delete, get, post, put}, Router};
use routes::{admin, auth as auth_routes, machines, setup}; use routes::{admin, auth as auth_routes, machines, setup};
use tower_http::{cors::CorsLayer, services::{ServeDir, ServeFile}}; use tower_http::{cors::CorsLayer, services::{ServeDir, ServeFile}};
use std::path::Path; use std::path::Path;
use tokio::signal;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@@ -32,7 +33,7 @@ async fn main() -> Result<()> {
.with_state(pool); .with_state(pool);
let dist_path = "../dist"; let dist_path = "./dist";
let app = Router::new() let app = Router::new()
.nest("/api", api_routes) .nest("/api", api_routes)
.nest_service("/assets", ServeDir::new(format!("{}/assets", dist_path))) .nest_service("/assets", ServeDir::new(format!("{}/assets", dist_path)))
@@ -46,6 +47,35 @@ async fn main() -> Result<()> {
let listener = tokio::net::TcpListener::bind("0.0.0.0:8379").await?; let listener = tokio::net::TcpListener::bind("0.0.0.0:8379").await?;
println!("Server running on http://0.0.0.0:8379"); println!("Server running on http://0.0.0.0:8379");
axum::serve(listener, app).await?;
axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()).await?;
Ok(()) Ok(())
} }
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {
println!("\nShutting down due to Ctrl+C...");
},
_ = terminate => {
println!("\nShutting down due to terminate signal...");
},
}
}