captains-log/backend/src/main.rs
Drew Galbraith 112ec89bb4
All checks were successful
Check / Backend (pull_request) Successful in 20m54s
Check / Frontend (pull_request) Successful in 2m1s
Add a docker build step.
2025-10-18 19:57:23 -07:00

54 lines
1.6 KiB
Rust

use axum::{Router, routing::get};
use tower_http::cors::{Any, CorsLayer};
mod database;
mod models;
mod services;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let database_url = std::env::var("DATABASE_URL").unwrap_or("sqlite:local.db".to_string());
let binding = database::DatabaseConfig { database_url };
let pool = database::create_pool(&binding)
.await
.expect("Failed to create database pool");
let cors = if std::env::var("RUST_ENV").unwrap_or_default() == "production" {
CorsLayer::new()
.allow_origin([
"https://tiramisu.one"
.parse::<axum::http::HeaderValue>()
.unwrap(),
"https://*.tiramisu.one"
.parse::<axum::http::HeaderValue>()
.unwrap(),
])
.allow_methods(Any)
.allow_headers(Any)
} else {
CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any)
};
let app = Router::new()
.route("/health", get(health))
.nest("/api/tasks", services::create_task_router())
.layer(cors)
.with_state(pool);
let port = std::env::var("PORT").unwrap_or("3000".to_string());
let addr = format!("0.0.0.0:{}", port);
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
tracing::info!("API Server listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
async fn health() -> &'static str {
"Ok"
}