use std::{collections::HashMap, sync::Arc}; use axum::{Router, routing::post}; use nanoid::nanoid; use std::collections::HashSet; use tokio::sync::Mutex; use tower_http::cors::CorsLayer; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; mod auth; mod handlers; mod log; mod types; use handlers::*; use types::*; use crate::auth::auth_test; use crate::log::log_req_res_bodies; #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "info,tower_http=info,axum=info".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); // TODO: should this be inside an Arc? let state = AppState { users: Arc::new(Mutex::new(Vec::new())), signup_keys: Arc::new(Mutex::new(HashSet::new())), friend_requests: Arc::new(Mutex::new(HashSet::new())), links: Arc::new(Mutex::new(HashSet::new())), positions: Arc::new(Mutex::new(HashMap::new())), admin_id: Arc::new(Mutex::new(None)), ring_buffer_cap: 5, }; // Until we have disk saves, always generate a admin signup key since there will be no admin set at launch let admin_signup_key = nanoid!(5); println!("Admin signup key: {admin_signup_key}"); println!("http://127.0.0.1:3000"); state.signup_keys.lock().await.insert(admin_signup_key); // build our application with a route let app = Router::new() .route("/", post(|| async { "You just sent a POST to /" })) // for testing .route("/create-account", post(create_user)) .route("/generate-signup-key", post(generate_signup_key)) .route("/request-friend-request", post(request_friend_request)) .route( "/is-friend-request-accepted", post(is_friend_request_accepted), ) .route("/send-pings", post(send_pings)) .route("/get-pings", post(get_pings)) .with_state(state.clone()) .layer(axum::middleware::from_fn_with_state(state, auth_test)) .layer(axum::middleware::from_fn(log_req_res_bodies)) .layer(CorsLayer::permissive()); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } // TODO: potential security risk of returning error messages directly to the user. nice for debugging tho :p