Allow specifying IP address to listen on

This commit is contained in:
Xiretza 2024-02-15 19:58:33 +00:00
parent 0e2d440489
commit fa7dc5a3f9
4 changed files with 16 additions and 6 deletions

View file

@ -1,5 +1,5 @@
padlock-secret = "" padlock-secret = ""
port = 80 listen = "[::]:80"
[database] [database]
connection-string = "sqlite://sqlite.db" connection-string = "sqlite://sqlite.db"

View file

@ -1,13 +1,20 @@
use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6};
use serde::Deserialize; use serde::Deserialize;
use crate::secrets::PadlockGenerationSecret; use crate::secrets::PadlockGenerationSecret;
fn default_listen_addr() -> SocketAddr {
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 80, 0, 0))
}
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct Config { pub struct Config {
#[serde(with = "hex::serde")] #[serde(with = "hex::serde")]
pub padlock_secret: PadlockGenerationSecret, pub padlock_secret: PadlockGenerationSecret,
pub port: u16, #[serde(default = "default_listen_addr")]
pub listen: SocketAddr,
pub database: DatabaseConfig, pub database: DatabaseConfig,
#[serde(default)] #[serde(default)]

View file

@ -99,7 +99,7 @@ async fn main() -> Result<()> {
)); ));
tokio::spawn(server::run( tokio::spawn(server::run(
config.port, config.listen,
user_authenticator, user_authenticator,
padlock_generator, padlock_generator,
user_server_key_generator, user_server_key_generator,

View file

@ -1,4 +1,4 @@
use std::{net::Ipv6Addr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use axum::{ use axum::{
extract::{Query, State}, extract::{Query, State},
@ -7,6 +7,7 @@ use axum::{
routing::{get, post}, routing::{get, post},
Form, Json, Router, Form, Json, Router,
}; };
use color_eyre::eyre::Context;
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::{event, instrument, Level}; use tracing::{event, instrument, Level};
@ -25,7 +26,7 @@ struct AppState {
#[instrument] #[instrument]
pub async fn run( pub async fn run(
port: u16, listen: SocketAddr,
user_authenticator: Arc<UserAuthenticator>, user_authenticator: Arc<UserAuthenticator>,
server_padlock_generator: Arc<ServerPadlockGenerator>, server_padlock_generator: Arc<ServerPadlockGenerator>,
user_server_key_generator: Arc<UserServerKeyGenerator>, user_server_key_generator: Arc<UserServerKeyGenerator>,
@ -49,7 +50,9 @@ pub async fn run(
) )
.with_state(app_state); .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?; axum::serve(listener, app).await?;
Ok(()) Ok(())