VirtWeb/virtweb_backend/src/main.rs

125 lines
4.6 KiB
Rust
Raw Normal View History

2023-09-04 12:11:56 +00:00
use actix_cors::Cors;
2023-09-02 07:12:36 +00:00
use actix_identity::config::LogoutBehaviour;
use actix_identity::IdentityMiddleware;
2023-09-05 11:19:25 +00:00
use actix_multipart::form::tempfile::TempFileConfig;
use actix_multipart::form::MultipartFormConfig;
2023-09-02 06:07:06 +00:00
use actix_remote_ip::RemoteIPConfig;
2023-09-02 07:12:36 +00:00
use actix_session::storage::CookieSessionStore;
use actix_session::SessionMiddleware;
use actix_web::cookie::{Key, SameSite};
2023-09-04 12:11:56 +00:00
use actix_web::http::header;
2023-09-02 06:07:06 +00:00
use actix_web::middleware::Logger;
2023-09-04 09:25:03 +00:00
use actix_web::web::Data;
2023-09-02 07:12:36 +00:00
use actix_web::{web, App, HttpServer};
2023-09-04 09:25:03 +00:00
use light_openid::basic_state_manager::BasicStateManager;
2023-09-02 07:12:36 +00:00
use std::time::Duration;
use virtweb_backend::app_config::AppConfig;
2023-09-05 11:19:25 +00:00
use virtweb_backend::constants;
2023-09-02 07:12:36 +00:00
use virtweb_backend::constants::{
MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME,
};
2023-09-05 11:19:25 +00:00
use virtweb_backend::controllers::{auth_controller, iso_controller, server_controller};
2023-09-02 17:15:11 +00:00
use virtweb_backend::middlewares::auth_middleware::AuthChecker;
2023-09-05 11:19:25 +00:00
use virtweb_backend::utils::files_utils;
2023-09-02 06:07:06 +00:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2023-09-05 11:19:25 +00:00
log::debug!("Create required directory, if missing");
files_utils::create_directory_if_missing(&AppConfig::get().iso_storage_path()).unwrap();
2023-09-02 06:07:06 +00:00
log::info!("Start to listen on {}", AppConfig::get().listen_address);
2023-09-04 09:25:03 +00:00
let state_manager = Data::new(BasicStateManager::new());
HttpServer::new(move || {
2023-09-02 07:12:36 +00:00
let session_mw = SessionMiddleware::builder(
CookieSessionStore::default(),
Key::from(AppConfig::get().secret().as_bytes()),
)
.cookie_name(SESSION_COOKIE_NAME.to_string())
.cookie_secure(AppConfig::get().cookie_secure)
.cookie_same_site(SameSite::Strict)
2023-09-04 13:12:00 +00:00
.cookie_domain(AppConfig::get().cookie_domain())
.cookie_http_only(true)
2023-09-02 07:12:36 +00:00
.build();
let identity_middleware = IdentityMiddleware::builder()
.logout_behaviour(LogoutBehaviour::PurgeSession)
.visit_deadline(Some(Duration::from_secs(MAX_INACTIVITY_DURATION)))
.login_deadline(Some(Duration::from_secs(MAX_SESSION_DURATION)))
.build();
2023-09-04 12:11:56 +00:00
let cors = Cors::default()
.allowed_origin(&AppConfig::get().website_origin)
.allowed_methods(vec!["GET", "POST", "DELETE", "PUT"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.supports_credentials()
.max_age(3600);
2023-09-02 06:07:06 +00:00
App::new()
.wrap(Logger::default())
2023-09-02 17:15:11 +00:00
.wrap(AuthChecker)
2023-09-02 07:12:36 +00:00
.wrap(identity_middleware)
.wrap(session_mw)
2023-09-04 13:12:00 +00:00
.wrap(cors)
2023-09-04 09:25:03 +00:00
.app_data(state_manager.clone())
.app_data(Data::new(RemoteIPConfig {
2023-09-02 07:12:36 +00:00
proxy: AppConfig::get().proxy_ip.clone(),
2023-09-02 06:07:06 +00:00
}))
2023-09-05 11:19:25 +00:00
// Uploaded files
.app_data(MultipartFormConfig::default().total_limit(constants::ISO_MAX_SIZE))
.app_data(TempFileConfig::default().directory(&AppConfig::get().temp_dir))
2023-09-02 16:44:16 +00:00
// Server controller
2023-09-02 06:07:06 +00:00
.route("/", web::get().to(server_controller::root_index))
2023-09-02 08:28:08 +00:00
.route(
"/api/server/static_config",
web::get().to(server_controller::static_config),
)
2023-09-02 16:44:16 +00:00
// Auth controller
.route(
"/api/auth/local",
web::post().to(auth_controller::local_auth),
)
2023-09-04 09:25:03 +00:00
.route(
"/api/auth/start_oidc",
web::get().to(auth_controller::start_oidc),
)
.route(
"/api/auth/finish_oidc",
web::post().to(auth_controller::finish_oidc),
)
2023-09-02 17:15:11 +00:00
.route(
"/api/auth/user",
web::get().to(auth_controller::current_user),
)
2023-09-04 09:25:03 +00:00
.route(
"/api/auth/sign_out",
web::get().to(auth_controller::sign_out),
)
2023-09-05 11:19:25 +00:00
// ISO controller
.route(
"/api/iso/upload",
web::post().to(iso_controller::upload_file),
)
2023-09-05 14:12:20 +00:00
.route(
"/api/iso/upload_from_url",
web::post().to(iso_controller::upload_from_url),
)
2023-09-06 12:21:26 +00:00
.route("/api/iso/list", web::get().to(iso_controller::get_list))
2023-09-06 14:57:38 +00:00
.route(
"/api/iso/{filename}",
web::get().to(iso_controller::download_file),
)
2023-09-06 12:55:41 +00:00
.route(
"/api/iso/{filename}",
web::delete().to(iso_controller::delete_file),
)
2023-09-02 06:07:06 +00:00
})
2023-09-02 07:12:36 +00:00
.bind(&AppConfig::get().listen_address)?
.run()
.await
2023-09-01 08:59:53 +00:00
}