what2watch/src/main.rs

55 lines
1.8 KiB
Rust

use std::net::SocketAddr;
use axum::{routing::get, Router};
use axum_login::axum_sessions::SessionLayer;
use rand_core::{OsRng, RngCore};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use witch_watch::{
db,
generic_handlers::{handle_slash, handle_slash_redir},
session_store::SqliteSessionStore,
signup::{get_create_user, get_login, handle_signup_success, post_create_user, post_login},
};
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "witch_watch=debug,axum::routing=info".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
let pool = db::get_pool().await;
let session_layer = {
let store = SqliteSessionStore::from_client(pool.clone());
store.migrate().await.expect("Could not migrate session DB");
let secret = {
let mut bytes = [0u8; 128];
let mut rng = OsRng;
rng.fill_bytes(&mut bytes);
bytes
};
SessionLayer::new(store, &secret).with_secure(true)
};
let app = Router::new()
.route("/", get(handle_slash).post(handle_slash))
.route("/signup", get(get_create_user).post(post_create_user))
.route(
"/signup_success/:id",
get(handle_signup_success).post(handle_signup_success),
)
.route("/login", get(get_login).post(post_login))
.fallback(handle_slash_redir)
.layer(session_layer)
.with_state(pool);
tracing::debug!("binding to 0.0.0.0:3000");
axum::Server::bind(&SocketAddr::from(([0, 0, 0, 0], 3000)))
.serve(app.into_make_service())
.await
.unwrap();
}