feat: add print default config cmd
This commit is contained in:
37
src/main.rs
37
src/main.rs
@@ -1,8 +1,8 @@
|
||||
use crate::middleware::auth;
|
||||
use axum::{http::Method, Router};
|
||||
use clap::Parser;
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
use serde::Deserialize;
|
||||
use sea_orm::{Database, DatabaseConnection, Iden};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
use tower_http::trace::TraceLayer;
|
||||
@@ -18,7 +18,7 @@ mod query;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenvy::dotenv().unwrap();
|
||||
// dotenvy::dotenv().unwrap();
|
||||
// initialize tracing
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
@@ -35,6 +35,9 @@ async fn main() {
|
||||
eprintln!("Failed to load config from {}", config_path);
|
||||
}
|
||||
}
|
||||
Command::PrintExampleConfig {}=>{
|
||||
print_default_config().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,23 +46,23 @@ struct AppState {
|
||||
conn: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize,Serialize)]
|
||||
struct Key {
|
||||
jwt: String,
|
||||
user: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize,Serialize)]
|
||||
struct DatabaseConf {
|
||||
connection: String,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize,Serialize)]
|
||||
struct ServiceConf {
|
||||
host: String,
|
||||
port: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize,Serialize)]
|
||||
struct Config {
|
||||
service: ServiceConf,
|
||||
database: DatabaseConf,
|
||||
@@ -78,6 +81,7 @@ enum Command {
|
||||
#[arg(long = "conf")]
|
||||
config_path: String,
|
||||
},
|
||||
PrintExampleConfig {},
|
||||
}
|
||||
async fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
|
||||
let content = tokio::fs::read_to_string(path).await?;
|
||||
@@ -118,3 +122,22 @@ async fn start_server(config: &Config) {
|
||||
.await
|
||||
.expect("Service panic happened");
|
||||
}
|
||||
|
||||
async fn print_default_config() {
|
||||
let example_conf = Config{
|
||||
service: ServiceConf {
|
||||
host: "localhost".to_string(),
|
||||
port: 8080,
|
||||
},
|
||||
database: DatabaseConf {
|
||||
connection: "postgres://postgres:postgres@localhost/test_db".to_string(),
|
||||
},
|
||||
keys: Key {
|
||||
jwt: "THIS_IS_TEST_CONFIG".to_string(),
|
||||
user: "test_user".to_string(),
|
||||
},
|
||||
};
|
||||
// 序列化为 TOML 字符串
|
||||
let toml_string = toml::to_string(&example_conf);
|
||||
println!("#This is an example config.\n{}", toml_string.unwrap());
|
||||
}
|
||||
Reference in New Issue
Block a user