This commit is contained in:
lychang
2026-03-22 04:02:47 +08:00
parent e4bf7cf1a4
commit f3ae54a26b
22 changed files with 10607 additions and 347 deletions

View File

@@ -1,14 +1,14 @@
mod config;
mod models;
mod database;
mod image_processor;
mod routes;
mod import;
mod models;
mod routes;
use actix_files as afs;
use actix_web::{web, App, HttpServer, HttpResponse};
use std::sync::Arc;
use actix_web::{App, HttpResponse, HttpServer, web};
use std::fs;
use std::sync::Arc;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
@@ -20,10 +20,10 @@ async fn main() -> std::io::Result<()> {
std::process::exit(1);
}
};
println!("启动图片展示服务...");
println!("服务地址: http://{}", config.server_address());
// 初始化数据库
let db = match database::Database::new(&config).await {
Ok(db) => {
@@ -35,21 +35,21 @@ async fn main() -> std::io::Result<()> {
std::process::exit(1);
}
};
// 在后台线程中自动导入新相册(不阻塞HTTP服务启动)
let config_clone = config.clone();
let db_clone = Arc::clone(&db);
tokio::spawn(async move {
println!("[后台任务] 开始扫描图片目录并导入新相册...");
match import::import_existing_data(&config_clone, &db_clone).await {
match import::import_existing_data(&config_clone, db_clone).await {
Ok(_) => println!("[后台任务] 相册导入完成"),
Err(e) => eprintln!("[后台任务] 相册导入失败: {}", e),
}
});
// 初始化图片处理器
let image_processor = Arc::new(image_processor::ImageProcessor::new(config.storage.clone()));
println!("API接口:");
println!(" - GET /api/albums - 获取所有相册");
println!(" - GET /api/search?q=关键词 - 搜索相册");
@@ -57,16 +57,21 @@ async fn main() -> std::io::Result<()> {
println!(" - POST /api/albums - 创建新相册");
println!(" - GET /api/thumbnail/:id - 获取缩略图");
println!(" - GET /api/original/:id - 获取原始图片");
println!(" - GET /api/favorites - 获取收藏列表");
println!(" - GET /api/favorites/status/:id - 检查收藏状态");
println!(" - POST /api/favorites - 添加收藏");
println!(" - DELETE /api/favorites/:id - 取消收藏");
println!(" - DELETE /api/favorites/image/:id - 通过图片ID取消收藏");
println!("按 Ctrl+C 停止服务");
// 创建数据目录
std::fs::create_dir_all(&config.storage.images_dir).unwrap_or(());
std::fs::create_dir_all(&config.storage.thumbnails_dir).unwrap_or(());
std::fs::create_dir_all("./data").unwrap_or(());
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(());
// 在闭包外提取需要的路径
let server_address = config.server_address();
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(Arc::clone(&db)))
@@ -77,26 +82,41 @@ async fn main() -> std::io::Result<()> {
.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/original/{id}",
web::get().to(routes::get_original_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", 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))
// 静态文件服务
.service(afs::Files::new("/assets", "./assets").show_files_listing())
// HTML页面路由
.route("/", web::get().to(|| async {
match fs::read_to_string("./assets/index.html") {
Ok(content) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(content),
Err(_) => HttpResponse::NotFound().body("首页文件不存在")
}
}))
.route("/album", web::get().to(|| async {
match fs::read_to_string("./assets/album.html") {
Ok(content) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(content),
Err(_) => HttpResponse::NotFound().body("相册页面文件不存在")
}
}))
.route(
"/",
web::get().to(|| async {
match fs::read_to_string("./assets/index.html") {
Ok(content) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(content),
Err(_) => HttpResponse::NotFound().body("首页文件不存在"),
}
}),
)
.route(
"/album",
web::get().to(|| async {
match fs::read_to_string("./assets/album.html") {
Ok(content) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(content),
Err(_) => HttpResponse::NotFound().body("相册页面文件不存在"),
}
}),
)
})
.bind(server_address)?
.run()