2023-05-10 19:08:03 +00:00
|
|
|
use std::net::SocketAddr;
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-05-10 19:08:03 +00:00
|
|
|
use axum::{routing::get, Router};
|
2023-05-24 23:39:13 +00:00
|
|
|
use axum_login::axum_sessions::SessionLayer;
|
|
|
|
use rand_core::{OsRng, RngCore};
|
2022-04-10 06:00:33 +00:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2023-05-18 17:05:29 +00:00
|
|
|
use witch_watch::{
|
|
|
|
db,
|
2023-05-22 23:57:08 +00:00
|
|
|
generic_handlers::{handle_slash, handle_slash_redir},
|
2023-05-24 23:39:13 +00:00
|
|
|
session_store::SqliteSessionStore,
|
2023-05-28 20:31:45 +00:00
|
|
|
signup::{get_create_user, get_login, handle_signup_success, post_create_user, post_login},
|
2023-05-18 17:05:29 +00:00
|
|
|
};
|
2023-05-10 19:08:03 +00:00
|
|
|
|
2022-04-10 06:00:33 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
tracing_subscriber::registry()
|
2023-04-26 05:19:49 +00:00
|
|
|
.with(
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
2023-05-10 19:08:03 +00:00
|
|
|
.unwrap_or_else(|_| "witch_watch=debug,axum::routing=info".into()),
|
2023-04-26 05:19:49 +00:00
|
|
|
)
|
2022-04-10 06:00:33 +00:00
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
.init();
|
|
|
|
|
2023-05-10 19:08:03 +00:00
|
|
|
let pool = db::get_pool().await;
|
2023-05-28 19:20:55 +00:00
|
|
|
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)
|
2023-05-24 23:39:13 +00:00
|
|
|
};
|
2022-04-10 06:00:33 +00:00
|
|
|
|
2023-04-26 05:19:49 +00:00
|
|
|
let app = Router::new()
|
2023-05-22 23:57:08 +00:00
|
|
|
.route("/", get(handle_slash).post(handle_slash))
|
2023-05-18 17:05:29 +00:00
|
|
|
.route("/signup", get(get_create_user).post(post_create_user))
|
2023-05-20 00:17:24 +00:00
|
|
|
.route(
|
2023-05-22 22:08:14 +00:00
|
|
|
"/signup_success/:id",
|
|
|
|
get(handle_signup_success).post(handle_signup_success),
|
2023-05-20 00:17:24 +00:00
|
|
|
)
|
2023-05-28 19:20:55 +00:00
|
|
|
.route("/login", get(get_login).post(post_login))
|
2023-05-22 23:57:08 +00:00
|
|
|
.fallback(handle_slash_redir)
|
2023-05-24 23:39:13 +00:00
|
|
|
.layer(session_layer)
|
2023-04-26 05:19:49 +00:00
|
|
|
.with_state(pool);
|
|
|
|
|
2023-05-10 19:08:03 +00:00
|
|
|
tracing::debug!("binding to 0.0.0.0:3000");
|
2023-04-26 05:19:49 +00:00
|
|
|
axum::Server::bind(&SocketAddr::from(([0, 0, 0, 0], 3000)))
|
2022-04-10 06:00:33 +00:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|