Compare commits

...

2 Commits

Author SHA1 Message Date
hz
144d13a1e9 feat(serv): 初始化数据库连接并集成SeaORM
- 添加 SeaORM 依赖并配置工作区共享
- 替换原有的 r2d2 数据库连接池实现
- 实现异步数据库初始化函数 init_database
- 在主函数中初始化数据库并注入应用上下文
- 移除旧的 sqlite 模块和相关路由配置文件
- 更新日志模块引用并增强环境变量打印测试
- 新增运行时信息打印工具模块 progress_running_info
- 调整模块结构,移除冗余的 router 模块定义
- 修正单元测试模块引用路径及内容适配新架构
2025-12-05 18:24:50 +08:00
hz
bb01f99fc0 refactor(bill): 重构 bank card 路由删除逻辑以支持 ID 参数
- 修改 DELETE 路由以接收 ID 参数
- 移除了冗余的 HTTP 请求示例文件
- 将 bill 包从主应用中解耦为库模块
- 更新 Cargo.toml 依赖关系以包含 bill 模块
- 升级多个工作区依赖版本
- 移除了已弃用的 docker-watcher 包及相关测试代码
- 修正数据库执行语句引用方式
- 添加 EntityTrait 引用以支持模型操作
2025-12-05 14:31:37 +08:00
26 changed files with 93 additions and 114 deletions

View File

@@ -1,24 +1,26 @@
[workspace]
resolver = "2"
members = [ "packages/docker-watcher","packages/gotify-ws", "packages/model", "packages/serv", "packages/hutils", "packages/bill"]
members = ["packages/model", "packages/serv", "packages/hutils", "packages/bill"]
#default-members = ["packages/gotify-ws"]
[workspace.dependencies]
gotify-ws = { path = "./packages/gotify-ws" }
tokio = { version = "1.47.1", features = ["full"] }
tokio = { version = "1.48.0", features = ["full"] }
# 这个和 sea-orm 的 feature 兼容性有点问题,所以只能用这个
rusqlite = { version = "0.32.1", features = ["bundled"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.143"
regex = "1.11.2"
sea-orm = { version = "1.1.14", features = [
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
regex = "1.12.2"
sea-orm = { version = "2.0.0-rc.20", features = [
"sqlx-sqlite",
"runtime-tokio-rustls",
"macros",
] }
chrono = "0.4.41"
chrono = "0.4.42"
env_logger = "0.11.8"
futures = "0.3.31"
log = "0.4.27"
actix-web = { version = "4.11.0" , features = ["rustls"]}
hutils = { path = "./packages/hutils"}
model = { path = "./packages/model"}
log = "0.4.29"
actix-web = { version = "4.12.1", features = ["rustls"] }
hutils = { path = "./packages/hutils" }
model = { path = "./packages/model" }
bill = { path = "./packages/bill" }

View File

@@ -9,4 +9,5 @@ serde.workspace = true
serde_json.workspace = true
tokio.workspace = true
hutils.workspace = true
log = "0.4.28"
log.workspace = true
sea-orm.workspace = true

View File

@@ -1,8 +0,0 @@
### 请求 card 数据
GET http://localhost:8080/bank-card
### 添加 card 数据
POST http://localhost:8080/bank-card
### 删除 card 数据
DELETE http://localhost:8080/bank-card

View File

@@ -0,0 +1,4 @@
### 获取银行卡列表
GET http://localhost:8080/bank_card
###

1
packages/bill/src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod router;

View File

@@ -1,19 +0,0 @@
mod router;
use actix_web::middleware::Logger;
use actix_web::{App, HttpServer, main};
use hutils::logger::init_logger;
#[main]
async fn main() -> std::io::Result<()> {
init_logger();
HttpServer::new(move || {
let app = App::new();
let app = app.configure(router::router_register);
let app = app.wrap(Logger::default());
app
})
.bind("0.0.0.0:8080")?
.run()
.await
}

View File

@@ -1,5 +1,6 @@
use actix_web::{delete, get, post, web};
use log::info;
use sea_orm::DatabaseConnection;
pub fn bank_card_router_configure(cfg: &mut web::ServiceConfig) {
cfg
@@ -8,7 +9,7 @@ pub fn bank_card_router_configure(cfg: &mut web::ServiceConfig) {
.service(delete_bank_card);
}
#[get("/bank-card")]
pub async fn get_bank_card() -> String {
pub async fn get_bank_card(db: web::Data<DatabaseConnection>) -> String {
info!("this is get bank card");
String::from("Bill Get")
}
@@ -18,7 +19,7 @@ pub async fn post_bank_card() -> String {
String::from("Bill Post")
}
#[delete("/bank-card")]
#[delete("/bank-card/{id}")]
pub async fn delete_bank_card() -> String {
info!("this is delete bank card");
String::from("Bill Delete")

View File

@@ -1,5 +1,7 @@
pub mod bank_card;
use actix_web::web;
pub mod bank_card;
pub fn router_register(cfg: &mut web::ServiceConfig) {
cfg.configure(bank_card::bank_card_router_configure);
}

View File

@@ -1,9 +0,0 @@
[package]
name = "docker-watcher"
version = "0.1.0"
edition = "2024"
[dependencies]
bollard = "0.19.2"
tokio.workspace = true
hutils.workspace = true

View File

@@ -1,3 +0,0 @@
mod test;
fn main() {}

View File

@@ -1 +0,0 @@
mod docker;

View File

@@ -1,7 +0,0 @@
use bollard::Docker;
#[tokio::test]
async fn docker_version() {
let docker = Docker::connect_with_local_defaults().expect("连接 docker 失败");
println!("{:#?}", docker.version().await.expect("获取 docker 版本失败"));
}

View File

@@ -1,4 +1,5 @@
use crate::utils::time::get_current_time;
use sea_orm::EntityTrait;
use sea_orm::prelude::DeriveEntityModel;
use sea_orm::{ActiveModelBehavior, DeriveRelation, EnumIter};
use sea_orm::{DerivePrimaryKey, PrimaryKeyTrait};

View File

@@ -1,8 +1,6 @@
use crate::model::ssh_connection::{ActiveModel, Entity};
use log::{info, warn};
use sea_orm::{
ActiveModelTrait, ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, Schema,
};
use sea_orm::{ActiveModelTrait, ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, Schema};
use std::sync::OnceLock;
static DB: OnceLock<Option<DatabaseConnection>> = OnceLock::new();
@@ -31,7 +29,7 @@ pub async fn create_ssh_connection_table() -> bool {
let statement = Schema::new(DatabaseBackend::Sqlite).create_table_from_entity(Entity);
let sql = db.get_database_backend().build(&statement);
match db.execute(sql).await {
match db.execute(&sql).await {
Ok(_) => {
info!("创建表成功!");
true

View File

@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
env_logger = "0.11.8"
log = "0.4.29"

View File

@@ -1 +1,2 @@
pub mod logger;
pub mod running_info;

View File

@@ -0,0 +1,9 @@
use log::info;
use std::env;
/**
* 获取正在运行的进程信息
* 前置条件 需要 初始化 logger
*/
pub fn progress_running_info() {
info!("当前项目工作路径为 {}", env::current_dir().unwrap().display());
}

View File

@@ -5,10 +5,10 @@ edition = "2024"
[dependencies]
tokio.workspace = true
gotify-ws.workspace = true
log.workspace = true
sea-orm.workspace = true
regex.workspace = true
actix-web.workspace = true
model.workspace = true
hutils.workspace = true
hutils.workspace = true
bill.workspace = true

View File

@@ -1,5 +1,27 @@
mod handler;
mod test;
mod utils;
#[tokio::main]
async fn main() {}
use crate::utils::database::init_database;
use actix_web::middleware::Logger;
use actix_web::{App, HttpServer, main};
use hutils::logger::init_logger;
use hutils::running_info::progress_running_info;
#[main]
async fn main() -> std::io::Result<()> {
init_logger();
progress_running_info();
let db = init_database().await;
HttpServer::new(move || {
let app = App::new();
let app = app.app_data(db.clone());
let app = app.configure(bill::router::router_register);
let app = app.wrap(Logger::default());
app
})
.bind("0.0.0.0:8080")?
.run()
.await
}

View File

@@ -1,9 +1,12 @@
use gotify_ws::utils::logger;
use hutils::logger::init_logger;
use log::info;
use std::env;
#[test]
fn test_base_api_env() {
logger::init_logger();
info!(r"current dir is {:?}", env::vars())
init_logger();
for (key, value) in env::vars() {
info!("{:?} = {:?}", key, value);
}
// info!(r"current dir is {:?}", env::vars())
}

View File

@@ -0,0 +1,7 @@
use sea_orm::{ActiveModelBehavior, DeriveEntityModel};
#[sea_orm::model]
#[derive(Debug, Clone, Copy, Eq, PartialEq, DeriveEntityModel)]
pub struct Model {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -1,20 +0,0 @@
use crate::handler::test;
use actix_web::{App, HttpServer};
#[tokio::test]
async fn run_application() {
println!("run application");
let serve = HttpServer::new(move || {
let app = App::new();
let app = app.service(test::test);
app
});
serve
.bind("127.0.0.1:8080")
.expect("端口绑定失败")
.run()
.await
.expect("服务启动失败");
}

View File

@@ -1,4 +1,4 @@
#[cfg(test)]
mod base_api;
mod id_card;
mod main;
mod identity_card;

View File

@@ -1,14 +1,15 @@
use actix_web::web;
use actix_web::web::Data;
use r2d2::{Pool, State};
use r2d2_sqlite::SqliteConnectionManager;
use log::{info, warn};
use sea_orm::{Database, DatabaseConnection, DbErr};
pub struct SqliteState {
pool: Pool<SqliteConnectionManager>,
}
pub fn init_database() -> Data<SqliteState> {
let db = SqliteConnectionManager::file("./database.mod");
let pool = Pool::new(db).unwrap();
Data::new(SqliteState { pool: pool.clone() })
pub async fn init_database() -> Result<DatabaseConnection, DbErr> {
match Database::connect("sqlite:///home/hz/repo/hz/home-api/serv.sqlite?mode=rwc").await {
Ok(conn) => {
info!("数据库连接成功");
Ok(conn)
}
Err(err) => {
warn!("数据库连接失败 {}", err);
Err(err)
}
}
}

View File

@@ -1,2 +1 @@
pub mod database;
pub mod router;

View File

@@ -1,7 +0,0 @@
use actix_web::web;
use crate::handler::location;
pub fn location(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/location").service(location::location));
}