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-06-28 23:21:45 +00:00
|
|
|
use anyhow::Result;
|
2023-06-29 12:08:59 +00:00
|
|
|
use axum::{
|
|
|
|
routing::{get, post},
|
|
|
|
Router, 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-30 13:02:20 +00:00
|
|
|
use crate::state::Config;
|
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-06-30 13:02:20 +00:00
|
|
|
let shared_state = Arc::new(AppState { database, config });
|
2023-06-28 22:59:26 +00:00
|
|
|
|
2023-06-29 12:08:59 +00:00
|
|
|
let app = Router::new()
|
2023-06-30 13:02:20 +00:00
|
|
|
.route("/auth/login", post(routes::auth::login))
|
2023-07-01 13:02:40 +00:00
|
|
|
.route("/me", get(routes::auth::me))
|
2023-06-29 12:08:59 +00:00
|
|
|
.route("/pages/:site/:slug", get(routes::content::page))
|
|
|
|
.route("/admin/bootstrap", post(routes::admin::bootstrap))
|
|
|
|
.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(())
|
|
|
|
}
|