VirtWeb/virtweb_backend/src/main.rs

67 lines
2.4 KiB
Rust
Raw Normal View History

2023-09-02 07:12:36 +00:00
use actix_identity::config::LogoutBehaviour;
use actix_identity::IdentityMiddleware;
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-02 06:07:06 +00:00
use actix_web::middleware::Logger;
2023-09-02 07:12:36 +00:00
use actix_web::{web, App, HttpServer};
use std::time::Duration;
use virtweb_backend::app_config::AppConfig;
use virtweb_backend::constants::{
MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME,
};
2023-09-02 16:44:16 +00:00
use virtweb_backend::controllers::{auth_controller, server_controller};
2023-09-02 17:15:11 +00:00
use virtweb_backend::middlewares::auth_middleware::AuthChecker;
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"));
log::info!("Start to listen on {}", AppConfig::get().listen_address);
HttpServer::new(|| {
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)
.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-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-02 06:07:06 +00:00
.app_data(web::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-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-02 17:15:11 +00:00
.route(
"/api/auth/user",
web::get().to(auth_controller::current_user),
)
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
}