From 32bc454094b602f03a9b93365e1bc794aa70fddc Mon Sep 17 00:00:00 2001 From: photino Date: Fri, 22 Sep 2023 18:45:45 +0800 Subject: [PATCH] Add support for the config --- zino-core/Cargo.toml | 11 +++- zino-core/src/accessor/mod.rs | 17 ++--- zino-core/src/application/mod.rs | 2 +- .../src/application/tracing_subscriber.rs | 2 +- zino-core/src/auth/user_session.rs | 20 ++++++ zino-core/src/i18n/mod.rs | 2 +- zino-core/src/openapi/mod.rs | 2 +- zino-core/src/state/mod.rs | 2 +- zino/src/application/actix_cluster.rs | 8 ++- zino/src/application/axum_cluster.rs | 66 ++++++++++--------- 10 files changed, 78 insertions(+), 54 deletions(-) diff --git a/zino-core/Cargo.toml b/zino-core/Cargo.toml index 0cbdd5d9..fccd18f7 100644 --- a/zino-core/Cargo.toml +++ b/zino-core/Cargo.toml @@ -114,7 +114,7 @@ hmac = "0.12.1" http = "0.2.9" http-body = "0.4.5" intl-memoizer = "0.5.1" -jwt-simple = "0.11.6" +jwt-simple = "0.11.7" metrics = "0.21.0" metrics-exporter-prometheus = "0.12.1" mime = "0.3.17" @@ -162,7 +162,7 @@ optional = true features = ["debug", "loader"] [dependencies.opendal] -version = "0.39.0" +version = "0.40.0" optional = true features = ["layers-all"] @@ -217,7 +217,12 @@ features = ["macros"] [dependencies.utoipa] version = "3.5.0" -features = ["non_strict_integers", "uuid"] +features = [ + "non_strict_integers", + "preserve_order", + "preserve_path_order", + "uuid", +] [dependencies.uuid] version = "1.4.1" diff --git a/zino-core/src/accessor/mod.rs b/zino-core/src/accessor/mod.rs index 8a42c477..23ef4a8b 100644 --- a/zino-core/src/accessor/mod.rs +++ b/zino-core/src/accessor/mod.rs @@ -5,7 +5,7 @@ //! | Scheme | Description | Feature flag | //! |---------------|------------------------------------------|-----------------------| //! | `azblob` | Azure Storage Blob services. | `accessor` | -//! | `azdfs` | Azure Data Lake Storage Gen2 services. | `accessor` | +//! | `azdls` | Azure Data Lake Storage Gen2 services. | `accessor` | //! | `cacache` | Cacache services. | `accessor-cacache` | //! | `cos` | Tencent-Cloud COS services. | `accessor` | //! | `dashmap` | Dashmap backend. | `accessor-dashmap` | @@ -41,7 +41,7 @@ use crate::{extension::TomlTableExt, state::State}; use opendal::{ layers::{MetricsLayer, RetryLayer, TracingLayer}, services::{ - Azblob, Azdfs, Cos, Fs, Gcs, Ghac, Http, Ipmfs, Memory, Obs, Oss, Webdav, Webhdfs, S3, + Azblob, Azdls, Cos, Fs, Gcs, Ghac, Http, Ipmfs, Memory, Obs, Oss, Webdav, Webhdfs, S3, }, Error, ErrorKind::Unsupported, @@ -126,8 +126,8 @@ impl GlobalAccessor { } Ok(Operator::new(builder)?.finish()) } - "azdfs" => { - let mut builder = Azdfs::default(); + "azdls" => { + let mut builder = Azdls::default(); if let Some(root) = config.get_str("root") { builder.root(root); } @@ -170,9 +170,6 @@ impl GlobalAccessor { if let Some(secret_key) = config.get_str("secret-key") { builder.secret_key(secret_key); } - if let Some(write_min_size) = config.get_usize("write-min-size") { - builder.write_min_size(write_min_size); - } Ok(Operator::new(builder)?.finish()) } #[cfg(feature = "accessor-dashmap")] @@ -385,9 +382,6 @@ impl GlobalAccessor { if let Some(secret_access_key) = config.get_str("secret_access_key") { builder.secret_access_key(secret_access_key); } - if let Some(write_min_size) = config.get_usize("write-min-size") { - builder.write_min_size(write_min_size); - } Ok(Operator::new(builder)?.finish()) } #[cfg(feature = "accessor-onedrive")] @@ -512,9 +506,6 @@ impl GlobalAccessor { if let Some(external_id) = config.get_str("external-id") { builder.external_id(external_id); } - if let Some(write_min_size) = config.get_usize("write-min-size") { - builder.write_min_size(write_min_size); - } if let Some(batch_max_operations) = config.get_usize("batch-max-operations") { builder.batch_max_operations(batch_max_operations); } diff --git a/zino-core/src/application/mod.rs b/zino-core/src/application/mod.rs index 4ac3514d..88ca226a 100644 --- a/zino-core/src/application/mod.rs +++ b/zino-core/src/application/mod.rs @@ -55,7 +55,7 @@ pub trait Application { project_dir.join(dir) }; if !path.exists() && let Err(err) = fs::create_dir_all(&path) { - let path = path.to_string_lossy(); + let path = path.display(); tracing::error!("fail to create the directory {path}: {err}"); } } diff --git a/zino-core/src/application/tracing_subscriber.rs b/zino-core/src/application/tracing_subscriber.rs index 4c42a039..2f4e11f9 100644 --- a/zino-core/src/application/tracing_subscriber.rs +++ b/zino-core/src/application/tracing_subscriber.rs @@ -57,7 +57,7 @@ pub(super) fn init() { let log_dir = project_dir.join("logs"); if !log_dir.exists() { fs::create_dir(log_dir.as_path()).unwrap_or_else(|err| { - let log_dir = log_dir.to_string_lossy(); + let log_dir = log_dir.display(); panic!("fail to create the log directory `{log_dir}`: {err}"); }); } diff --git a/zino-core/src/auth/user_session.rs b/zino-core/src/auth/user_session.rs index 38422843..1363398b 100644 --- a/zino-core/src/auth/user_session.rs +++ b/zino-core/src/auth/user_session.rs @@ -223,4 +223,24 @@ impl UserSession { } false } + + /// Returns `true` if the user has any of the specific `roles`. + pub fn has_any_roles(&self, roles: &[&str]) -> bool { + for role in roles { + if self.has_role(role) { + return true; + } + } + false + } + + /// Returns `true` if the user has all of the specific `roles`. + pub fn has_all_roles(&self, roles: &[&str]) -> bool { + for role in roles { + if !self.has_role(role) { + return false; + } + } + true + } } diff --git a/zino-core/src/i18n/mod.rs b/zino-core/src/i18n/mod.rs index 48c2cb64..9e35d70e 100644 --- a/zino-core/src/i18n/mod.rs +++ b/zino-core/src/i18n/mod.rs @@ -71,7 +71,7 @@ static LOCALIZATION: LazyLock> = LazyLock for file in files { let locale_file = file.path(); let ftl_string = fs::read_to_string(&locale_file).unwrap_or_else(|err| { - let locale_file = locale_file.to_string_lossy(); + let locale_file = locale_file.display(); panic!("fail to read `{locale_file}`: {err}"); }); let resource = diff --git a/zino-core/src/openapi/mod.rs b/zino-core/src/openapi/mod.rs index d2fc83e9..4839f1b8 100644 --- a/zino-core/src/openapi/mod.rs +++ b/zino-core/src/openapi/mod.rs @@ -242,7 +242,7 @@ static OPENAPI_PATHS: LazyLock> = LazyLock::new(|| { let openapi_file = file.path(); let openapi_config = fs::read_to_string(&openapi_file) .unwrap_or_else(|err| { - let openapi_file = openapi_file.to_string_lossy(); + let openapi_file = openapi_file.display(); panic!("fail to read the OpenAPI file `{openapi_file}`: {err}"); }) .parse::() diff --git a/zino-core/src/state/mod.rs b/zino-core/src/state/mod.rs index ed76c10b..bdf4bf68 100644 --- a/zino-core/src/state/mod.rs +++ b/zino-core/src/state/mod.rs @@ -45,7 +45,7 @@ impl State { value.parse().unwrap_or_default() } Err(err) => { - let config_file = config_file.to_string_lossy(); + let config_file = config_file.display(); tracing::error!("fail to read the config file `{config_file}`: {err}"); Table::new() } diff --git a/zino/src/application/actix_cluster.rs b/zino/src/application/actix_cluster.rs index 457188a8..3ed42fff 100644 --- a/zino/src/application/actix_cluster.rs +++ b/zino/src/application/actix_cluster.rs @@ -77,13 +77,17 @@ impl Application for ActixCluster { // Server config let project_dir = Self::project_dir(); let default_public_dir = project_dir.join("public"); + let mut public_route_name = "/public"; let mut public_dir = PathBuf::new(); let mut body_limit = 100 * 1024 * 1024; // 100MB if let Some(server_config) = Self::config().get_table("server") { if let Some(limit) = server_config.get_usize("body-limit") { body_limit = limit; } - if let Some(dir) = server_config.get_str("public-dir") { + if let Some(dir) = server_config.get_str("page-dir") { + public_route_name = "/page"; + public_dir.push(dir); + } else if let Some(dir) = server_config.get_str("public-dir") { public_dir.push(dir); } else { public_dir = default_public_dir; @@ -95,7 +99,7 @@ impl Application for ActixCluster { HttpServer::new(move || { let index_file_handler = web::get() .to(move || async { NamedFile::open_async("./public/index.html").await }); - let static_files = Files::new("/public", public_dir.clone()) + let static_files = Files::new(public_route_name, public_dir.clone()) .show_files_listing() .prefer_utf8(true) .index_file("index.html") diff --git a/zino/src/application/axum_cluster.rs b/zino/src/application/axum_cluster.rs index 7296bc21..beb00ca6 100644 --- a/zino/src/application/axum_cluster.rs +++ b/zino/src/application/axum_cluster.rs @@ -74,35 +74,6 @@ impl Application for AxumCluster { } }); - // Server config - let project_dir = Self::project_dir(); - let default_public_dir = project_dir.join("public"); - let mut public_dir = PathBuf::new(); - let mut body_limit = 100 * 1024 * 1024; // 100MB - let mut request_timeout = Duration::from_secs(10); // 10 seconds - if let Some(server_config) = Self::config().get_table("server") { - if let Some(limit) = server_config.get_usize("body-limit") { - body_limit = limit; - } - if let Some(timeout) = server_config.get_duration("request-timeout") { - request_timeout = timeout; - } - if let Some(dir) = server_config.get_str("public-dir") { - public_dir.push(dir); - } else { - public_dir = default_public_dir; - } - } else { - public_dir = default_public_dir; - } - let index_file = public_dir.join("index.html"); - let not_found_file = public_dir.join("404.html"); - let serve_file = ServeFile::new(index_file); - let serve_dir = ServeDir::new(public_dir) - .precompressed_gzip() - .precompressed_br() - .not_found_service(ServeFile::new(not_found_file)); - runtime.block_on(async { let default_routes = self.default_routes; let named_routes = self.named_routes; @@ -123,9 +94,42 @@ impl Application for AxumCluster { "listen on {addr}", ); + // Server config + let project_dir = Self::project_dir(); + let default_public_dir = project_dir.join("public"); + let mut public_route_name = "/public"; + let mut public_dir = PathBuf::new(); + let mut body_limit = 100 * 1024 * 1024; // 100MB + let mut request_timeout = Duration::from_secs(10); // 10 seconds + if let Some(server_config) = Self::config().get_table("server") { + if let Some(limit) = server_config.get_usize("body-limit") { + body_limit = limit; + } + if let Some(timeout) = server_config.get_duration("request-timeout") { + request_timeout = timeout; + } + if let Some(dir) = server_config.get_str("page-dir") { + public_route_name = "/page"; + public_dir.push(dir); + } else if let Some(dir) = server_config.get_str("public-dir") { + public_dir.push(dir); + } else { + public_dir = default_public_dir; + } + } else { + public_dir = default_public_dir; + } + + let index_file = public_dir.join("index.html"); + let not_found_file = public_dir.join("404.html"); + let serve_file = ServeFile::new(index_file); + let serve_dir = ServeDir::new(public_dir) + .precompressed_gzip() + .precompressed_br() + .not_found_service(ServeFile::new(not_found_file)); let mut app = Router::new() - .route_service("/", serve_file.clone()) - .nest_service("/public", serve_dir.clone()) + .route_service("/", serve_file) + .nest_service(public_route_name, serve_dir) .route("/sse", routing::get(endpoint::sse_handler)) .route("/websocket", routing::get(endpoint::websocket_handler)); for route in &default_routes {