Skip to content

Commit

Permalink
fix: parse config logic, allowing to load config from env only
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Sep 15, 2023
1 parent d90a39c commit 06eedb4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 25 deletions.
11 changes: 3 additions & 8 deletions src/backend/controller/index/jsdelivr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use std::{
use tracing::{error, instrument};

use crate::utils::response::{fail, fail_with_message, APIResponse};
use crate::{
cache,
CONFIG,
};
use crate::{cache, CONFIG};

use self::types::FetchJSDelivrFailureError;

Expand Down Expand Up @@ -99,11 +96,9 @@ async fn remember_jsdelivr_resource(
}
let (mime, data) = fetch_jsdelivr(path).await?;
// 保存到 Redis
conn
.set_ex(format!("{}_mime", key), mime.clone(), 60 * 60 * 2)
conn.set_ex(format!("{}_mime", key), mime.clone(), 60 * 60 * 2)
.await?;
conn
.set_ex(format!("{}_data", key), data.to_vec(), 60 * 60 * 2)
conn.set_ex(format!("{}_data", key), data.to_vec(), 60 * 60 * 2)
.await?;
Ok((mime, data))
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/controller/index/jsdelivr/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum FetchJSDelivrFailureError {
#[error("RequestStatusCheck failed: {0}")]
RequestStatusCheck(u16),
#[error("RequestContentTypeConvert: {0}")]
RequestContentTypeConvert( #[from] reqwest::header::ToStrError),
RequestContentTypeConvert(#[from] reqwest::header::ToStrError),
#[error("CacheError::Pool: {0}")]
RedisPool(#[from] deadpool_redis::PoolError),
#[error("CacheError::Redis: {0}")]
Expand Down Expand Up @@ -68,4 +68,4 @@ impl From<reqwest::header::ToStrError> for FetchJSDelivrFailureError {
FetchJSDelivrFailureError::RequestContentTypeConvert(e)
}
}
*/
*/
33 changes: 33 additions & 0 deletions src/conf/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,46 @@ use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct Database {
#[serde(default = "Database::default_host")]
pub host: String,
#[serde(default = "Database::default_port")]
pub port: u16,
#[serde(default = "Database::default_user")]
pub user: String,
#[serde(default)]
pub password: String,
#[serde(default)]
pub database: String,
#[serde(default)]
pub prefix: String,
#[serde(default)]
pub charset: String,
}

impl Database {
fn default_host() -> String {
"127.0.0.1".into()
}

fn default_port() -> u16 {
3306
}

fn default_user() -> String {
"root".into()
}
}

impl Default for Database {
fn default() -> Self {
Database {
host: Database::default_host(),
port: Database::default_port(),
user: Database::default_user(),
password: "".into(),
database: "".into(),
prefix: "".into(),
charset: "".into(),
}
}
}
10 changes: 10 additions & 0 deletions src/conf/jsdelivr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ pub struct Jsdelivr {
pub user_agent: Option<String>,
pub referer: Option<String>,
}

impl Default for Jsdelivr {
fn default() -> Self {
Jsdelivr {
mirror: Some("https://cdn.jsdelivr.net".into()),
user_agent: None,
referer: None,
}
}
}
28 changes: 19 additions & 9 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ use rabbitmq::RabbitMQ;
#[derive(Deserialize)]
pub struct Config {
pub env: Environment,
#[serde(default)]
pub database: Database,
#[serde(default)]
pub jsdelivr: Jsdelivr,
#[serde(default)]
pub redis: Redis,
#[serde(default)]
pub rabbitmq: RabbitMQ,
pub server: server::Server
#[serde(default)]
pub server: server::Server,
}

impl Config {
Expand All @@ -38,17 +43,22 @@ impl Config {
}
};
super::logger::init(env == "Development")?; // 初始化 Logger
let mut builder = conf::builder()
.set_override("env", env)? // 初始化运行环境
.add_source(File::with_name("conf/config.toml").required(false))
.add_source(File::with_name("config.toml").required(false))
.add_source(File::with_name("../conf/config.toml").required(false))
.add_source(File::with_name("../config.toml").required(false))
.add_source(Env::with_prefix("HITOKOTO_POLL"));
let mut builder = conf::builder().set_override("env", env)?; // 初始化运行环境
builder = if let Some(path) = config_path {
builder.add_source(File::with_name(&path))
builder.add_source(File::with_name(&path).required(true))
} else {
builder
.add_source(File::with_name("conf/config").required(false))
.add_source(File::with_name("config/config").required(false))
.add_source(File::with_name("data/config").required(false))
.add_source(File::with_name("config").required(false))
.add_source(File::with_name("../conf/config").required(false))
.add_source(File::with_name("../config").required(false))
.add_source(
Env::with_prefix("JSDRLIVR_PROXY")
.try_parsing(true)
.separator("_"),
)
}; // 交回所有权
let settings = builder.build()?.try_deserialize::<Self>()?;
Ok(settings)
Expand Down
34 changes: 34 additions & 0 deletions src/conf/rabbitmq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,44 @@ use serde::Deserialize;

#[derive(Deserialize)]
pub struct RabbitMQ {
#[serde(default = "RabbitMQ::default_host")]
pub host: String,
#[serde(default = "RabbitMQ::default_port")]
pub port: u16,
#[serde(default = "RabbitMQ::default_user")]
pub user: String,
#[serde(default = "RabbitMQ::default_pass")]
pub password: String,
#[serde(default)]
pub vhost: String,
}

impl RabbitMQ {
pub fn default_host() -> String {
"127.0.0.1".into()
}

pub fn default_port() -> u16 {
5672
}

pub fn default_user() -> String {
"myuser".into()
}

pub fn default_pass() -> String {
"mypass".into()
}
}

impl Default for RabbitMQ {
fn default() -> Self {
RabbitMQ {
host: RabbitMQ::default_host(),
port: RabbitMQ::default_port(),
user: RabbitMQ::default_user(),
password: RabbitMQ::default_pass(),
vhost: "".into(),
}
}
}
27 changes: 27 additions & 0 deletions src/conf/redis.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Redis {
#[serde(default = "Redis::default_host")]
pub host: String,
#[serde(default = "Redis::default_port")]
pub port: u16,
pub password: Option<String>,
pub username: Option<String>,
#[serde(default = "Redis::default_database")]
pub database: i64,
}

impl Redis {
fn default_host() -> String {
"127.0.0.1".into()
}

fn default_port() -> u16 {
6379
}

fn default_database() -> i64 {
0
}

pub fn to_uri(&self) -> String {
format!(
"redis://{}{}{}:{}/{}",
Expand All @@ -26,3 +41,15 @@ impl Redis {
)
}
}

impl Default for Redis {
fn default() -> Self {
Redis {
host: Redis::default_host(),
port: Redis::default_port(),
password: None,
username: None,
database: Redis::default_database(),
}
}
}
21 changes: 15 additions & 6 deletions src/conf/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Server {
pub host: Option<String>,
pub port: Option<u16>,
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Server {
pub host: Option<String>,
pub port: Option<u16>,
}

impl Default for Server {
fn default() -> Self {
Server {
host: Some("0.0.0.0".to_string()),
port: Some(28319),
}
}
}

0 comments on commit 06eedb4

Please sign in to comment.