Add Json Config Sys

This commit is contained in:
FlyingBlackShark 2022-07-19 14:25:31 +08:00
parent aec5d2c436
commit f128196b52
9 changed files with 91 additions and 46 deletions

4
.gitignore vendored
View File

@ -17,4 +17,6 @@ backend/data
*.iml
backend/upload
blog.dat*
export/
export/
**/asset/
**/dist/

1
backend/.gitignore vendored
View File

@ -14,3 +14,4 @@ Cargo.lock
/target
.idea
asset/

View File

@ -0,0 +1,56 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use serde_json::*;
use std::fs;
pub fn load_config(args: &mut Args) -> Result<()> {
let data = fs::read_to_string(args.config.as_ref().unwrap()).unwrap();
let v: Args = serde_json::from_str(data.as_str())?;
*args = v;
// let b = Box::leak(Box::new(v));
// args = b;
Ok(())
}
/// Simple blog backend
#[derive(Parser, Serialize, Deserialize)]
#[clap(name = "Songday blog backend", author, version, about, long_about = None)]
pub struct Args {
#[clap(long, value_parser)]
/// Specify config path, e.g.: ./config.json
pub config: Option<String>,
/// Specify run mode: 'static' is for static file serve, 'blog' is blog warp server mode
#[clap(long, value_parser)]
pub mode: Option<String>,
/// HTTP Server Settings
/// Specify http listening address, e.g.: 0.0.0.0 or [::] or 127.0.0.1 or other particular ip, default is '127.0.0.1'
#[clap(long, default_value = "127.0.0.1", value_parser)]
pub ip: String,
/// Specify listening port, default value is '80'
#[clap(long, default_value_t = 80, value_parser)]
pub port: u16,
/// Enable HTTPS Server
#[clap(long, value_parser)]
pub https_enabled: bool,
/// Cert file path, needed by https
#[clap(long, value_parser)]
pub cert_path: Option<String>,
/// Key file path, needed by https
#[clap(long, value_parser)]
pub key_path: Option<String>,
/// Specify HTTPS listening port, default value is '443'
#[clap(long, value_parser, default_value_t = 443)]
pub https_port: u16,
/// Enable HSTS Redirect Server
#[clap(long, value_parser)]
pub hsts_enabled: bool,
/// Hostname for CORS
#[clap(long, value_parser)]
pub cors_host: Option<String>,
}

View File

@ -0,0 +1,11 @@
{
"mode":"blog",
"ip":"0.0.0.0",
"port":80,
"https_enabled":false,
"https_port":443,
"cert_path":"./cert.crt",
"key_path":"./key.key",
"hsts_enabled":false,
"cors_host":"https://localhost"
}

View File

@ -0,0 +1 @@
pub mod config_loader;

View File

@ -12,3 +12,4 @@ mod facade;
mod image;
pub mod service;
pub mod util;
pub mod config;

View File

@ -2,7 +2,7 @@
use std::net::SocketAddr;
use blog_backend::{db, service, util::result};
use blog_backend::{db, service, util::result,config::{config_loader, self}};
use clap::Parser;
use futures::future::{join_all, BoxFuture};
use tokio::{
@ -10,56 +10,27 @@ use tokio::{
sync::broadcast,
};
/// Simple blog backend
#[derive(Parser)]
#[clap(name = "Songday blog backend", author, version, about, long_about = None)]
struct Args {
/// Specify run mode: 'static' is for static file serve, 'blog' is blog warp server mode
#[clap(long, value_parser)]
mode: Option<String>,
/// HTTP Server Settings
/// Specify http listening address, e.g.: 0.0.0.0 or [::] or 127.0.0.1 or other particular ip, default is '127.0.0.1'
#[clap(long, default_value = "127.0.0.1", value_parser)]
ip: String,
/// Specify listening port, default value is '80'
#[clap(long, default_value_t = 80, value_parser)]
port: u16,
/// Enable HTTPS Server
#[clap(long, value_parser)]
https_enabled: bool,
/// Cert file path, needed by https
#[clap(long, value_parser)]
cert_path: Option<String>,
/// Key file path, needed by https
#[clap(long, value_parser)]
key_path: Option<String>,
/// Specify HTTPS listening port, default value is '443'
#[clap(long, value_parser, default_value_t = 443)]
https_port: u16,
/// Enable HSTS Redirect Server
#[clap(long, value_parser)]
hsts_enabled: bool,
/// Hostname for CORS
#[clap(long, value_parser)]
cors_host: Option<String>,
}
fn main() -> result::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "access-log=info");
}
pretty_env_logger::init();
let args = Args::parse();
let mut args = crate::config_loader::Args::parse();
if args.config.is_some(){
let config_result = config_loader::load_config(&mut args);
match config_result{
Ok(_)=>{
println!("Config Loaded")
},
Err(_)=>{
panic!("Config Invalid!");
},
_=>()
}
}
let runtime = Builder::new_multi_thread()
.worker_threads(4)
.enable_all()

3
common/.gitignore vendored
View File

@ -11,4 +11,5 @@ Cargo.lock
bin/
pkg/
wasm-pack.log
wasm-pack.log
**/dist/

1
frontend/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**/asset