Phase 2.1: Backend project initialized with Docker configuration
- Created Cargo.toml with all required dependencies - Implemented health/ready endpoints - Added Docker configuration (production + development) - Configured docker-compose with resource limits - Set up MongoDB service with persistence - Verified build (cargo check passed) - Prepared monorepo structure for mobile/web/shared Next: Phase 2.2 (MongoDB connection and models)
This commit is contained in:
parent
4dca44dbbe
commit
1e38fe3ace
11 changed files with 388 additions and 80 deletions
8
backend/.env.example
Normal file
8
backend/.env.example
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
RUST_LOG=info
|
||||
SERVER_HOST=0.0.0.0
|
||||
SERVER_PORT=8000
|
||||
MONGODB_URI=mongodb://mongodb:27017
|
||||
MONGODB_DATABASE=normogen
|
||||
JWT_SECRET=change-this-to-a-random-secret-key
|
||||
JWT_ACCESS_TOKEN_EXPIRY_MINUTES=15
|
||||
JWT_REFRESH_TOKEN_EXPIRY_DAYS=30
|
||||
29
backend/Cargo.toml
Normal file
29
backend/Cargo.toml
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
[package]
|
||||
name = "normogen-backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.7", features = ["macros", "multipart"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tower = "0.4"
|
||||
tower-http = { version = "0.5", features = ["cors", "trace", "limit", "decompression-gzip"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
mongodb = "2.8"
|
||||
jsonwebtoken = "9"
|
||||
async-trait = "0.1"
|
||||
dotenv = "0.15"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
validator = { version = "0.16", features = ["derive"] }
|
||||
uuid = { version = "1", features = ["v4", "serde"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
pbkdf2 = { version = "0.12", features = ["simple"] }
|
||||
sha2 = "0.10"
|
||||
rand = "0.8"
|
||||
anyhow = "1"
|
||||
thiserror = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test = "0.4"
|
||||
1
backend/config/test.env
Normal file
1
backend/config/test.env
Normal file
|
|
@ -0,0 +1 @@
|
|||
test
|
||||
4
backend/defaults.env
Normal file
4
backend/defaults.env
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
RUST_LOG=debug
|
||||
SERVER_PORT=8000
|
||||
MONGODB_URI=mongodb://mongodb:27017
|
||||
MONGODB_DATABASE=normogen
|
||||
45
backend/docker-compose.dev.yml
Normal file
45
backend/docker-compose.dev.yml
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
version: '3.8'
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/Dockerfile.dev
|
||||
container_name: normogen-backend-dev
|
||||
ports:
|
||||
- '6000:8000'
|
||||
volumes:
|
||||
- ./src:/app/src
|
||||
environment:
|
||||
- RUST_LOG=debug
|
||||
- SERVER_PORT=8000
|
||||
- MONGODB_URI=mongodb://mongodb:27017
|
||||
- MONGODB_DATABASE=normogen_dev
|
||||
depends_on:
|
||||
mongodb:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- normogen-network
|
||||
restart: unless-stopped
|
||||
mongodb:
|
||||
image: mongo:6.0
|
||||
container_name: normogen-mongodb-dev
|
||||
ports:
|
||||
- '27017:27017'
|
||||
environment:
|
||||
- MONGO_INITDB_DATABASE=normogen_dev
|
||||
volumes:
|
||||
- mongodb_dev_data:/data/db
|
||||
networks:
|
||||
- normogen-network
|
||||
healthcheck:
|
||||
test: ['CMD', 'mongosh', '--eval', 'db.adminCommand.ping()']
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
volumes:
|
||||
mongodb_dev_data:
|
||||
driver: local
|
||||
networks:
|
||||
normogen-network:
|
||||
driver: bridge
|
||||
57
backend/docker-compose.yml
Normal file
57
backend/docker-compose.yml
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
version: '3.8'
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/Dockerfile
|
||||
container_name: normogen-backend
|
||||
ports:
|
||||
- '6000:8000'
|
||||
environment:
|
||||
- RUST_LOG=info
|
||||
- SERVER_PORT=8000
|
||||
- MONGODB_URI=mongodb://mongodb:27017
|
||||
- MONGODB_DATABASE=normogen
|
||||
env_file:
|
||||
- .env
|
||||
depends_on:
|
||||
mongodb:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- normogen-network
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.0'
|
||||
memory: 1000M
|
||||
healthcheck:
|
||||
test: ['CMD', 'wget', '--no-verbose', '--tries=1', '--spider', 'http://localhost:8000/health']
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 40s
|
||||
mongodb:
|
||||
image: mongo:6.0
|
||||
container_name: normogen-mongodb
|
||||
ports:
|
||||
- '27017:27017'
|
||||
environment:
|
||||
- MONGO_INITDB_DATABASE=normogen
|
||||
volumes:
|
||||
- mongodb_data:/data/db
|
||||
networks:
|
||||
- normogen-network
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ['CMD', 'mongosh', '--eval', 'db.adminCommand.ping()']
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
volumes:
|
||||
mongodb_data:
|
||||
driver: local
|
||||
networks:
|
||||
normogen-network:
|
||||
driver: bridge
|
||||
18
backend/docker/Dockerfile
Normal file
18
backend/docker/Dockerfile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
FROM rust:1.75-alpine AS builder
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache musl-dev pkgconf openssl-dev
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
RUN mkdir src && echo 'fn main() {}' > src/main.rs
|
||||
RUN cargo build --release && rm -rf src
|
||||
COPY src ./src
|
||||
RUN touch src/main.rs && cargo build --release
|
||||
|
||||
FROM alpine:3.18
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache ca-certificates openssl wget
|
||||
COPY --from=builder /app/target/release/normogen-backend /app/normogen-backend
|
||||
RUN addgroup -g 1000 normogen && adduser -D -u 1000 -G normogen normogen && chown -R normogen:normogen /app
|
||||
USER normogen
|
||||
EXPOSE 8000
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 CMD wget --no-verbose --tries=1 --spider http://localhost:8000/health || exit 1
|
||||
CMD ['./normogen-backend']
|
||||
10
backend/docker/Dockerfile.dev
Normal file
10
backend/docker/Dockerfile.dev
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
FROM rust:1.75-alpine
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache musl-dev pkgconf openssl-dev curl wget git pkgconfig
|
||||
RUN cargo install cargo-watch
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
RUN mkdir src && echo 'fn main() {}' > src/main.rs
|
||||
RUN cargo build && rm -rf src
|
||||
COPY src ./src
|
||||
EXPOSE 8000
|
||||
CMD ['cargo-watch', '-x', 'run']
|
||||
52
backend/src/main.rs
Normal file
52
backend/src/main.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
response::Json,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "normogen_backend=debug,tower_http=debug,axum=debug".into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
tracing::info!("Starting Normogen backend server");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/health", get(health_check))
|
||||
.route("/ready", get(readiness_check))
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 8000));
|
||||
tracing::info!("Listening on {}", addr);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(addr)
|
||||
.await
|
||||
.expect("Failed to bind address");
|
||||
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.expect("Server error");
|
||||
}
|
||||
|
||||
async fn health_check() -> Json<serde_json::Value> {
|
||||
Json(json!({
|
||||
"status": "ok",
|
||||
"timestamp": chrono::Utc::now().to_rfc3339(),
|
||||
}))
|
||||
}
|
||||
|
||||
async fn readiness_check() -> Json<serde_json::Value> {
|
||||
Json(json!({
|
||||
"status": "ready",
|
||||
"database": "not_connected",
|
||||
"timestamp": chrono::Utc::now().to_rfc3339(),
|
||||
}))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue