compendium/src/main.rs
aleidk af709f2e72 refactor: change assets handling to vite-rs
chore: update dev environment

wip: handle vite tooling manually
2025-07-16 09:46:27 -04:00

58 lines
1.7 KiB
Rust

#![allow(unused)]
#![allow(dead_code)]
use axum_htmx::AutoVaryLayer;
use compendium::config::Config;
use compendium::static_assets::Assets;
use compendium::{config, router, AppState, Error, Result, Tx};
use minijinja::{Environment, Value};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use std::env;
use std::sync::Arc;
use tower_http::trace::TraceLayer;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() -> Result<()> {
let config = Config::new("./config.toml".into())?;
let assets = Assets::new(&config)?;
// Logs
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
info!("Connecting to database {}", config.db);
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&config.db.generate_db_string(false)?)
.await?;
sqlx::migrate!("./migrations").run(&pool).await?;
let (tx_state, tx_layer) = Tx::setup(pool);
let app = router::new(&config)
.layer(TraceLayer::new_for_http().on_request(()))
.layer(tx_layer)
.layer(AutoVaryLayer)
.with_state(AppState::new(assets, tx_state));
// Add hot reload only on dev mode
#[cfg(debug_assertions)]
let app = app.layer(tower_livereload::LiveReloadLayer::new());
let listener = tokio::net::TcpListener::bind(config.addr).await?;
info!("listening on {}", listener.local_addr()?);
axum::serve(listener, app).await?;
Ok(())
}