use actix_remote_ip::RemoteIPConfig; use actix_session::config::SessionLifecycle; use actix_session::{storage::RedisSessionStore, SessionMiddleware}; use actix_web::cookie::Key; use actix_web::{web, App, HttpServer}; use matrix_gateway::app_config::AppConfig; use matrix_gateway::broadcast_messages::BroadcastMessage; use matrix_gateway::server::{api, web_ui}; use matrix_gateway::sync_client; use matrix_gateway::user::UserConfig; #[tokio::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); UserConfig::create_bucket_if_required() .await .expect("Failed to create bucket!"); let secret_key = Key::from(AppConfig::get().secret().as_bytes()); let redis_store = RedisSessionStore::new(AppConfig::get().redis_connection_string()) .await .expect("Failed to connect to Redis!"); let (ws_tx, _) = tokio::sync::broadcast::channel::(16); // Launch sync manager tokio::spawn(sync_client::sync_client_manager(ws_tx.clone())); log::info!( "Starting to listen on {} for {}", AppConfig::get().listen_address, AppConfig::get().website_origin ); HttpServer::new(move || { App::new() // Add session management to your application using Redis for session state storage .wrap( SessionMiddleware::builder(redis_store.clone(), secret_key.clone()) .cookie_name("matrixgw-session".to_string()) .session_lifecycle(SessionLifecycle::BrowserSession(Default::default())) .build(), ) .app_data(web::Data::new(RemoteIPConfig { proxy: AppConfig::get().proxy_ip.clone(), })) .app_data(web::Data::new(ws_tx.clone())) // Web configuration routes .route("/assets/{tail:.*}", web::get().to(web_ui::static_file)) .route("/", web::get().to(web_ui::home)) .route("/", web::post().to(web_ui::home)) .route("/oidc_cb", web::get().to(web_ui::oidc_cb)) .route("/sign_out", web::get().to(web_ui::sign_out)) .route("/ws_debug", web::get().to(web_ui::ws_debug)) // API routes .route("/api", web::get().to(api::api_home)) .route("/api", web::post().to(api::api_home)) .route("/api/account/whoami", web::get().to(api::account::who_am_i)) .route("/api/room/joined", web::get().to(api::room::joined_rooms)) .route("/api/room/{room_id}", web::get().to(api::room::info)) .route( "/api/media/{server_name}/{media_id}/download", web::get().to(api::media::download), ) .route( "/api/media/{server_name}/{media_id}/thumbnail", web::get().to(api::media::thumbnail), ) .route( "/api/profile/{user_id}", web::get().to(api::profile::get_profile), ) .service(web::resource("/api/ws").route(web::get().to(api::ws::ws))) }) .workers(4) .bind(&AppConfig::get().listen_address)? .run() .await }