Skip to content

Commit e85a531

Browse files
committed
fix(config): default split DB ports by backend
When using the split `[database]` fields instead of a full `connection_url`, `port` was previously required in both `validate()` and `database_url()`. Make it optional and fall back to the backend's well-known default via a new `DatabaseBackend::default_port()` (3306 for MySQL, 5432 for PostgreSQL). Callers that already set `port` explicitly continue to win. Also: - drop `port` from the two user-facing error messages (`Error::ConfigValidation` in `Config::validate` and `Error::InvalidDatabaseConfig` as the defensive fallthrough in `Config::database_url`) so they match the new required-field set; - exit with status 1 (instead of a silent `return`) when `App::new` or `App::serve` fails, so init failures are visible to systemd and other supervisors rather than looking like a clean shutdown; - document the port default in the example `config.toml` comment; - add MySQL and PostgreSQL port-omission regression tests. Signed-off-by: JmPotato <github@ipotato.me>
1 parent 92f828a commit e85a531

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ backend = "mysql"
3838
# connection_url = "postgres://postgres:password@127.0.0.1:5432/rsomhaP"
3939
#
4040
# If you use the split fields below instead of `connection_url`, `backend`
41-
# controls whether `mysql://` or `postgres://` is generated.
41+
# controls whether `mysql://` or `postgres://` is generated. `port` is
42+
# optional; it defaults to 3306 for MySQL and 5432 for PostgreSQL.
4243
username = "root"
4344
password = "password"
4445
host = "127.0.0.1"

src/config.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ impl DatabaseBackend {
4949
Self::Postgres => "postgres",
5050
}
5151
}
52+
53+
fn default_port(self) -> u16 {
54+
match self {
55+
Self::MySql => 3306,
56+
Self::Postgres => 5432,
57+
}
58+
}
5259
}
5360

5461
#[derive(Clone, Debug, Deserialize)]
@@ -207,11 +214,10 @@ impl Config {
207214
&& (self.database.username.is_none()
208215
|| self.database.password.is_none()
209216
|| self.database.host.is_none()
210-
|| self.database.port.is_none()
211217
|| self.database.database.is_none())
212218
{
213219
return Err(Error::ConfigValidation(
214-
"invalid database config, please specify the connection URL or the username, password, host, port and database".to_string(),
220+
"invalid database config, please specify the connection URL or the username, password, host and database".to_string(),
215221
));
216222
}
217223

@@ -244,7 +250,9 @@ impl Config {
244250
.host
245251
.as_ref()
246252
.ok_or(Error::InvalidDatabaseConfig)?,
247-
self.database.port.ok_or(Error::InvalidDatabaseConfig)?,
253+
self.database
254+
.port
255+
.unwrap_or(self.database.backend.default_port()),
248256
self.database
249257
.database
250258
.as_ref()
@@ -402,6 +410,43 @@ database = "rsomhaP"
402410
);
403411
}
404412

413+
#[test]
414+
fn test_database_url_defaults_mysql_port_when_split_fields_omit_port() {
415+
let config = parse_config(
416+
r#"
417+
username = "root"
418+
password = "password"
419+
host = "127.0.0.1"
420+
database = "rsomhaP"
421+
"#,
422+
);
423+
424+
config.validate().unwrap();
425+
assert_eq!(
426+
config.database_url().unwrap(),
427+
"mysql://root:password@127.0.0.1:3306/rsomhaP"
428+
);
429+
}
430+
431+
#[test]
432+
fn test_database_url_defaults_postgres_port_when_split_fields_omit_port() {
433+
let config = parse_config(
434+
r#"
435+
backend = "postgres"
436+
username = "postgres"
437+
password = "password"
438+
host = "127.0.0.1"
439+
database = "rsomhaP"
440+
"#,
441+
);
442+
443+
config.validate().unwrap();
444+
assert_eq!(
445+
config.database_url().unwrap(),
446+
"postgres://postgres:password@127.0.0.1:5432/rsomhaP"
447+
);
448+
}
449+
405450
#[test]
406451
fn test_database_url_prefers_connection_url_over_backend_field() {
407452
let config = parse_config(

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum Error {
1818
#[error("config validation failed: {0}")]
1919
ConfigValidation(String),
2020

21-
#[error("invalid database config, please specify the connection URL or the username, password, host, port and database")]
21+
#[error("invalid database config, please specify the connection URL or the username, password, host and database")]
2222
InvalidDatabaseConfig,
2323

2424
#[error("page with same title {0} already exists")]

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ async fn main() {
99
Ok(app) => app,
1010
Err(e) => {
1111
error!("failed to create app: {}", e);
12-
return;
12+
std::process::exit(1);
1313
}
1414
};
1515
if let Err(e) = app.serve().await {
1616
error!("failed to serve app: {}", e);
17-
return;
17+
std::process::exit(1);
1818
}
1919
}

0 commit comments

Comments
 (0)