update
This commit is contained in:
parent
4d458afeb6
commit
14a3049ae7
1
.gitignore
vendored
1
.gitignore
vendored
@ -23,3 +23,4 @@ Cargo.lock
|
||||
test.db
|
||||
test.db-journal
|
||||
perf.data
|
||||
setings.json
|
||||
|
@ -14,7 +14,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
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<dyn Error>|{
|
||||
info!("The table have been created. Don't care this Error!!!!!:{}",e);
|
||||
|
@ -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<Self, Box<dyn Error>> {
|
||||
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<Self, Box<dyn Error>> {
|
||||
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})
|
||||
|
Loading…
Reference in New Issue
Block a user