From 14a3049ae7b74fe9d58199df4ca5f31879362b15 Mon Sep 17 00:00:00 2001 From: ZtRXR Date: Mon, 22 Jul 2024 12:05:55 +0800 Subject: [PATCH] update --- .gitignore | 1 + src/main.rs | 2 +- src/start_db.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 290f94f..61d5f95 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ Cargo.lock test.db test.db-journal perf.data +setings.json diff --git a/src/main.rs b/src/main.rs index 5d62209..f75caba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ async fn main() -> Result<(), Box> { env_logger::init(); println!("The program is starting.\nIf you want to see the log info,please set env RUST_LOG=info"); info!("DataBase is starting"); - let db_info = Starter::connect("sqlite://test.db?mode=rwc").await?; + let db_info = Starter::connect_from_path("setings.json").await?; db_info.create_table().await.unwrap_or_else(|e: Box|{ info!("The table have been created. Don't care this Error!!!!!:{}",e); diff --git a/src/start_db.rs b/src/start_db.rs index 19da66f..a90b81d 100644 --- a/src/start_db.rs +++ b/src/start_db.rs @@ -1,16 +1,65 @@ -use log::info; +use log::{error, info, warn}; use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseConnection}; -use std::{error::Error, time::Duration}; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use std::{error::Error, fs::File, io::{Read, Write}, process::exit, time::Duration}; use sea_orm::Schema; +#[derive(Debug,Deserialize,Serialize)] +pub struct Settings{ + pub connection_string:String, + pub connect_timeout_secs:u64, +} +impl Settings { + pub async fn new(file_path:&str) -> Self { + let mut json_file:File ; + let file_result =File::open(&file_path); + match file_result { + Ok(f)=>{ + json_file=f; + } + Err(e)=>{ + warn!("Error opening file {} try to create new: {}",&file_path, e); + json_file = File::create_new(&file_path) + .map_err(|e|{error!("Cannot create new file {}",&file_path);error!("{}",e)}) + .unwrap(); + let new_json_string: String = json!( + { + "connection_string":"sqlite://test.db?mode=rwc", + "connect_timeout_secs":360 + } + ).to_string(); + json_file.write(new_json_string.as_bytes()) + .map_err(|e|{error!("Cannot write to file {}",&file_path);error!("{}",e)}) + .unwrap(); + warn!("write file {} done , please check it",&file_path); + exit(1); + } + }; + + let mut json_file_string = String::new(); + json_file.read_to_string(&mut json_file_string) + .map_err(|e|{error!("Cannot read from file {}",&file_path);error!("{}",e)}) + .unwrap(); + let settings: Settings = serde_json::from_str(&json_file_string) + .map_err(|e|{error!("Cannot parse json from file {}",&file_path);error!("{}",e)}) + .unwrap(); + + Settings{ + connection_string: settings.connection_string, + connect_timeout_secs: settings.connect_timeout_secs, + } + } +} pub struct Starter { pub conn: DatabaseConnection, } -impl Starter { - pub async fn connect(db_url:&str) -> Result> { - let mut opt: ConnectOptions = ConnectOptions::new(db_url); - opt.connect_timeout(Duration::from_secs(360)); +impl Starter { + pub async fn connect_from_path(path:&str) -> Result> { + let settings =Settings::new(path).await; + let mut opt: ConnectOptions = ConnectOptions::new(settings.connection_string); + opt.connect_timeout(Duration::from_secs(settings.connect_timeout_secs)); let conn_new: DatabaseConnection = Database::connect(opt).await?; Ok(Starter{conn:conn_new})