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
8 changes: 5 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,11 @@ Run `floo notifications list` to see the available categories."
/// Inspect custom domains declared in floo.app.toml.
#[command(
subcommand,
after_help = "Declare [domains.\"<host>\"] in floo.app.toml and release to prod.
Publish the DNS records printed by the deploy or `floo domains show <host>`,
then run `floo domains watch <host>`. Remove the block to retire the domain."
after_help = "Declare [domains.\"<host>\"] in floo.app.toml. A domain belongs to prod
by default; `environment = \"dev\"` attaches it to dev instead, and that
environment's deploy applies it. Publish the DNS records printed by the deploy
or `floo domains show <host>`, then run `floo domains watch <host>`. Remove the
block to retire the domain."
)]
Domains(DomainsCommands),

Expand Down
4 changes: 2 additions & 2 deletions src/commands/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn list(app: Option<&str>) {
if !output::is_json_mode() {
output::info(
&format!(
"No custom domains on {app_name}. Declare [domains.\"<host>\"] in floo.app.toml and release to prod."
"No custom domains on {app_name}. Declare [domains.\"<host>\"] in floo.app.toml and deploy its environment (prod unless the block sets `environment = \"dev\"`)."
),
None,
);
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn watch(hostname: &str, app: Option<&str>, timeout_secs: u64) {
output::error(
&format!("Domain {hostname} is {}.", domain_status(Some("removed"))),
&ErrorCode::DomainNotFound,
Some("Re-declare the domain in floo.app.toml and release to prod to restore."),
Some("Re-declare the domain in floo.app.toml and deploy its environment to restore."),
);
process::exit(1);
}
Expand Down
86 changes: 80 additions & 6 deletions src/project_config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,25 @@ pub struct ManagedServiceBlock {

const VALID_MANAGED_SERVICE_TYPES: &[&str] = &["postgres", "redis", "storage"];

/// A custom domain declared as `[domains."<hostname>"]` on the prod release.
/// `service` selects its backend; omission uses the app default. Removing the
/// block retires the domain while retaining its certificate for seven days.
/// A custom domain declared as `[domains."<hostname>"]`. It belongs to prod by
/// default; `environment = "dev"` attaches it to dev instead, and that
/// environment's deploy applies it. `service` selects its backend; omission uses
/// the app default. Removing the block retires the domain while retaining its
/// certificate for seven days.
#[derive(Debug, Serialize, Default, Clone)]
pub struct DomainBlock {
#[serde(skip_serializing_if = "Option::is_none")]
pub service: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<DomainEnvironment>,
}

/// The environment a custom domain attaches to; unset means prod.
#[derive(Debug, Serialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum DomainEnvironment {
Dev,
Prod,
}

impl<'de> Deserialize<'de> for DomainBlock {
Expand All @@ -181,17 +193,31 @@ impl<'de> Deserialize<'de> for DomainBlock {
#[serde(deny_unknown_fields)]
struct Fields {
service: Option<String>,
environment: Option<String>,
}

// Keep serde's key-specific rejection and attach migration guidance at
// this boundary so both app and service config parsers explain removal.
let fields = Fields::deserialize(deserializer).map_err(|err| {
serde::de::Error::custom(format!(
"{err}; domain blocks accept only `service`; remove the block to remove the domain"
"{err}; domain blocks accept only `service` and `environment`; \
remove the block to remove the domain"
))
})?;
let environment = match fields.environment.as_deref() {
None => None,
Some("dev") => Some(DomainEnvironment::Dev),
Some("prod") => Some(DomainEnvironment::Prod),
Some(other) => {
return Err(serde::de::Error::custom(format!(
"domain `environment` must be \"dev\" or \"prod\", got \"{other}\"; \
omit it to attach the domain to prod"
)))
}
};
Ok(Self {
service: fields.service,
environment,
})
}
}
Expand Down Expand Up @@ -2079,17 +2105,65 @@ name = "my-app"
service = "web"

[domains."api.example.com"]

[domains."dev.example.com"]
environment = "dev"

[domains."www.example.com"]
environment = "prod"
"#,
)
.unwrap();

let config = load_app_config(dir.path()).unwrap().unwrap();
assert_eq!(config.domains.len(), 2);
assert_eq!(config.domains.len(), 4);
assert_eq!(
config.domains["app.example.com"].service.as_deref(),
Some("web")
);
assert!(config.domains.contains_key("api.example.com"));
assert_eq!(config.domains["app.example.com"].environment, None);
assert_eq!(config.domains["api.example.com"].environment, None);
assert_eq!(
config.domains["dev.example.com"].environment,
Some(DomainEnvironment::Dev)
);
assert_eq!(
config.domains["www.example.com"].environment,
Some(DomainEnvironment::Prod)
);

// An omitted environment stays omitted, so existing files round-trip unchanged.
let serialized = toml::to_string(&config.domains["app.example.com"]).unwrap();
assert_eq!(serialized.trim(), r#"service = "web""#);
let serialized = toml::to_string(&config.domains["dev.example.com"]).unwrap();
assert_eq!(serialized.trim(), r#"environment = "dev""#);
}

#[test]
fn test_load_app_config_rejects_unknown_domain_environment() {
let _guard = crate::output::GLOBAL_MODE_LOCK.lock().unwrap();
crate::output::set_json_mode(false);
crate::output::set_dry_run_mode(false);
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join(super::super::APP_CONFIG_FILE),
r#"[app]
name = "my-app"
[domains."app.example.com"]
environment = "preview"
"#,
)
.unwrap();

let err = load_app_config(dir.path()).unwrap_err();
assert_eq!(err.code, ErrorCode::InvalidProjectConfig);
assert!(
err.message
.contains(r#"domain `environment` must be "dev" or "prod", got "preview""#),
"got: {}",
err.message
);
assert!(!err.suggestion.unwrap().contains("floo update"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/project_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(super) fn toml_parse_error(file_label: &str, err: toml::de::Error) -> crate:
None => field.to_string(),
};
let correction = if raw.contains("remove the block to remove the domain") {
"Domain blocks accept only `service`; remove the block to remove the domain."
"Domain blocks accept only `service` and `environment`; remove the block to remove the domain."
} else {
"Remove or correct it."
};
Expand Down
24 changes: 24 additions & 0 deletions src/project_config/service_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,30 @@ domain = "app.example.com"
assert!(err.suggestion.unwrap().contains("`service.domain`"));
}

#[test]
fn test_load_service_config_accepts_dev_domain() {
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join(super::super::SERVICE_CONFIG_FILE),
r#"[app]
name = "my-app"
[service]
name = "web"
type = "web"
port = 3000
[domains."dev.example.com"]
environment = "dev"
"#,
)
.unwrap();

let config = load_service_config(dir.path()).unwrap().unwrap();
assert_eq!(
config.domains["dev.example.com"].environment,
Some(super::super::app_config::DomainEnvironment::Dev)
);
}

#[test]
fn test_load_service_config_with_resources() {
let dir = TempDir::new().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ size = "large"
[managed.db.users]
[domains."app.example.com"]
service = "web"
environment = "dev"
[edge]
rules = [{ action = "allow", cidr = "10.0.0.0/8" }]
default_action = "deny"
Expand Down