Skip to main content

httpward_core/config/
site.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use std::sync::Arc;
4
5use crate::config::global::{Listener, Route};
6use crate::config::strategy::{Strategy, StrategyRef, LegacyStrategyCollection as StrategyCollection};
7
8/// Configuration for one virtual host / site
9#[derive(Debug, Clone, Deserialize, Serialize, Default, JsonSchema)]
10#[serde(default, deny_unknown_fields)]
11pub struct SiteConfig {
12    /// Primary domain name (used for SNI matching & logging)
13    #[serde(default)]
14    pub domain: String,
15
16    /// Additional domain names / aliases
17    #[serde(default)]
18    pub domains: Vec<String>,
19
20    /// Optional site-specific listeners (overrides global listeners)
21    #[serde(default)]
22    pub listeners: Vec<Listener>,
23
24    /// Site-level routing rules
25    #[serde(default)]
26    pub routes: Vec<Route>,
27
28    /// Site-specific strategy (overrides global default)
29    #[serde(default)]
30    pub strategy: Option<StrategyRef>,
31
32    /// Site-specific strategy definitions
33    #[serde(default)]
34    pub strategies: StrategyCollection,
35}
36
37impl SiteConfig {
38    /// Get all domains for this site config (primary + additional)
39    pub fn get_all_domains(&self) -> Vec<String> {
40        let mut domains = Vec::with_capacity(1 + self.domains.len());
41        if !self.domain.is_empty() {
42            domains.push(self.domain.clone());
43        }
44        domains.extend(self.domains.iter().cloned());
45        domains
46    }
47
48    /// Check if this site config has any domains configured
49    pub fn has_domains(&self) -> bool {
50        !self.domain.is_empty() || !self.domains.is_empty()
51    }
52
53    /// Get the site strategy reference
54    pub fn get_strategy(&self) -> Option<&StrategyRef> {
55        self.strategy.as_ref()
56    }
57
58    /// Get a strategy from the site's strategy collection
59    pub fn get_site_strategy(&self, name: &str) -> Option<Strategy> {
60        self.strategies.get(name).map(|middleware| Strategy {
61            name: name.to_string(),
62            middleware: Arc::new(middleware.clone()),
63        })
64    }
65
66}