Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions crates/daemon-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,50 @@ pub enum ProxyBackend {
#[default]
Caddy,
Nginx,
Apache,
Traefik,
None,
}

impl ProxyBackend {
/// The proxies the panel can pick for a machine.
pub const CHOOSABLE: [ProxyBackend; 4] =
[Self::Caddy, Self::Nginx, Self::Apache, Self::Traefik];

pub fn name(self) -> &'static str {
match self {
Self::Caddy => "caddy",
Self::Nginx => "nginx",
Self::Apache => "apache",
Self::Traefik => "traefik",
Self::None => "none",
}
}

pub fn from_name(name: &str) -> Option<Self> {
[
Self::Caddy,
Self::Nginx,
Self::Apache,
Self::Traefik,
Self::None,
]
.into_iter()
.find(|b| b.name() == name)
}

/// How people write it.
pub fn label(self) -> &'static str {
match self {
Self::Caddy => "Caddy",
Self::Nginx => "nginx",
Self::Apache => "Apache",
Self::Traefik => "Traefik",
Self::None => "no proxy",
}
}
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum FirewallBackend {
Expand Down Expand Up @@ -533,4 +574,26 @@ mod tests {
let parsed: Config = toml::from_str(&text).unwrap();
assert_eq!(parsed, config);
}

#[test]
fn every_proxy_round_trips_by_name() {
for backend in [
ProxyBackend::Caddy,
ProxyBackend::Nginx,
ProxyBackend::Apache,
ProxyBackend::Traefik,
ProxyBackend::None,
] {
assert_eq!(ProxyBackend::from_name(backend.name()), Some(backend));
let mut config = Config::new("api.serveros.com", "mch_1");
config.integrations.proxy = backend;
let text = toml::to_string(&config).unwrap();
assert!(
text.contains(&format!("proxy = \"{}\"", backend.name())),
"{text}"
);
}
assert_eq!(ProxyBackend::from_name("lighttpd"), None);
assert!(!ProxyBackend::CHOOSABLE.contains(&ProxyBackend::None));
}
}
31 changes: 22 additions & 9 deletions crates/daemon-deploy/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ pub struct DeployResult {
pub struct Deployer {
pub paths: Paths,
pub state: Arc<State>,
pub proxy: Proxy,
/// The machine's reverse proxy. Behind a lock because a deploy can
/// move the machine to another one.
pub proxy: std::sync::RwLock<Proxy>,
pub build_limits: BuildLimits,
}

impl Deployer {
/// The reverse proxy in use now.
pub fn proxy(&self) -> Proxy {
self.proxy.read().unwrap_or_else(|e| e.into_inner()).clone()
}

pub fn set_proxy(&self, proxy: Proxy) {
*self.proxy.write().unwrap_or_else(|e| e.into_inner()) = proxy;
}

fn apps_dir(&self) -> PathBuf {
self.paths.state_dir.join("apps")
}
Expand Down Expand Up @@ -206,9 +217,10 @@ impl Deployer {
}
if let (Some(port), false) = (host_port, spec.domains.is_empty()) {
progress.phase("proxy", Some(80)).await;
self.proxy.ensure_installed(cancel, progress).await?;
self.proxy.ensure_import()?;
self.proxy
let proxy = self.proxy();
proxy.ensure_installed(cancel, progress).await?;
proxy.ensure_import()?;
proxy
.publish(service, &spec.domains, port, cancel, progress)
.await?;
}
Expand Down Expand Up @@ -296,9 +308,10 @@ impl Deployer {

if !spec.domains.is_empty() {
progress.phase("proxy", Some(80)).await;
self.proxy.ensure_installed(cancel, progress).await?;
self.proxy.ensure_import()?;
self.proxy
let proxy = self.proxy();
proxy.ensure_installed(cancel, progress).await?;
proxy.ensure_import()?;
proxy
.publish(service, &spec.domains, host_port, cancel, progress)
.await?;
}
Expand Down Expand Up @@ -434,7 +447,7 @@ impl Deployer {

if !domains.is_empty() {
progress.phase("proxy", Some(80)).await;
self.proxy
self.proxy()
.publish(service, domains, release.port, &mut cancel, progress)
.await?;
}
Expand Down Expand Up @@ -643,7 +656,7 @@ mod tests {
let deployer = Deployer {
paths: Paths::under(dir.path()),
state: Arc::new(State::in_memory().unwrap()),
proxy: Proxy::default(),
proxy: std::sync::RwLock::new(Proxy::default()),
build_limits: BuildLimits {
cpu_percent: 100,
memory_mb: 512,
Expand Down
Loading
Loading