121 lines
4.2 KiB
Rust
121 lines
4.2 KiB
Rust
use actix_cors::Cors;
|
|
use actix_remote_ip::RemoteIPConfig;
|
|
use actix_session::SessionMiddleware;
|
|
use actix_session::config::SessionLifecycle;
|
|
use actix_session::storage::RedisSessionStore;
|
|
use actix_web::cookie::Key;
|
|
use actix_web::middleware::Logger;
|
|
use actix_web::{App, HttpServer, web};
|
|
use matrixgw_backend::app_config::AppConfig;
|
|
use matrixgw_backend::broadcast_messages::BroadcastMessage;
|
|
use matrixgw_backend::constants;
|
|
use matrixgw_backend::controllers::{auth_controller, matrix_link_controller, server_controller};
|
|
use matrixgw_backend::matrix_connection::matrix_manager::MatrixManagerActor;
|
|
use matrixgw_backend::users::User;
|
|
use ractor::Actor;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
let secret_key = Key::from(AppConfig::get().secret().as_bytes());
|
|
|
|
log::info!("Connect to Redis session store...");
|
|
let redis_store = RedisSessionStore::new(AppConfig::get().redis_connection_string())
|
|
.await
|
|
.expect("Failed to connect to Redis!");
|
|
|
|
let (ws_tx, _) = tokio::sync::broadcast::channel::<BroadcastMessage>(16);
|
|
|
|
// Auto create default account, if requested
|
|
if let Some(mail) = &AppConfig::get().unsecure_auto_login_email() {
|
|
User::create_or_update_user(mail, "Anonymous")
|
|
.await
|
|
.expect("Failed to create auto-login account!");
|
|
}
|
|
|
|
// Create matrix clients manager actor
|
|
let (manager_actor, manager_actor_handle) = Actor::spawn(
|
|
Some("matrix-clients-manager".to_string()),
|
|
MatrixManagerActor,
|
|
ws_tx.clone(),
|
|
)
|
|
.await
|
|
.expect("Failed to start Matrix manager actor!");
|
|
|
|
log::info!(
|
|
"Starting to listen on {} for {}",
|
|
AppConfig::get().listen_address,
|
|
AppConfig::get().website_origin
|
|
);
|
|
|
|
let manager_actor_clone = manager_actor.clone();
|
|
HttpServer::new(move || {
|
|
let session_mw = SessionMiddleware::builder(redis_store.clone(), secret_key.clone())
|
|
.cookie_name("matrixgw-session".to_string())
|
|
.session_lifecycle(SessionLifecycle::BrowserSession(Default::default()))
|
|
.build();
|
|
|
|
let cors = Cors::default()
|
|
.allowed_origin(&AppConfig::get().website_origin)
|
|
.allowed_methods(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
|
|
.allowed_header(constants::API_AUTH_HEADER)
|
|
.allow_any_header()
|
|
.supports_credentials()
|
|
.max_age(3600);
|
|
|
|
App::new()
|
|
.wrap(Logger::default())
|
|
.wrap(session_mw)
|
|
.wrap(cors)
|
|
.app_data(web::Data::new(manager_actor_clone.clone()))
|
|
.app_data(web::Data::new(RemoteIPConfig {
|
|
proxy: AppConfig::get().proxy_ip.clone(),
|
|
}))
|
|
.app_data(web::Data::new(ws_tx.clone()))
|
|
// Server controller
|
|
.route("/robots.txt", web::get().to(server_controller::robots_txt))
|
|
.route(
|
|
"/api/server/config",
|
|
web::get().to(server_controller::config),
|
|
)
|
|
// Auth controller
|
|
.route(
|
|
"/api/auth/start_oidc",
|
|
web::get().to(auth_controller::start_oidc),
|
|
)
|
|
.route(
|
|
"/api/auth/finish_oidc",
|
|
web::post().to(auth_controller::finish_oidc),
|
|
)
|
|
.route("/api/auth/info", web::get().to(auth_controller::auth_info))
|
|
.route(
|
|
"/api/auth/sign_out",
|
|
web::get().to(auth_controller::sign_out),
|
|
)
|
|
// Matrix link controller
|
|
.route(
|
|
"/api/matrix_link/start_auth",
|
|
web::post().to(matrix_link_controller::start_auth),
|
|
)
|
|
.route(
|
|
"/api/matrix_link/finish_auth",
|
|
web::post().to(matrix_link_controller::finish_auth),
|
|
)
|
|
.route(
|
|
"/api/matrix_link/logout",
|
|
web::post().to(matrix_link_controller::logout),
|
|
)
|
|
})
|
|
.workers(4)
|
|
.bind(&AppConfig::get().listen_address)?
|
|
.run()
|
|
.await?;
|
|
|
|
// Terminate manager actor
|
|
manager_actor.stop(None);
|
|
manager_actor_handle.await?;
|
|
|
|
Ok(())
|
|
}
|