feat: add print default config cmd

This commit is contained in:
brian
2025-09-21 18:40:41 +08:00
parent cade85d576
commit daf6c7c16a
4 changed files with 110 additions and 39 deletions

View File

@@ -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());
}