master
veypi 3 years ago
parent f88ef1394e
commit b4f3b67535

2124
oab/Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,23 @@
[package]
name = "oab"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
include_dir = "*"
lazy_static = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "*"
serde_yaml = "*"
clap = { version = "3", features = ["derive"] }
time = { version = "0.3", features = ["formatting", "macros"] }
tokio = { version = "1", features = ["full"] }
tracing = "*"
tracing-subscriber = "*"
rbson = "2"
rbatis = { version = "*", default-features = false, features = ["runtime-tokio-rustls","mysql","debug_mode"] }

@ -0,0 +1,98 @@
//
// cfg.rs
// Copyright (C) 2022 veypi <veypi@qq.com>
// Distributed under terms of the MIT license.
//
//
//
//
//
use std::borrow::Borrow;
use lazy_static::lazy_static;
use rbatis::rbatis::Rbatis;
lazy_static! {
// Rbatis是线程、协程安全的运行时的方法是Send+Sync无需担心线程竞争
pub static ref DB:Rbatis=Rbatis::new();
}
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ApplicationConfig {
pub debug: bool,
pub server_url: String,
pub db_url: String,
pub db_user: String,
pub db_pass: String,
pub db_name: String,
pub log_dir: String,
/// "100MB" 日志分割尺寸-单位KB,MB,GB
pub log_temp_size: String,
pub log_pack_compress: String,
pub log_level: String,
pub jwt_secret: String,
}
impl ApplicationConfig {
pub fn new() -> Self {
ApplicationConfig {
debug: true,
server_url: "127.0.0.1:4000".to_string(),
db_url: "127.0.0.1:3306".to_string(),
db_user: "root".to_string(),
db_name: "test".to_string(),
db_pass: "123456".to_string(),
log_dir: "".to_string(),
log_temp_size: "".to_string(),
log_pack_compress: "".to_string(),
log_level: "".to_string(),
jwt_secret: "".to_string(),
}
}
pub fn db(&self) -> &DB {
DB.borrow()
}
pub async fn connect(&self) {
let url = format!(
"mysql://{}:{}@{}/{}",
self.db_user, self.db_pass, self.db_url, self.db_name
);
DB.link(&url).await.unwrap();
}
}
///默认配置
impl Default for ApplicationConfig {
fn default() -> Self {
let yml_data = include_str!("./mod.rs");
//读取配置
let result: ApplicationConfig =
serde_yaml::from_str(yml_data).expect("load config file fail");
if result.debug {
println!("load config:{:?}", result);
println!("///////////////////// Start On Debug Mode ////////////////////////////");
} else {
println!("release_mode is enable!")
}
result
}
}
struct FormatTime;
impl tracing_subscriber::fmt::time::FormatTime for FormatTime {
fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result {
let d =
time::OffsetDateTime::now_utc().to_offset(time::UtcOffset::from_hms(8, 0, 0).unwrap());
w.write_str(&format!(
"{} {}:{}:{}",
d.date(),
d.hour(),
d.minute(),
d.second()
))
}
}
pub fn init_log() {
tracing_subscriber::fmt().with_timer(FormatTime {}).init();
}

@ -0,0 +1,12 @@
//
// mod.rs
// Copyright (C) 2022 veypi <veypi@qq.com>
// 2022-05-29 18:39
// Distributed under terms of the MIT license.
//
//
mod cfg;
pub use cfg::init_log;
pub use cfg::ApplicationConfig;
pub use cfg::DB;

@ -0,0 +1,9 @@
//
// lib.rs
// Copyright (C) 2022 veypi <veypi@qq.com>
// 2022-05-29 18:48
// Distributed under terms of the MIT license.
//
pub mod cfg;
pub mod models;

@ -0,0 +1,15 @@
use oab::{cfg, models};
use tracing::{info, warn};
#[tokio::main]
async fn main() -> std::io::Result<()> {
let mut cf = cfg::ApplicationConfig::new();
cfg::init_log();
cf.log_dir = "".to_string();
cf.connect().await;
info!("{}", cf.server_url);
warn!("{}", cf.db_url);
models::init().await;
println!("Hello, world!");
return std::io::Result::Ok(());
}

@ -0,0 +1,20 @@
//
// mod.rs
// Copyright (C) 2022 veypi <veypi@qq.com>
// 2022-06-02 23:04
// Distributed under terms of the MIT license.
//
mod user;
use rbatis::crud::CRUD;
use tracing::info;
use user::User;
use crate::cfg;
pub async fn init() {
let mut u = User::default();
u.name = Some("asd".to_string());
cfg::DB.save(&u, &[]).await.unwrap();
info!("{:#?}", u);
}

@ -0,0 +1,25 @@
//
// user.rs
// Copyright (C) 2022 veypi <veypi@qq.com>
// 2022-06-02 23:03
// Distributed under terms of the MIT license.
//
//
use rbatis::crud_table;
#[crud_table]
#[derive(Debug, Clone)]
pub struct User {
pub id: String,
pub name: Option<String>,
}
impl Default for User {
fn default() -> Self {
User {
id: rbatis::plugin::object_id::ObjectId::new().to_string(),
name: None,
}
}
}
Loading…
Cancel
Save