From 391eb5b6f9a934ef8f4311d0507beab318ced78d Mon Sep 17 00:00:00 2001 From: houseme Date: Tue, 18 Feb 2025 18:37:17 +0800 Subject: [PATCH] Refactor main.rs for clarity and modularity in rustfs-gui (#225) * Improve init_logger function and format code with cargo fmt - Enhance `init_logger` function for better logging configuration. - Apply `cargo fmt` to format the codebase. * Refactor main.rs for clarity and modularity in rustfs-gui - Move `init_logger` function to `utils/logger.rs` for better separation of concerns. - Create a dedicated `router` module in `router/router.rs`. - Update `main.rs` to use the new `logger` and `router` modules. - Apply `cargo fmt` to format the codebase. --- cli/rustfs-gui/Cargo.toml | 6 +-- cli/rustfs-gui/assets/rustfs-logo.svg | 31 ++++++----- cli/rustfs-gui/assets/styling/navbar.css | 17 +++--- cli/rustfs-gui/src/components/home.rs | 2 +- cli/rustfs-gui/src/components/navbar.rs | 3 +- cli/rustfs-gui/src/main.rs | 68 ++---------------------- cli/rustfs-gui/src/router/mod.rs | 3 ++ cli/rustfs-gui/src/router/router.rs | 14 +++++ cli/rustfs-gui/src/utils/config.rs | 24 +++------ cli/rustfs-gui/src/utils/logger.rs | 48 +++++++++++++++++ cli/rustfs-gui/src/utils/mod.rs | 2 + cli/rustfs-gui/src/views/app.rs | 24 +++++++++ cli/rustfs-gui/src/views/mod.rs | 2 + cli/rustfs-gui/tailwind.config.js | 12 ++--- 14 files changed, 141 insertions(+), 115 deletions(-) create mode 100644 cli/rustfs-gui/src/router/mod.rs create mode 100644 cli/rustfs-gui/src/router/router.rs create mode 100644 cli/rustfs-gui/src/utils/logger.rs create mode 100644 cli/rustfs-gui/src/views/app.rs diff --git a/cli/rustfs-gui/Cargo.toml b/cli/rustfs-gui/Cargo.toml index 1d2675b0..87bf7a87 100644 --- a/cli/rustfs-gui/Cargo.toml +++ b/cli/rustfs-gui/Cargo.toml @@ -42,9 +42,9 @@ inherits = "dev" inherits = "dev" [profile.release] -opt-level = 3 # 优化级别(0-3) -lto = true # 启用链接时优化 -codegen-units = 1 # 减少代码生成单元以提高优化效果 +opt-level = 3 # Optimization Level (0-3) +lto = true # Optimize when linking +codegen-units = 1 # Reduce code generation units to improve optimization [lints] workspace = true diff --git a/cli/rustfs-gui/assets/rustfs-logo.svg b/cli/rustfs-gui/assets/rustfs-logo.svg index 93ca3c55..1a7d121f 100644 --- a/cli/rustfs-gui/assets/rustfs-logo.svg +++ b/cli/rustfs-gui/assets/rustfs-logo.svg @@ -1,15 +1,20 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/cli/rustfs-gui/assets/styling/navbar.css b/cli/rustfs-gui/assets/styling/navbar.css index 410b30d1..e9722278 100644 --- a/cli/rustfs-gui/assets/styling/navbar.css +++ b/cli/rustfs-gui/assets/styling/navbar.css @@ -1,16 +1,17 @@ #navbar { - display: flex; - flex-direction: row; + display: flex; + flex-direction: row; } #navbar a { - color: #ffffff; - margin-right: 20px; - text-decoration: none; - transition: color 0.2s ease; + color: #ffffff; + margin-right: 20px; + text-decoration: none; + transition: color 0.2s ease; } #navbar a:hover { - cursor: pointer; - color: #ffffff; // #91a4d2; + cursor: pointer; + color: #ffffff; +/ / #91a4d2; } \ No newline at end of file diff --git a/cli/rustfs-gui/src/components/home.rs b/cli/rustfs-gui/src/components/home.rs index 022823f4..141f48d6 100644 --- a/cli/rustfs-gui/src/components/home.rs +++ b/cli/rustfs-gui/src/components/home.rs @@ -1,6 +1,6 @@ use crate::components::navbar::LoadingSpinner; +use crate::router::Route; use crate::utils::{RustFSConfig, ServiceManager}; -use crate::Route; use chrono::Datelike; use dioxus::logger::tracing::debug; use dioxus::prelude::*; diff --git a/cli/rustfs-gui/src/components/navbar.rs b/cli/rustfs-gui/src/components/navbar.rs index a9445f35..6a23541b 100644 --- a/cli/rustfs-gui/src/components/navbar.rs +++ b/cli/rustfs-gui/src/components/navbar.rs @@ -1,4 +1,4 @@ -use crate::Route; +use crate::router::Route; use dioxus::logger::tracing::debug; use dioxus::prelude::*; @@ -11,6 +11,7 @@ pub fn Navbar() -> Element { div { id: "navbar", class: "hidden", style: "display: none;", Link { to: Route::HomeViews {}, "Home" } + Link { to: Route::SettingViews {}, "Setting" } } Outlet:: {} diff --git a/cli/rustfs-gui/src/main.rs b/cli/rustfs-gui/src/main.rs index a96f49f3..eba78337 100644 --- a/cli/rustfs-gui/src/main.rs +++ b/cli/rustfs-gui/src/main.rs @@ -1,71 +1,9 @@ -use components::Navbar; -use dioxus::logger::tracing::debug; -use dioxus::prelude::*; -use tracing_appender::rolling::{RollingFileAppender, Rotation}; -use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; -use views::{HomeViews, SettingViews}; - mod components; +mod router; mod utils; mod views; fn main() { - init_logger(); - dioxus::launch(App); -} - -#[derive(Debug, Clone, Routable, PartialEq)] -#[rustfmt::skip] -enum Route { - #[layout(Navbar)] - #[route("/")] - HomeViews {}, - #[route("/settings")] - SettingViews {}, -} - -const FAVICON: Asset = asset!("/assets/favicon.ico"); -const TAILWIND_CSS: Asset = asset!("/assets/tailwind.css"); - -#[component] -fn App() -> Element { - // Build cool things ✌️ - use document::{Link, Title}; - debug!("App rendered"); - rsx! { - // Global app resources - Link { rel: "icon", href: FAVICON } - Link { rel: "stylesheet", href: TAILWIND_CSS } - // Script { src: "https://cdn.tailwindcss.com" } - Title { "RustFS" } - Router:: {} - } -} - -fn init_logger() { - // configuring rolling logs rolling by day - let home_dir = dirs::home_dir().expect("无法获取用户目录"); - let rustfs_dir = home_dir.join("rustfs"); - let logs_dir = rustfs_dir.join("logs"); - let file_appender = RollingFileAppender::builder() - .rotation(Rotation::DAILY) // rotate log files once every hour - .filename_prefix("rustfs-cli") // log file names will be prefixed with `myapp.` - .filename_suffix("log") // log file names will be suffixed with `.log` - .build(logs_dir) // try to build an appender that stores log files in `/ var/ log` - .expect("initializing rolling file appender failed"); - // non-blocking writer for improved performance - let (non_blocking_file, _guard) = tracing_appender::non_blocking(file_appender); - - // console output layer - let console_layer = fmt::layer().with_writer(std::io::stdout).with_ansi(true); // enable colors in the console - - // file output layer - let file_layer = fmt::layer().with_writer(non_blocking_file).with_ansi(false); // disable colors in the file - - // Combine all tiers and initialize global subscribers - tracing_subscriber::registry() - .with(console_layer) - .with(file_layer) - .with(tracing_subscriber::EnvFilter::new("info")) // filter the log level by environment variables - .init(); + let _worker_guard = utils::init_logger(); + dioxus::launch(views::App); } diff --git a/cli/rustfs-gui/src/router/mod.rs b/cli/rustfs-gui/src/router/mod.rs new file mode 100644 index 00000000..afff5bc6 --- /dev/null +++ b/cli/rustfs-gui/src/router/mod.rs @@ -0,0 +1,3 @@ +mod router; + +pub use router::Route; diff --git a/cli/rustfs-gui/src/router/router.rs b/cli/rustfs-gui/src/router/router.rs new file mode 100644 index 00000000..4c17d60c --- /dev/null +++ b/cli/rustfs-gui/src/router/router.rs @@ -0,0 +1,14 @@ +use crate::components::Navbar; +use crate::views::{HomeViews, SettingViews}; +use dioxus::prelude::*; + +/// The router for the application +#[derive(Debug, Clone, Routable, PartialEq)] +#[rustfmt::skip] +pub enum Route { + #[layout(Navbar)] + #[route("/")] + HomeViews {}, + #[route("/settings")] + SettingViews {}, +} diff --git a/cli/rustfs-gui/src/utils/config.rs b/cli/rustfs-gui/src/utils/config.rs index d5baf952..0e41a085 100644 --- a/cli/rustfs-gui/src/utils/config.rs +++ b/cli/rustfs-gui/src/utils/config.rs @@ -122,36 +122,24 @@ impl RustFSConfig { if let Ok(stored_json) = entry.get_password() { if let Ok(stored_config) = serde_json::from_str::(&stored_json) { // update fields that are not empty and non default - if !stored_config.address.is_empty() - && stored_config.address != Self::DEFAULT_ADDRESS_VALUE - { + if !stored_config.address.is_empty() && stored_config.address != Self::DEFAULT_ADDRESS_VALUE { config.address = stored_config.address; let (host, port) = Self::extract_host_port(config.address.as_str()) - .ok_or_else(|| { - format!("无法从地址 '{}' 中提取主机和端口", config.address) - })?; + .ok_or_else(|| format!("无法从地址 '{}' 中提取主机和端口", config.address))?; config.host = host.to_string(); config.port = port.to_string(); } - if !stored_config.access_key.is_empty() - && stored_config.access_key != Self::DEFAULT_ACCESS_KEY_VALUE - { + if !stored_config.access_key.is_empty() && stored_config.access_key != Self::DEFAULT_ACCESS_KEY_VALUE { config.access_key = stored_config.access_key; } - if !stored_config.secret_key.is_empty() - && stored_config.secret_key != Self::DEFAULT_SECRET_KEY_VALUE - { + if !stored_config.secret_key.is_empty() && stored_config.secret_key != Self::DEFAULT_SECRET_KEY_VALUE { config.secret_key = stored_config.secret_key; } - if !stored_config.domain_name.is_empty() - && stored_config.domain_name != Self::DEFAULT_DOMAIN_NAME_VALUE - { + if !stored_config.domain_name.is_empty() && stored_config.domain_name != Self::DEFAULT_DOMAIN_NAME_VALUE { config.domain_name = stored_config.domain_name; } // The stored volume_name is updated only if it is not empty and different from the default - if !stored_config.volume_name.is_empty() - && stored_config.volume_name != Self::default_volume_name() - { + if !stored_config.volume_name.is_empty() && stored_config.volume_name != Self::default_volume_name() { config.volume_name = stored_config.volume_name; } if !stored_config.console_address.is_empty() diff --git a/cli/rustfs-gui/src/utils/logger.rs b/cli/rustfs-gui/src/utils/logger.rs new file mode 100644 index 00000000..528d05a6 --- /dev/null +++ b/cli/rustfs-gui/src/utils/logger.rs @@ -0,0 +1,48 @@ +use dioxus::logger::tracing::debug; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_appender::rolling::{RollingFileAppender, Rotation}; +use tracing_subscriber::fmt; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; + +/// Initialize the logger with a rolling file appender +/// that rotates log files daily +pub fn init_logger() -> WorkerGuard { + // configuring rolling logs rolling by day + let home_dir = dirs::home_dir().expect("无法获取用户目录"); + let rustfs_dir = home_dir.join("rustfs"); + let logs_dir = rustfs_dir.join("logs"); + let file_appender = RollingFileAppender::builder() + .rotation(Rotation::DAILY) // rotate log files once every hour + .filename_prefix("rustfs-cli") // log file names will be prefixed with `myapp.` + .filename_suffix("log") // log file names will be suffixed with `.log` + .build(logs_dir) // try to build an appender that stores log files in `/ var/ log` + .expect("initializing rolling file appender failed"); + // non-blocking writer for improved performance + let (non_blocking_file, worker_guard) = tracing_appender::non_blocking(file_appender); + + // console output layer + let console_layer = fmt::layer() + .with_writer(std::io::stdout) + .with_ansi(true) + .with_line_number(true); // enable colors in the console + + // file output layer + let file_layer = fmt::layer() + .with_writer(non_blocking_file) + .with_ansi(false) + .with_thread_names(true) + .with_target(true) + .with_thread_ids(true) + .with_level(true) + .with_line_number(true); // disable colors in the file + + // Combine all tiers and initialize global subscribers + tracing_subscriber::registry() + .with(console_layer) + .with(file_layer) + .with(tracing_subscriber::EnvFilter::new("info")) // filter the log level by environment variables + .init(); + debug!("Logger initialized"); + worker_guard +} diff --git a/cli/rustfs-gui/src/utils/mod.rs b/cli/rustfs-gui/src/utils/mod.rs index 8fee712d..fe795568 100644 --- a/cli/rustfs-gui/src/utils/mod.rs +++ b/cli/rustfs-gui/src/utils/mod.rs @@ -1,5 +1,7 @@ mod config; mod helper; +mod logger; pub use config::RustFSConfig; pub use helper::ServiceManager; +pub use logger::init_logger; diff --git a/cli/rustfs-gui/src/views/app.rs b/cli/rustfs-gui/src/views/app.rs new file mode 100644 index 00000000..1523eb0b --- /dev/null +++ b/cli/rustfs-gui/src/views/app.rs @@ -0,0 +1,24 @@ +use crate::router::Route; +use dioxus::logger::tracing::info; +use dioxus::prelude::*; + +const FAVICON: Asset = asset!("/assets/favicon.ico"); +const TAILWIND_CSS: Asset = asset!("/assets/tailwind.css"); + +/// The main application component +/// This is the root component of the application +/// It contains the global resources and the router +/// for the application +#[component] +pub fn App() -> Element { + // Build cool things ✌️ + use document::{Link, Title}; + info!("App rendered"); + rsx! { + // Global app resources + Link { rel: "icon", href: FAVICON } + Link { rel: "stylesheet", href: TAILWIND_CSS } + Title { "RustFS" } + Router:: {} + } +} diff --git a/cli/rustfs-gui/src/views/mod.rs b/cli/rustfs-gui/src/views/mod.rs index 7f92ad95..5da74ee4 100644 --- a/cli/rustfs-gui/src/views/mod.rs +++ b/cli/rustfs-gui/src/views/mod.rs @@ -1,5 +1,7 @@ +mod app; mod home; mod setting; +pub use app::App; pub use home::HomeViews; pub use setting::SettingViews; diff --git a/cli/rustfs-gui/tailwind.config.js b/cli/rustfs-gui/tailwind.config.js index b93ab1b9..222d1be4 100644 --- a/cli/rustfs-gui/tailwind.config.js +++ b/cli/rustfs-gui/tailwind.config.js @@ -1,8 +1,8 @@ module.exports = { - mode: "all", - content: ["./src/**/*.{rs,html,css}", "./dist/**/*.html"], - theme: { - extend: {}, - }, - plugins: [], + mode: "all", + content: ["./src/**/*.{rs,html,css}", "./dist/**/*.html"], + theme: { + extend: {}, + }, + plugins: [], }; \ No newline at end of file