mod auth;
mod config;
mod handlers;
mod helpers;
mod router;
mod upstream_router;
use std::{env, time::Duration};
use config::ProxyConfig;
use router::{create_router, health_check_loop};
use tracing::{error, info};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_target(false)
.with_level(true)
.init();
let config = ProxyConfig::from_env();
let listen = env::var("PROXY_LISTEN").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
if let Some(ref router) = config.router {
tokio::spawn(health_check_loop(router.clone(), Duration::from_mins(1)));
}
let app = create_router(config);
let listener = match tokio::net::TcpListener::bind(&listen).await {
Ok(l) => l,
Err(e) => {
error!(error = %e, listen, "failed to bind");
panic!("failed to bind: {e}");
}
};
info!(listen, "proxy listening");
axum::serve(listener, app).await.expect("serve failed");
}