update code level
This commit is contained in:
80
src/main.rs
80
src/main.rs
@@ -3,22 +3,23 @@ mod database;
|
||||
mod image_processor;
|
||||
mod import;
|
||||
mod models;
|
||||
mod routes;
|
||||
mod queue;
|
||||
mod routes;
|
||||
|
||||
use crate::models::AlbumsCache;
|
||||
use actix_files as afs;
|
||||
use actix_web::{App, HttpResponse, HttpServer, web, middleware};
|
||||
use actix_web::{App, HttpResponse, HttpServer, middleware, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{interval, Duration};
|
||||
use tracing::{info, error};
|
||||
use crate::models::AlbumsCache;
|
||||
|
||||
use tokio::time::{Duration, interval};
|
||||
use tracing::{error, info};
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// 初始化结构化日志
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
|
||||
// 加载配置
|
||||
let config = match config::Config::load() {
|
||||
Ok(cfg) => cfg,
|
||||
@@ -29,7 +30,7 @@ async fn main() -> std::io::Result<()> {
|
||||
};
|
||||
|
||||
info!("启动图片展示服务...");
|
||||
info!("服务地址: http://{}", config.server_address());
|
||||
info!("服务地址: http://localhost:{} http://{}", config.server.port, config.server_address());
|
||||
|
||||
// 初始化数据库
|
||||
let db = match database::Database::new(&config).await {
|
||||
@@ -46,11 +47,12 @@ async fn main() -> std::io::Result<()> {
|
||||
let image_processor = Arc::new(image_processor::ImageProcessor::new(config.storage.clone()));
|
||||
|
||||
// 初始化智能任务队列
|
||||
let task_queue = Arc::new(queue::TaskQueue::new(
|
||||
config.queue.max_concurrent_tasks, // 最大并发任务数
|
||||
config.queue.queue_size_limit, // 队列大小限制
|
||||
config.queue.high_request_rate_threshold, // 高请求频率阈值
|
||||
config.queue.idle_request_rate_threshold, // 空闲请求频率阈值
|
||||
let task_queue = Arc::new(queue::TaskQueue::new_with_db(
|
||||
config.queue.max_concurrent_tasks, // 最大并发任务数
|
||||
config.queue.queue_size_limit, // 队列大小限制
|
||||
config.queue.high_request_rate_threshold, // 高请求频率阈值
|
||||
config.queue.idle_request_rate_threshold, // 空闲请求频率阈值
|
||||
Arc::clone(&db), // 数据库实例
|
||||
));
|
||||
|
||||
// 在后台线程中自动导入新相册(不阻塞HTTP服务启动)
|
||||
@@ -65,15 +67,6 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
// 启动缩略图生成Worker
|
||||
let thumbnail_worker = queue::ThumbnailWorker::new(
|
||||
Arc::clone(&task_queue),
|
||||
Arc::clone(&image_processor),
|
||||
);
|
||||
tokio::spawn(async move {
|
||||
thumbnail_worker.start().await;
|
||||
});
|
||||
|
||||
// 启动相册导入Worker
|
||||
let album_import_worker = queue::AlbumImportWorker::new(
|
||||
Arc::clone(&task_queue),
|
||||
@@ -125,13 +118,18 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
// 启动定时清理过期任务的任务(每小时执行一次)
|
||||
let task_queue_for_cleanup = Arc::clone(&task_queue);
|
||||
tokio::spawn(async move {
|
||||
task_queue_for_cleanup.start_periodic_cleanup(Duration::from_secs(60 * 60)).await; // 1小时
|
||||
});
|
||||
|
||||
println!("API接口:");
|
||||
println!(" - GET /api/albums - 获取所有相册");
|
||||
println!(" - GET /api/search?q=关键词 - 搜索相册");
|
||||
println!(" - GET /api/album/:id - 获取相册详情");
|
||||
println!(" - POST /api/albums - 创建新相册");
|
||||
println!(" - GET /api/thumbnail/:id - 获取缩略图");
|
||||
println!(" - GET /api/original/:id - 获取原始图片");
|
||||
println!(" - GET /api/image/:id - 获取图片(已移除缩略图功能)");
|
||||
println!(" - GET /api/favorites - 获取收藏列表");
|
||||
println!(" - GET /api/favorites/status/:id - 检查收藏状态");
|
||||
println!(" - POST /api/favorites - 添加收藏");
|
||||
@@ -141,7 +139,7 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
// 创建数据目录
|
||||
fs::create_dir_all(&config.storage.images_dir).unwrap_or(());
|
||||
fs::create_dir_all(&config.storage.thumbnails_dir).unwrap_or(());
|
||||
// 不再创建缩略图目录,因为已移除缩略图功能
|
||||
fs::create_dir_all(&config.database.url[7..]).unwrap_or(());
|
||||
|
||||
// 在闭包外提取需要的路径
|
||||
@@ -156,29 +154,39 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(web::Data::new(Arc::clone(&db)))
|
||||
.app_data(web::Data::new(Arc::clone(&image_processor)))
|
||||
.app_data(web::Data::new(Arc::clone(&albums_cache)))
|
||||
.app_data(web::Data::new(Arc::clone(&task_queue)))
|
||||
// 不再需要任务队列作为app_data,因为已移除缩略图功能
|
||||
// API路由
|
||||
.route("/api/albums", web::get().to(routes::get_albums))
|
||||
.route("/api/search", web::get().to(routes::search_albums))
|
||||
.route("/api/album/random", web::get().to(routes::get_random_album))
|
||||
.route("/api/album/{id}", web::get().to(routes::get_album_detail))
|
||||
.route("/api/albums", web::post().to(routes::create_album))
|
||||
.route("/api/thumbnail/{id}", web::get().to(routes::get_thumbnail))
|
||||
.route("/api/original/{id}", web::get().to(routes::get_original_image))
|
||||
.route("/api/image/{id}", web::get().to(routes::get_image))
|
||||
// 收藏功能API路由
|
||||
.route("/api/favorites", web::get().to(routes::get_favorites))
|
||||
.route("/api/favorites/status/{id}", web::get().to(routes::check_favorite_status))
|
||||
.route(
|
||||
"/api/favorites/status/{id}",
|
||||
web::get().to(routes::check_favorite_status),
|
||||
)
|
||||
.route("/api/favorites", web::post().to(routes::add_favorite))
|
||||
.route("/api/favorites/{id}", web::delete().to(routes::remove_favorite))
|
||||
.route("/api/favorites/image/{id}", web::delete().to(routes::remove_favorite_by_image))
|
||||
.route(
|
||||
"/api/favorites/{id}",
|
||||
web::delete().to(routes::remove_favorite),
|
||||
)
|
||||
.route(
|
||||
"/api/favorites/image/{id}",
|
||||
web::delete().to(routes::remove_favorite_by_image),
|
||||
)
|
||||
// 队列状态API路由
|
||||
.route("/api/queue/stats", web::get().to(routes::get_queue_stats))
|
||||
// 静态文件服务 - 启用缓存
|
||||
.service(afs::Files::new("/assets", "./assets")
|
||||
.show_files_listing()
|
||||
.prefer_utf8(true)
|
||||
.use_last_modified(true)
|
||||
.use_etag(true))
|
||||
.service(
|
||||
afs::Files::new("/assets", "./assets")
|
||||
.show_files_listing()
|
||||
.prefer_utf8(true)
|
||||
.use_last_modified(true)
|
||||
.use_etag(true),
|
||||
)
|
||||
// HTML页面路由
|
||||
.route(
|
||||
"/",
|
||||
|
||||
Reference in New Issue
Block a user