diff --git a/config.toml.example b/config.toml.example index e172d3e..df589d3 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1,5 +1,5 @@ padlock-secret = "" -port = 80 +listen = "[::]:80" [database] connection-string = "sqlite://sqlite.db" diff --git a/src/config.rs b/src/config.rs index ef5901f..2ec75d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,13 +1,20 @@ +use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6}; + use serde::Deserialize; use crate::secrets::PadlockGenerationSecret; +fn default_listen_addr() -> SocketAddr { + SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 80, 0, 0)) +} + #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "kebab-case")] pub struct Config { #[serde(with = "hex::serde")] pub padlock_secret: PadlockGenerationSecret, - pub port: u16, + #[serde(default = "default_listen_addr")] + pub listen: SocketAddr, pub database: DatabaseConfig, #[serde(default)] diff --git a/src/main.rs b/src/main.rs index 1eaaad5..73c54d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,7 +99,7 @@ async fn main() -> Result<()> { )); tokio::spawn(server::run( - config.port, + config.listen, user_authenticator, padlock_generator, user_server_key_generator, diff --git a/src/server.rs b/src/server.rs index 960aa3c..8f70570 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -use std::{net::Ipv6Addr, sync::Arc}; +use std::{net::SocketAddr, sync::Arc}; use axum::{ extract::{Query, State}, @@ -7,6 +7,7 @@ use axum::{ routing::{get, post}, Form, Json, Router, }; +use color_eyre::eyre::Context; use secrecy::ExposeSecret; use serde::{Deserialize, Serialize}; use tracing::{event, instrument, Level}; @@ -25,7 +26,7 @@ struct AppState { #[instrument] pub async fn run( - port: u16, + listen: SocketAddr, user_authenticator: Arc, server_padlock_generator: Arc, user_server_key_generator: Arc, @@ -49,7 +50,9 @@ pub async fn run( ) .with_state(app_state); - let listener = tokio::net::TcpListener::bind((Ipv6Addr::UNSPECIFIED, port)).await?; + let listener = tokio::net::TcpListener::bind(listen) + .await + .context(format!("Failed to listen on {listen}"))?; axum::serve(listener, app).await?; Ok(())