Skip to content

Commit

Permalink
add validate function settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Mar 19, 2024
1 parent fc404d8 commit e31b4cb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $ curl -X DELETE http://127.0.0.1:8080/{code}/{file_name}
```toml
# api/settings/base.toml
# Maximum upload size in bytes
max_upload_bytes_size = 1000_000_000 # 1GB
max_upload_bytes_size = 1_000_000_000 # 1GB

# Default code length in the url path
default_code_length = 3
Expand Down
2 changes: 1 addition & 1 deletion api/settings/base.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Maximum upload size in bytes
max_upload_bytes_size = 1000_000_000 # 1GB
max_upload_bytes_size = 1_000_000_000 # 1GB
# Default code length in the url path
default_code_length = 3
# Default expiration time in seconds
Expand Down
2 changes: 2 additions & 0 deletions api/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async fn main() -> ApiResult {
let args = api::configure::args::Args::parse();
// Read API configuration
let config = api::configure::ApiConfig::read(args.settings, get_env_source(ENV_PREFIX))?;
// Validate settings
config.validate()?;
// Force initialization of subscriber
Lazy::force(&INIT_SUBSCRIBER);
// Create base directory if it doesn't exist
Expand Down
30 changes: 27 additions & 3 deletions api/src/configure/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::constant::ENV_PREFIX;
use crate::{
constant::ENV_PREFIX,
error::{result::ApiResult, ApiError},
};
use config::Environment;
use once_cell::sync::Lazy;
use sdk::util::dir::get_cargo_project_root;
Expand Down Expand Up @@ -109,6 +112,26 @@ impl ApiConfig {
.build()?
.try_deserialize()
}

pub fn validate(&self) -> ApiResult {
if self.default_code_length < 2 {
return Err(ApiError::ConfigError(config::ConfigError::Message(
"The default_code_length should be greater than 2.".to_string(),
)));
}
if self.server.domain_name.starts_with("http") {
return Err(ApiError::ConfigError(config::ConfigError::Message(
"The domain_name should not start with 'http://' or 'https://'.".to_string(),
)));
}
if self.server.port > 49151 || self.server.port < 1024 {
return Err(ApiError::ConfigError(config::ConfigError::Message(
"The port number is invalid.".to_string(),
)));
}
// TODO
Ok(())
}
}

fn get_basic_settings_path(file_src: Option<PathBuf>) -> std::io::Result<PathBuf> {
Expand All @@ -133,7 +156,8 @@ mod tests {
use super::*;

#[test]
fn test_read_config() {
ApiConfig::read(None, get_env_source("TEST_PF")).unwrap();
fn test_read_and_validate_config() {
let config = ApiConfig::read(None, get_env_source("TEST_PF")).unwrap();
config.validate().unwrap();
}
}
8 changes: 7 additions & 1 deletion api/src/handler/file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use axum::{
body::Body,
extract::{Multipart, Path, Query, State},
Expand Down Expand Up @@ -53,7 +54,12 @@ pub async fn download(
) -> ApiResult<Response<ServeFileSystemResponseBody>> {
let secret = crate::util::http::parse_basic_auth(req.headers())?;
let file = service::file::fetch(&state, &code, &file_name, secret).await?;
Ok(file.oneshot(req).await.unwrap())
Ok(
file
.oneshot(req)
.await
.map_err(|e| anyhow!("Download file failed, Error: {e}"))?,
)
}

pub async fn info(
Expand Down

0 comments on commit e31b4cb

Please sign in to comment.