Files
quoter/src/main.rs

78 lines
2.4 KiB
Rust
Raw Normal View History

2024-08-31 03:32:37 +03:00
mod app_state;
mod auth;
2025-08-02 00:18:09 +03:00
mod core;
2024-08-31 03:32:37 +03:00
mod handlers;
2025-08-02 00:18:09 +03:00
mod lookup;
mod overlay;
2024-08-31 03:32:37 +03:00
mod s3_utils;
mod thumbnail;
2024-04-08 10:17:14 +03:00
2025-06-02 22:20:37 +03:00
use actix_cors::Cors;
2025-08-02 00:18:09 +03:00
use actix_web::{
http::header::{self, HeaderName},
middleware::Logger,
web, App, HttpServer,
};
2024-08-31 03:32:37 +03:00
use app_state::AppState;
2025-08-02 00:18:09 +03:00
use env_logger;
use handlers::{
get_quota_handler, increase_quota_handler, proxy_handler, root_handler, set_quota_handler,
upload_handler,
};
2024-10-23 16:21:18 +03:00
use log::warn;
2024-10-02 18:56:52 +03:00
use std::env;
2025-08-02 00:18:09 +03:00
use tokio::task::spawn_blocking;
2023-09-28 02:08:48 +03:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2024-10-02 18:56:52 +03:00
env_logger::init();
2024-10-23 16:21:18 +03:00
warn!("Started");
2024-08-30 22:11:21 +03:00
2024-10-22 13:18:57 +03:00
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
let addr = format!("0.0.0.0:{}", port);
let app_state = AppState::new().await;
2024-08-30 23:27:01 +03:00
let app_state_clone = app_state.clone();
2024-09-23 18:16:47 +03:00
2024-09-23 16:32:54 +03:00
// Используем spawn_blocking для работы, которая не совместима с Send
spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
rt.block_on(async move {
2024-10-22 19:34:08 +03:00
app_state_clone.cache_filelist().await;
2024-09-23 16:32:54 +03:00
});
2024-08-30 23:27:01 +03:00
});
2023-09-28 02:08:48 +03:00
HttpServer::new(move || {
2025-06-02 22:20:37 +03:00
// Настройка CORS middleware
let cors = Cors::default()
.allow_any_origin() // TODO: ограничить конкретными доменами в продакшене
2025-08-02 00:18:09 +03:00
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"])
2025-06-02 22:20:37 +03:00
.allowed_headers(vec![
header::DNT,
header::USER_AGENT,
HeaderName::from_static("x-requested-with"),
header::IF_MODIFIED_SINCE,
header::CACHE_CONTROL,
header::CONTENT_TYPE,
header::RANGE,
header::AUTHORIZATION,
])
2025-08-02 00:18:09 +03:00
.expose_headers(vec![header::CONTENT_LENGTH, header::CONTENT_RANGE])
2025-06-02 22:20:37 +03:00
.supports_credentials()
.max_age(1728000); // 20 дней
2023-09-28 02:08:48 +03:00
App::new()
2024-08-30 22:11:21 +03:00
.app_data(web::Data::new(app_state.clone()))
2025-06-02 22:20:37 +03:00
.wrap(cors)
2023-10-11 23:03:12 +03:00
.wrap(Logger::default())
2025-06-02 22:20:37 +03:00
.route("/", web::get().to(root_handler))
2024-08-31 03:32:37 +03:00
.route("/", web::post().to(upload_handler))
2025-08-02 00:18:09 +03:00
.route("/quota", web::get().to(get_quota_handler))
.route("/quota/increase", web::post().to(increase_quota_handler))
.route("/quota/set", web::post().to(set_quota_handler))
2024-10-02 18:29:08 +03:00
.route("/{path:.*}", web::get().to(proxy_handler))
2023-09-28 02:08:48 +03:00
})
2024-10-02 18:56:52 +03:00
.bind(addr)?
2023-09-28 02:08:48 +03:00
.run()
.await
2024-09-23 18:16:47 +03:00
}