Files
user-to-supporter-chat-rust/src/main.rs
T

72 lines
2.5 KiB
Rust

mod handlers;
mod models;
mod state;
use axum::{
Router,
routing::{get, post},
};
use std::{net::SocketAddr, sync::Arc};
use tower_http::services::ServeDir;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use state::{AppState, CHAT_RESET_INTERVAL};
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "chat_service=debug,info".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();
let state = Arc::new(AppState::new());
let reset_state = state.clone();
tokio::spawn(async move {
loop {
tokio::time::sleep(CHAT_RESET_INTERVAL).await;
reset_state.clear_everything();
tracing::info!(
"Cleared all in-memory chat sessions, messages, notifiers, and rate limits"
);
}
});
let app = Router::new()
.route("/", get(handlers::pages::index_page))
.route("/user", get(handlers::pages::user_page))
.route("/supporter", get(handlers::pages::supporter_page))
.route("/reset-countdown", get(handlers::pages::reset_countdown))
// New API endpoint for creating sessions without a form
.route("/api/session/create", post(handlers::chat::create_session))
.route("/ws/user/:session_id", get(handlers::ws::user_ws))
.route(
"/ws/supporter/:supporter_id",
get(handlers::ws::supporter_ws),
)
.route("/messages/:session_id", get(handlers::chat::get_messages))
.route(
"/supporter/messages/:session_id",
get(handlers::chat::get_supporter_messages),
)
.route("/send/:session_id", post(handlers::chat::send_message))
.route(
"/supporter/send/:session_id",
post(handlers::chat::supporter_send_message),
)
.route("/sessions", get(handlers::chat::get_sessions))
// Removed: .route("/sessions/new", post(handlers::chat::new_session))
.nest_service("/static", ServeDir::new("static"))
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3111").await.unwrap();
tracing::info!("Chat service running — http://0.0.0.0:3111");
axum::Server::from_tcp(listener.into_std().unwrap())
.unwrap()
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
.unwrap();
}