Skip to content

Commit

Permalink
small usability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Dec 1, 2023
1 parent a9ce743 commit c67f326
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- The previous version reduced log verbosity, but also removed the ability to see the HTTP requests in the logs.
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.
- 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.
- When https_domain is set, but a port number different from 443 is set, SQLPage now starts both an HTTP and an HTTPS server.

## 0.17.0

Expand Down
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlpage"
version = "0.17.0"
version = "0.17.1"
edition = "2021"
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
keywords = ["web", "sql", "framework"]
Expand Down
3 changes: 1 addition & 2 deletions sqlpage/sqlpage.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc",
"listen_on": "localhost:8080"
"database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc"
}
45 changes: 26 additions & 19 deletions src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ pub struct AppConfig {
#[serde(default)]
pub sqlite_extensions: Vec<String>,

#[serde(
deserialize_with = "deserialize_socket_addr",
default = "default_listen_on"
)]
pub listen_on: SocketAddr,
#[serde(default, deserialize_with = "deserialize_socket_addr")]
pub listen_on: Option<SocketAddr>,
pub port: Option<u16>,

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

impl AppConfig {
#[must_use]
pub fn listen_on(&self) -> SocketAddr {
let mut addr = self.listen_on.unwrap_or_else(|| {
if self.https_domain.is_some() {
SocketAddr::from(([0, 0, 0, 0], 443))
} else {
SocketAddr::from(([0, 0, 0, 0], 8080))
}
});
if let Some(port) = self.port {
addr.set_port(port);
}
addr
}
}

pub fn load() -> anyhow::Result<AppConfig> {
let mut conf = Config::builder()
Config::builder()
.add_source(config::File::with_name("sqlpage/sqlpage").required(false))
.add_source(env_config())
.add_source(env_config().prefix("SQLPAGE"))
.build()?
.try_deserialize::<AppConfig>()
.with_context(|| "Unable to load configuration")?;
if let Some(port) = conf.port {
conf.listen_on.set_port(port);
}
Ok(conf)
.with_context(|| "Unable to load configuration")
}

fn env_config() -> config::Environment {
Expand All @@ -95,16 +105,13 @@ fn env_config() -> config::Environment {
.with_list_parse_key("sqlite_extensions")
}

#[must_use]
pub fn default_listen_on() -> SocketAddr {
SocketAddr::from(([0, 0, 0, 0], 8080))
}

fn deserialize_socket_addr<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<SocketAddr, D::Error> {
let host_str: String = Deserialize::deserialize(deserializer)?;
parse_socket_addr(&host_str).map_err(D::Error::custom)
) -> Result<Option<SocketAddr>, D::Error> {
let host_str: Option<String> = Deserialize::deserialize(deserializer)?;
host_str
.map(|h| parse_socket_addr(&h).map_err(D::Error::custom))
.transpose()
}

fn parse_socket_addr(host_str: &str) -> anyhow::Result<SocketAddr> {
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ async fn main() {

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

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

log::info!(
"Server started successfully.
SQLPage is now running on http://{}/ {}
SQLPage is now running on {}
You can write your website's code in .sql files in {}.",
http_addr,
if let Some(domain) = &config.https_domain {
format!("and on https://{}", domain)
format!("https://{}", domain)
} else {
"".to_string()
format!("http://{}", http_addr)
},
config.web_root.display()
);
Expand Down
13 changes: 8 additions & 5 deletions src/webserver/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ pub fn create_app(
}

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

Expand All @@ -495,18 +495,21 @@ pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<(
}
let mut server = HttpServer::new(factory);
if let Some(domain) = &config.https_domain {
listen_on.set_port(443);
let mut listen_on_https = listen_on;
listen_on_https.set_port(443);
log::info!("Will start HTTPS server on {listen_on}");
let config = make_auto_rustls_config(domain, config);
server = server
.bind_rustls_021(listen_on, config)
.map_err(|e| bind_error(e, listen_on))?;
} else if listen_on.port() != 443 {
} else if listen_on.port() == 443 {
bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS (port 443)");
}
if listen_on.port() != 443 {
log::info!("Will start HTTP server on {listen_on}");
server = server
.bind(listen_on)
.map_err(|e| bind_error(e, listen_on))?;
} else {
bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS.");
}
server
.run()
Expand Down

0 comments on commit c67f326

Please sign in to comment.