Skip to content

Commit

Permalink
Add support for the config
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Sep 22, 2023
1 parent c667ab7 commit 32bc454
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 54 deletions.
11 changes: 8 additions & 3 deletions zino-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -162,7 +162,7 @@ optional = true
features = ["debug", "loader"]

[dependencies.opendal]
version = "0.39.0"
version = "0.40.0"
optional = true
features = ["layers-all"]

Expand Down Expand Up @@ -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"
Expand Down
17 changes: 4 additions & 13 deletions zino-core/src/accessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion zino-core/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
Expand Down
2 changes: 1 addition & 1 deletion zino-core/src/application/tracing_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(super) fn init<APP: Application + ?Sized>() {
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}");
});
}
Expand Down
20 changes: 20 additions & 0 deletions zino-core/src/auth/user_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,24 @@ impl<U, T> UserSession<U, String, T> {
}
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
}
}
2 changes: 1 addition & 1 deletion zino-core/src/i18n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static LOCALIZATION: LazyLock<Vec<(LanguageIdentifier, Translation)>> = 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 =
Expand Down
2 changes: 1 addition & 1 deletion zino-core/src/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static OPENAPI_PATHS: LazyLock<BTreeMap<String, PathItem>> = 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::<Table>()
Expand Down
2 changes: 1 addition & 1 deletion zino-core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<T> State<T> {
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()
}
Expand Down
8 changes: 6 additions & 2 deletions zino/src/application/actix_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
66 changes: 35 additions & 31 deletions zino/src/application/axum_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 32bc454

Please sign in to comment.