70 lines
2.0 KiB
Rust
70 lines
2.0 KiB
Rust
mod api;
|
|
mod config;
|
|
mod db;
|
|
mod error;
|
|
mod shared;
|
|
mod state;
|
|
mod storage;
|
|
|
|
use axum::http::{HeaderValue, Method};
|
|
use std::sync::Arc;
|
|
use tower_http::cors::CorsLayer;
|
|
use tower_http::trace::TraceLayer;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
tracing_subscriber::fmt()
|
|
.json()
|
|
.with_ansi(false)
|
|
.flatten_event(true)
|
|
.with_current_span(true)
|
|
.with_span_list(true)
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
.init();
|
|
|
|
let cfg = config::Config::from_env()?;
|
|
tracing::info!("config loaded: bind={}", cfg.bind_addr);
|
|
|
|
let pool = db::connect(&cfg.database_url).await?;
|
|
|
|
let storage = Arc::new(storage::seaweed::SeaweedClient::new(cfg.storage_url.clone()));
|
|
|
|
let state = state::AppState {
|
|
db: pool.clone(),
|
|
seaweed: storage.clone(),
|
|
public_base: cfg.storage_public_base.clone(),
|
|
};
|
|
|
|
// Optional: ingest seed assets if images table empty
|
|
if let Some(dir) = cfg.seed_assets_dir.clone() {
|
|
let s = state.clone();
|
|
tokio::spawn(async move {
|
|
if let Err(e) = api::upload_image::service::ingest_seed_dir(&s, &dir).await {
|
|
tracing::warn!("seed-assets ingest skipped: {e:?}");
|
|
}
|
|
});
|
|
}
|
|
|
|
let cors_origins: Vec<HeaderValue> = cfg
|
|
.cors_origins
|
|
.iter()
|
|
.filter_map(|o| o.parse::<HeaderValue>().ok())
|
|
.collect();
|
|
let cors = CorsLayer::new()
|
|
.allow_origin(cors_origins)
|
|
.allow_methods([Method::GET, Method::POST, Method::DELETE, Method::OPTIONS])
|
|
.allow_headers([axum::http::header::CONTENT_TYPE, axum::http::header::AUTHORIZATION])
|
|
.allow_credentials(false);
|
|
|
|
let app = axum::Router::new()
|
|
.merge(api::router())
|
|
.layer(TraceLayer::new_for_http())
|
|
.layer(cors)
|
|
.with_state(state);
|
|
|
|
let listener = tokio::net::TcpListener::bind(&cfg.bind_addr).await?;
|
|
tracing::info!("listening on {}", cfg.bind_addr);
|
|
axum::serve(listener, app).await?;
|
|
Ok(())
|
|
}
|