77 lines
1.9 KiB
Rust
77 lines
1.9 KiB
Rust
mod auth;
|
|
mod builtins;
|
|
mod content;
|
|
mod cursor;
|
|
mod database;
|
|
mod http;
|
|
mod routes;
|
|
mod state;
|
|
|
|
use crate::{http::session::refresh_sessions, state::Config};
|
|
use anyhow::Result;
|
|
use axum::{
|
|
http::{
|
|
header::{ACCEPT, AUTHORIZATION, CONTENT_ENCODING, CONTENT_TYPE},
|
|
Method,
|
|
},
|
|
middleware, Server,
|
|
};
|
|
use figment::{
|
|
providers::{Env, Format, Serialized, Toml},
|
|
Figment,
|
|
};
|
|
use sqlx::postgres::PgPoolOptions;
|
|
use state::AppState;
|
|
use std::{net::SocketAddr, sync::Arc};
|
|
use tower_http::cors::{AllowOrigin, CorsLayer};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
dotenvy::dotenv()?;
|
|
|
|
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 cors = CorsLayer::new()
|
|
.allow_methods([
|
|
Method::GET,
|
|
Method::POST,
|
|
Method::PUT,
|
|
Method::DELETE,
|
|
Method::OPTIONS,
|
|
])
|
|
.allow_origin(AllowOrigin::mirror_request())
|
|
.allow_credentials(true)
|
|
.allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE])
|
|
.expose_headers([CONTENT_ENCODING]);
|
|
|
|
let database = PgPoolOptions::new()
|
|
.max_connections(5)
|
|
.connect(config.database_url.as_str())
|
|
.await?;
|
|
|
|
sqlx::migrate!().run(&database).await?;
|
|
|
|
// Sanity check config
|
|
config.sanity_check();
|
|
|
|
let shared_state = Arc::new(AppState { database, config });
|
|
|
|
let app = routes::create_router()
|
|
.route_layer(middleware::from_fn_with_state(
|
|
shared_state.clone(),
|
|
refresh_sessions,
|
|
))
|
|
.layer(cors)
|
|
.with_state(shared_state);
|
|
|
|
tracing::debug!("listening on {}", addr);
|
|
Server::bind(&addr).serve(app.into_make_service()).await?;
|
|
Ok(())
|
|
}
|