Files
mass-driver/crates/mass-driver-backend/src/main.rs
scott 47ef19d76c Add Axum backend, Dockerfiles, and K8s deployment manifests
- Axum backend server: health check, transfer matrix cache (base64 BYTEA),
  route cache (JSONB), CORS, gzip compression, tracing
- Postgres schema: transfer_matrices + cached_routes tables with upserts
- Dockerfile.frontend: 3-stage (wasm-pack → SvelteKit → nginx:alpine)
- Dockerfile.backend: 2-stage (rust build → debian:bookworm-slim)
- nginx.conf: SPA fallback, WASM mime type, /api proxy to backend
- docker-compose.yml: Postgres + backend for local development
- K8s manifests: namespace, frontend/backend deployments with services,
  ingress routing, health probes, secret-based DATABASE_URL

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 12:22:07 -07:00

50 lines
1.5 KiB
Rust

mod db;
mod routes;
use axum::{routing::{get, post}, Router};
use sqlx::postgres::PgPoolOptions;
use tower_http::{compression::CompressionLayer, cors::CorsLayer};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let database_url =
std::env::var("DATABASE_URL").expect("DATABASE_URL environment variable must be set");
let port = std::env::var("PORT").unwrap_or_else(|_| "3001".to_string());
tracing::info!("Connecting to database...");
let pool = PgPoolOptions::new()
.max_connections(10)
.connect(&database_url)
.await
.expect("Failed to connect to Postgres");
db::init_db(&pool).await.expect("Failed to initialize database tables");
tracing::info!("Database initialized");
let app = Router::new()
.route("/api/health", get(routes::health))
.route("/api/cache/transfer-matrix", post(routes::store_matrix))
.route(
"/api/cache/transfer-matrix/{config_hash}",
get(routes::get_matrix),
)
.route("/api/cache/route", post(routes::store_route))
.route("/api/cache/route", get(routes::get_route))
.layer(CompressionLayer::new())
.layer(CorsLayer::permissive())
.with_state(pool);
let addr = format!("0.0.0.0:{port}");
tracing::info!("Listening on {addr}");
let listener = tokio::net::TcpListener::bind(&addr)
.await
.expect("Failed to bind listener");
axum::serve(listener, app)
.await
.expect("Server error");
}