feat: [阶段1] 项目初始化和基础设置

- 创建 Cargo.toml 配置文件,包含所有必要依赖
- 建立完整的项目模块结构(config, models, handlers, routes, services, storage, middleware, utils)
- 实现用户数据模型和内存存储
- 创建基础的 HTTP 处理器和路由配置
- 添加错误处理和 JWT 认证中间件
- 配置环境变量和日志系统
- 创建项目文档和学习指南
- 服务器可以成功编译和启动
This commit is contained in:
2025-08-04 16:49:50 +08:00
commit 28afc7532f
24 changed files with 2170 additions and 0 deletions

5
src/services/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
//! 业务逻辑服务模块
pub mod user_service;
pub use user_service::UserService;

View File

@@ -0,0 +1,30 @@
//! 用户业务逻辑服务
use crate::models::user::User;
use crate::storage::memory::MemoryUserStore;
use crate::utils::errors::ApiError;
/// 用户服务
pub struct UserService {
store: MemoryUserStore,
}
impl UserService {
pub fn new(store: MemoryUserStore) -> Self {
Self { store }
}
/// 验证用户凭据
pub async fn authenticate_user(&self, username: &str, password: &str) -> Result<User, ApiError> {
match self.store.get_user_by_username(username).await {
Some(user) => {
if user.password_hash == format!("hashed_{}", password) {
Ok(user)
} else {
Err(ApiError::Unauthorized)
}
}
None => Err(ApiError::NotFound("用户不存在".to_string())),
}
}
}