pub struct DashboardConfig {
pub bind_address: String,
pub port: u16,
pub database_url: String,
pub pool_size: u32,
pub static_dir: PathBuf,
pub auth: AuthConfig,
pub websocket: WebSocketConfig,
pub enable_cors: bool,
pub allowed_origins: Vec<String>,
}Expand description
Main configuration for the web dashboard.
This struct contains all configuration options for the Hammerwork web dashboard, including server settings, database connection, authentication, and WebSocket options.
§Examples
use hammerwork_web::config::DashboardConfig;
use std::path::PathBuf;
// Create with defaults
let config = DashboardConfig::default();
assert_eq!(config.bind_address, "127.0.0.1");
assert_eq!(config.port, 8080);
// Use builder pattern
let config = DashboardConfig::new()
.with_bind_address("0.0.0.0", 9090)
.with_database_url("postgresql://localhost/hammerwork")
.with_cors(true);
assert_eq!(config.bind_addr(), "0.0.0.0:9090");
assert!(config.enable_cors);Debug shows database_url with its password replaced by ***.
Fields§
§bind_address: StringServer bind address
port: u16Server port
database_url: StringDatabase connection URL
pool_size: u32Database connection pool size
static_dir: PathBufDirectory containing static assets (HTML, CSS, JS)
auth: AuthConfigAuthentication configuration
websocket: WebSocketConfigWebSocket configuration
enable_cors: boolEnable CORS for cross-origin requests from allowed_origins.
CORS is only ever granted to the listed origins, never to any origin, so enabling it requires at least one entry there.
allowed_origins: Vec<String>Origins other than the dashboard’s own (scheme://host[:port]) that may send
state-changing requests and WebSocket connections, and, with
enable_cors, read API responses. Browsers on any other origin
are refused (see crate::security).
Implementations§
Source§impl DashboardConfig
impl DashboardConfig
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new configuration with defaults.
§Examples
use hammerwork_web::config::DashboardConfig;
let config = DashboardConfig::new();
assert_eq!(config.bind_address, "127.0.0.1");
assert_eq!(config.port, 8080);
assert_eq!(config.database_url, "postgresql://localhost/hammerwork");Sourcepub fn with_bind_address(self, address: &str, port: u16) -> Self
pub fn with_bind_address(self, address: &str, port: u16) -> Self
Set the server bind address and port.
§Examples
use hammerwork_web::config::DashboardConfig;
let config = DashboardConfig::new()
.with_bind_address("0.0.0.0", 9090);
assert_eq!(config.bind_address, "0.0.0.0");
assert_eq!(config.port, 9090);
assert_eq!(config.bind_addr(), "0.0.0.0:9090");Sourcepub fn with_database_url(self, url: &str) -> Self
pub fn with_database_url(self, url: &str) -> Self
Set the database URL.
Supports both PostgreSQL and MySQL database URLs.
§Examples
use hammerwork_web::config::DashboardConfig;
// PostgreSQL
let pg_config = DashboardConfig::new()
.with_database_url("postgresql://user:pass@localhost/hammerwork");
assert_eq!(pg_config.database_url, "postgresql://user:pass@localhost/hammerwork");
// MySQL
let mysql_config = DashboardConfig::new()
.with_database_url("mysql://root:password@localhost/hammerwork");
assert_eq!(mysql_config.database_url, "mysql://root:password@localhost/hammerwork");Sourcepub fn with_static_dir(self, dir: PathBuf) -> Self
pub fn with_static_dir(self, dir: PathBuf) -> Self
Set the static assets directory.
§Examples
use hammerwork_web::config::DashboardConfig;
use std::path::PathBuf;
let config = DashboardConfig::new()
.with_static_dir(PathBuf::from("/var/www/dashboard"));
assert_eq!(config.static_dir, PathBuf::from("/var/www/dashboard"));Sourcepub fn with_auth(self, username: &str, password_hash: &str) -> Self
pub fn with_auth(self, username: &str, password_hash: &str) -> Self
Enable authentication with username and password hash.
The password should be a bcrypt hash for security. When authentication is enabled, all API endpoints and WebSocket connections will require basic authentication.
§Examples
use hammerwork_web::config::DashboardConfig;
let config = DashboardConfig::new()
.with_auth("admin", "$2b$12$hash...");
assert!(config.auth.enabled);
assert_eq!(config.auth.username, "admin");
assert_eq!(config.auth.password_hash, "$2b$12$hash...");Sourcepub fn with_cors(self, enabled: bool) -> Self
pub fn with_cors(self, enabled: bool) -> Self
Enable or disable CORS support.
When enabled, browsers on the origins in allowed_origins
may call the API; validate requires at least one of them.
§Examples
use hammerwork_web::config::DashboardConfig;
let config = DashboardConfig::new()
.with_cors(true)
.with_allowed_origin("https://ops.example.com");
assert!(config.enable_cors);
assert_eq!(config.allowed_origins, ["https://ops.example.com"]);
let config = DashboardConfig::new()
.with_cors(false);
assert!(!config.enable_cors);Sourcepub fn with_allowed_origin(self, origin: &str) -> Self
pub fn with_allowed_origin(self, origin: &str) -> Self
Allow requests from another origin (scheme://host[:port]); see
allowed_origins.
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Check settings that would make the server unsafe or make it fail at runtime:
- authentication enabled in a build without the
authfeature, which cannot verify bcrypt password hashes; - an entry of
allowed_originsthat is not an origin, orenable_corswithout any; - a zero
websocket.ping_interval(tokio::time::intervalpanics on a zero period),websocket.message_buffer_sizeorwebsocket.max_message_size; - a zero
websocket.live_update_max_jobswhile live updates are enabled.
Sourcepub fn save_to_file(&self, path: &str) -> Result<()>
pub fn save_to_file(&self, path: &str) -> Result<()>
Save configuration to a TOML file
Trait Implementations§
Source§impl Clone for DashboardConfig
impl Clone for DashboardConfig
Source§impl Debug for DashboardConfig
impl Debug for DashboardConfig
Source§impl Default for DashboardConfig
impl Default for DashboardConfig
Source§impl<'de> Deserialize<'de> for DashboardConfig
impl<'de> Deserialize<'de> for DashboardConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for DashboardConfig
impl RefUnwindSafe for DashboardConfig
impl Send for DashboardConfig
impl Sync for DashboardConfig
impl Unpin for DashboardConfig
impl UnsafeUnpin for DashboardConfig
impl UnwindSafe for DashboardConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more