mabel/src/main.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2023-06-29 12:59:39 +00:00
mod auth;
2023-06-28 22:59:26 +00:00
mod content;
2023-06-28 23:21:45 +00:00
mod error;
2023-06-29 12:59:39 +00:00
mod roles;
2023-06-29 12:08:59 +00:00
mod routes;
mod state;
2023-06-28 22:59:26 +00:00
2023-07-02 01:26:42 +00:00
use crate::{auth::http::refresh_sessions, state::Config};
2023-06-28 23:21:45 +00:00
use anyhow::Result;
2023-07-06 08:43:33 +00:00
use axum::{middleware, Server};
2023-06-26 08:08:49 +00:00
use figment::{
providers::{Env, Format, Serialized, Toml},
Figment,
};
2023-06-29 12:08:59 +00:00
use sqlx::postgres::PgPoolOptions;
use state::AppState;
2023-06-28 22:59:26 +00:00
use std::{net::SocketAddr, sync::Arc};
2023-06-26 08:08:49 +00:00
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let config: Config = Figment::from(Serialized::defaults(Config::default()))
.merge(Toml::file("mabel.toml"))
.merge(Env::prefixed("MABEL_"))
.extract()?;
2023-06-30 13:02:20 +00:00
let addr: SocketAddr = config.bind.parse()?;
2023-06-26 08:08:49 +00:00
2023-06-30 13:02:20 +00:00
let database = PgPoolOptions::new()
2023-06-26 08:08:49 +00:00
.max_connections(5)
.connect(config.database_url.as_str())
.await?;
2023-06-30 13:02:20 +00:00
sqlx::migrate!().run(&database).await?;
2023-06-26 08:08:49 +00:00
2023-07-06 08:43:33 +00:00
// Sanity check config
config.sanity_check();
2023-06-30 13:02:20 +00:00
let shared_state = Arc::new(AppState { database, config });
2023-06-28 22:59:26 +00:00
2023-07-06 08:43:33 +00:00
let app = routes::create_router()
2023-07-02 01:26:42 +00:00
.route_layer(middleware::from_fn_with_state(
shared_state.clone(),
refresh_sessions,
))
2023-06-29 12:08:59 +00:00
.with_state(shared_state);
2023-06-28 22:59:26 +00:00
2023-06-26 08:08:49 +00:00
tracing::debug!("listening on {}", addr);
2023-06-29 14:42:57 +00:00
Server::bind(&addr).serve(app.into_make_service()).await?;
2023-06-26 08:08:49 +00:00
Ok(())
}