mabel/src/main.rs
2023-07-01 15:02:40 +02:00

52 lines
1.3 KiB
Rust

mod auth;
mod content;
mod error;
mod roles;
mod routes;
mod state;
use anyhow::Result;
use axum::{
routing::{get, post},
Router, Server,
};
use figment::{
providers::{Env, Format, Serialized, Toml},
Figment,
};
use sqlx::postgres::PgPoolOptions;
use state::AppState;
use std::{net::SocketAddr, sync::Arc};
use crate::state::Config;
#[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()?;
let addr: SocketAddr = config.bind.parse()?;
let database = PgPoolOptions::new()
.max_connections(5)
.connect(config.database_url.as_str())
.await?;
sqlx::migrate!().run(&database).await?;
let shared_state = Arc::new(AppState { database, config });
let app = Router::new()
.route("/auth/login", post(routes::auth::login))
.route("/me", get(routes::auth::me))
.route("/pages/:site/:slug", get(routes::content::page))
.route("/admin/bootstrap", post(routes::admin::bootstrap))
.with_state(shared_state);
tracing::debug!("listening on {}", addr);
Server::bind(&addr).serve(app.into_make_service()).await?;
Ok(())
}