Skip to content

Commit c67f326

Browse files
committed
small usability improvements
1 parent a9ce743 commit c67f326

File tree

7 files changed

+58
-49
lines changed

7 files changed

+58
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- The previous version reduced log verbosity, but also removed the ability to see the HTTP requests in the logs.
66
This is now fixed, and you can see the HTTP requests again. Logging is still less verbose than before, but you can enable debug logs by setting the `RUST_LOG` environment variable to `debug`, or to `sqlpage=debug` to only see SQLPage debug logs.
77
- Better error message when failing to bind to a low port (<1024) on Linux. SQLPage now displays a message explaining how to allow SQLPage to bind to a low port.
8+
- When https_domain is set, but a port number different from 443 is set, SQLPage now starts both an HTTP and an HTTPS server.
89

910
## 0.17.0
1011

Cargo.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlpage"
3-
version = "0.17.0"
3+
version = "0.17.1"
44
edition = "2021"
55
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
66
keywords = ["web", "sql", "framework"]

sqlpage/sqlpage.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc",
3-
"listen_on": "localhost:8080"
2+
"database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc"
43
}

src/app_config.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ pub struct AppConfig {
2121
#[serde(default)]
2222
pub sqlite_extensions: Vec<String>,
2323

24-
#[serde(
25-
deserialize_with = "deserialize_socket_addr",
26-
default = "default_listen_on"
27-
)]
28-
pub listen_on: SocketAddr,
24+
#[serde(default, deserialize_with = "deserialize_socket_addr")]
25+
pub listen_on: Option<SocketAddr>,
2926
pub port: Option<u16>,
3027

3128
/// Number of times to retry connecting to the database after a failure when the server starts
@@ -74,18 +71,31 @@ pub struct AppConfig {
7471
pub https_acme_directory_url: String,
7572
}
7673

74+
impl AppConfig {
75+
#[must_use]
76+
pub fn listen_on(&self) -> SocketAddr {
77+
let mut addr = self.listen_on.unwrap_or_else(|| {
78+
if self.https_domain.is_some() {
79+
SocketAddr::from(([0, 0, 0, 0], 443))
80+
} else {
81+
SocketAddr::from(([0, 0, 0, 0], 8080))
82+
}
83+
});
84+
if let Some(port) = self.port {
85+
addr.set_port(port);
86+
}
87+
addr
88+
}
89+
}
90+
7791
pub fn load() -> anyhow::Result<AppConfig> {
78-
let mut conf = Config::builder()
92+
Config::builder()
7993
.add_source(config::File::with_name("sqlpage/sqlpage").required(false))
8094
.add_source(env_config())
8195
.add_source(env_config().prefix("SQLPAGE"))
8296
.build()?
8397
.try_deserialize::<AppConfig>()
84-
.with_context(|| "Unable to load configuration")?;
85-
if let Some(port) = conf.port {
86-
conf.listen_on.set_port(port);
87-
}
88-
Ok(conf)
98+
.with_context(|| "Unable to load configuration")
8999
}
90100

91101
fn env_config() -> config::Environment {
@@ -95,16 +105,13 @@ fn env_config() -> config::Environment {
95105
.with_list_parse_key("sqlite_extensions")
96106
}
97107

98-
#[must_use]
99-
pub fn default_listen_on() -> SocketAddr {
100-
SocketAddr::from(([0, 0, 0, 0], 8080))
101-
}
102-
103108
fn deserialize_socket_addr<'de, D: Deserializer<'de>>(
104109
deserializer: D,
105-
) -> Result<SocketAddr, D::Error> {
106-
let host_str: String = Deserialize::deserialize(deserializer)?;
107-
parse_socket_addr(&host_str).map_err(D::Error::custom)
110+
) -> Result<Option<SocketAddr>, D::Error> {
111+
let host_str: Option<String> = Deserialize::deserialize(deserializer)?;
112+
host_str
113+
.map(|h| parse_socket_addr(&h).map_err(D::Error::custom))
114+
.transpose()
108115
}
109116

110117
fn parse_socket_addr(host_str: &str) -> anyhow::Result<SocketAddr> {

src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ async fn main() {
1414

1515
async fn start() -> anyhow::Result<()> {
1616
let app_config = app_config::load()?;
17-
log::debug!("Starting with the following configuration: {app_config:?}");
17+
log::debug!("Starting with the following configuration: {app_config:#?}");
1818
let state = AppState::init(&app_config).await?;
1919
webserver::database::migrations::apply(&state.db).await?;
20-
log::debug!("Starting server on {}", app_config.listen_on);
20+
log::debug!("Starting server...");
2121
let (r, _) = tokio::join!(
2222
webserver::http::run_server(&app_config, state),
2323
log_welcome_message(&app_config)
@@ -27,7 +27,7 @@ async fn start() -> anyhow::Result<()> {
2727

2828
async fn log_welcome_message(config: &AppConfig) {
2929
// Don't show 0.0.0.0 as the host, show the actual IP address
30-
let http_addr = config.listen_on.to_string().replace(
30+
let http_addr = config.listen_on().to_string().replace(
3131
"0.0.0.0",
3232
std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
3333
.to_string()
@@ -36,13 +36,12 @@ async fn log_welcome_message(config: &AppConfig) {
3636

3737
log::info!(
3838
"Server started successfully.
39-
SQLPage is now running on http://{}/ {}
39+
SQLPage is now running on {}
4040
You can write your website's code in .sql files in {}.",
41-
http_addr,
4241
if let Some(domain) = &config.https_domain {
43-
format!("and on https://{}", domain)
42+
format!("https://{}", domain)
4443
} else {
45-
"".to_string()
44+
format!("http://{}", http_addr)
4645
},
4746
config.web_root.display()
4847
);

src/webserver/http.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pub fn create_app(
482482
}
483483

484484
pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<()> {
485-
let mut listen_on = config.listen_on;
485+
let listen_on = config.listen_on();
486486
let state = web::Data::new(state);
487487
let factory = move || create_app(web::Data::clone(&state));
488488

@@ -495,18 +495,21 @@ pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<(
495495
}
496496
let mut server = HttpServer::new(factory);
497497
if let Some(domain) = &config.https_domain {
498-
listen_on.set_port(443);
498+
let mut listen_on_https = listen_on;
499+
listen_on_https.set_port(443);
499500
log::info!("Will start HTTPS server on {listen_on}");
500501
let config = make_auto_rustls_config(domain, config);
501502
server = server
502503
.bind_rustls_021(listen_on, config)
503504
.map_err(|e| bind_error(e, listen_on))?;
504-
} else if listen_on.port() != 443 {
505+
} else if listen_on.port() == 443 {
506+
bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS (port 443)");
507+
}
508+
if listen_on.port() != 443 {
509+
log::info!("Will start HTTP server on {listen_on}");
505510
server = server
506511
.bind(listen_on)
507512
.map_err(|e| bind_error(e, listen_on))?;
508-
} else {
509-
bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS.");
510513
}
511514
server
512515
.run()

0 commit comments

Comments
 (0)