-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
228e8c0
commit 9ddb294
Showing
11 changed files
with
101 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::str::FromStr; | ||
|
||
use super::Profile; | ||
use config::ConfigError; | ||
|
||
pub fn get_env_source(prefix: &str) -> config::Environment { | ||
config::Environment::with_prefix(prefix) | ||
.prefix_separator("__") | ||
.separator("__") | ||
} | ||
|
||
pub fn get_profile() -> Result<Profile, config::ConfigError> { | ||
std::env::var("APP_PROFILE") | ||
.map(|env| Profile::from_str(&env).map_err(|e| ConfigError::Message(e.to_string()))) | ||
.unwrap_or_else(|_e| Ok(Profile::Dev)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ use utoipa::OpenApi; | |
|
||
use crate::{ | ||
client::{email::EmailClient, http::HttpClient, redis::RedisClient, ClientBuilder}, | ||
configure::template::TemplateEngine, | ||
configure::{env::get_env_source, get_static_dir, template::TemplateEngine}, | ||
handler::openapi::ApiDoc, | ||
util, | ||
}; | ||
|
||
pub const ENV_PREFIX: &str = "APP"; | ||
pub const CODE_LEN: usize = 5; | ||
pub const CLIENT_TIMEOUT: Duration = Duration::from_secs(120); | ||
pub const EXPIRE_SESSION_CODE_SECS: Duration = Duration::from_secs(2000); | ||
|
@@ -26,11 +26,11 @@ pub const AUTHORIZATION: &str = "Authorization"; | |
pub const BEARER: &str = "Bearer"; | ||
pub const APP_DOMAIN: &str = "rustfulapi.com"; | ||
pub const APP_EMAIL_ADDR: &str = "[email protected]"; | ||
pub static IMAGES_PATH: Lazy<PathBuf> = Lazy::new(|| util::dir::root_dir("static/images").unwrap()); | ||
pub static IMAGES_PATH: Lazy<PathBuf> = Lazy::new(|| get_static_dir().unwrap().join("images")); | ||
pub static APP_IMAGE: Lazy<PathBuf> = | ||
Lazy::new(|| util::dir::root_dir("static/images/logo.jpg").unwrap()); | ||
Lazy::new(|| get_static_dir().unwrap().join("images/logo.jpg")); | ||
pub static CONFIG: Lazy<crate::configure::AppConfig> = | ||
Lazy::new(|| crate::configure::AppConfig::read().unwrap()); | ||
Lazy::new(|| crate::configure::AppConfig::read(get_env_source(ENV_PREFIX)).unwrap()); | ||
pub static HTTP: Lazy<reqwest::Client> = | ||
Lazy::new(|| HttpClient::build_from_config(&CONFIG).unwrap()); | ||
pub static REDIS: Lazy<RedisClient> = | ||
|
@@ -57,8 +57,9 @@ pub static ACCESS_TOKEN_DECODE_KEY: Lazy<DecodingKey> = Lazy::new(|| { | |
}); | ||
pub static API_DOC: Lazy<utoipa::openapi::OpenApi> = Lazy::new(ApiDoc::openapi); | ||
pub static TEMPLATE_ENGIN: Lazy<TemplateEngine> = Lazy::new(|| { | ||
let path = util::dir::root_dir("static/template/**/*") | ||
let path = get_static_dir() | ||
.unwrap() | ||
.join("template/**/*") | ||
.into_os_string() | ||
.into_string() | ||
.unwrap(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
use std::path::Path; | ||
|
||
pub fn root_dir<P: AsRef<Path> + ?Sized>(path: &P) -> std::io::Result<std::path::PathBuf> { | ||
Ok( | ||
project_root::get_project_root() | ||
.or_else(|_| std::env::current_dir())? | ||
.join(path), | ||
) | ||
use std::path::PathBuf; | ||
|
||
pub fn get_project_root() -> std::io::Result<PathBuf> { | ||
if let Some(root) = get_cargo_project_root()? { | ||
Ok(root) | ||
} else { | ||
Ok(std::env::current_dir()?) | ||
} | ||
} | ||
|
||
pub fn get_cargo_project_root() -> std::io::Result<Option<PathBuf>> { | ||
let current_path = std::env::current_dir()?; | ||
|
||
for ancestor in current_path.ancestors() { | ||
for dir in std::fs::read_dir(ancestor)? { | ||
let dir = dir?; | ||
if dir.file_name() == *"Cargo.lock" { | ||
return Ok(Some(ancestor.to_path_buf())); | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::get_cargo_project_root; | ||
|
||
#[test] | ||
fn test_get_cargo_project_root() { | ||
let root = get_cargo_project_root().unwrap().unwrap(); | ||
assert_eq!(root.file_name().unwrap().to_str().unwrap(), "rustfulapi"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters