feat(serv): 初始化数据库连接并集成SeaORM
- 添加 SeaORM 依赖并配置工作区共享 - 替换原有的 r2d2 数据库连接池实现 - 实现异步数据库初始化函数 init_database - 在主函数中初始化数据库并注入应用上下文 - 移除旧的 sqlite 模块和相关路由配置文件 - 更新日志模块引用并增强环境变量打印测试 - 新增运行时信息打印工具模块 progress_running_info - 调整模块结构,移除冗余的 router 模块定义 - 修正单元测试模块引用路径及内容适配新架构
This commit is contained in:
@@ -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
|
||||
4
packages/bill/src/http_request/bank_card.http
Normal file
4
packages/bill/src/http_request/bank_card.http
Normal file
@@ -0,0 +1,4 @@
|
||||
### 获取银行卡列表
|
||||
GET http://localhost:8080/bank_card
|
||||
|
||||
###
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -5,3 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.11.8"
|
||||
log = "0.4.29"
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod logger;
|
||||
pub mod running_info;
|
||||
|
||||
9
packages/hutils/src/running_info.rs
Normal file
9
packages/hutils/src/running_info.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use log::info;
|
||||
use std::env;
|
||||
/**
|
||||
* 获取正在运行的进程信息
|
||||
* 前置条件 需要 初始化 logger
|
||||
*/
|
||||
pub fn progress_running_info() {
|
||||
info!("当前项目工作路径为 {}", env::current_dir().unwrap().display());
|
||||
}
|
||||
@@ -5,7 +5,6 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
tokio.workspace = true
|
||||
gotify-ws.workspace = true
|
||||
log.workspace = true
|
||||
sea-orm.workspace = true
|
||||
regex.workspace = true
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
mod handler;
|
||||
mod test;
|
||||
mod utils;
|
||||
|
||||
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
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
7
packages/serv/src/test/identity_card.rs
Normal file
7
packages/serv/src/test/identity_card.rs
Normal 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 {}
|
||||
@@ -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("服务启动失败");
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#[cfg(test)]
|
||||
mod base_api;
|
||||
mod id_card;
|
||||
mod main;
|
||||
mod identity_card;
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
pub mod database;
|
||||
pub mod router;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user