84 lines
2.9 KiB
Rust
84 lines
2.9 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::constants;
|
|
use matrixgw_backend::controllers::{auth_controller, server_controller};
|
|
use matrixgw_backend::users::User;
|
|
|
|
#[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());
|
|
|
|
let redis_store = RedisSessionStore::new(AppConfig::get().redis_connection_string())
|
|
.await
|
|
.expect("Failed to connect to Redis!");
|
|
|
|
// 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!");
|
|
}
|
|
|
|
log::info!(
|
|
"Starting to listen on {} for {}",
|
|
AppConfig::get().listen_address,
|
|
AppConfig::get().website_origin
|
|
);
|
|
|
|
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(vec!["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(RemoteIPConfig {
|
|
proxy: AppConfig::get().proxy_ip.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),
|
|
)
|
|
})
|
|
.workers(4)
|
|
.bind(&AppConfig::get().listen_address)?
|
|
.run()
|
|
.await
|
|
}
|