Make web server address configurable

This commit is contained in:
Joscha 2023-08-08 23:55:34 +02:00
parent 3a4a4c1cfa
commit 8b53a22b78
3 changed files with 20 additions and 10 deletions

View file

@ -1,6 +1,6 @@
//! Configuration from a file. //! Configuration from a file.
use std::{fs, io::ErrorKind, path::Path, time::Duration}; use std::{fs, io::ErrorKind, net::SocketAddr, path::Path, time::Duration};
use serde::Deserialize; use serde::Deserialize;
use tracing::{debug, info}; use tracing::{debug, info};
@ -8,12 +8,17 @@ use tracing::{debug, info};
use crate::somehow; use crate::somehow;
mod default { mod default {
use std::time::Duration; use std::{net::SocketAddr, time::Duration};
pub fn web_base() -> String { pub fn web_base() -> String {
"".to_string() "".to_string()
} }
pub fn web_address() -> SocketAddr {
// Port chosen by fair dice roll
"[::]:8221".parse().unwrap()
}
pub fn repo_name() -> String { pub fn repo_name() -> String {
"local repo".to_string() "local repo".to_string()
} }
@ -27,12 +32,15 @@ mod default {
pub struct Web { pub struct Web {
#[serde(default = "default::web_base")] #[serde(default = "default::web_base")]
pub base: String, pub base: String,
#[serde(default = "default::web_address")]
pub address: SocketAddr,
} }
impl Default for Web { impl Default for Web {
fn default() -> Self { fn default() -> Self {
Self { Self {
base: default::web_base(), base: default::web_base(),
address: default::web_address(),
} }
} }
} }
@ -86,9 +94,10 @@ impl ConfigFile {
} }
pub struct Config { pub struct Config {
pub web_base: String,
pub web_address: SocketAddr,
pub repo_name: String, pub repo_name: String,
pub repo_update_delay: Duration, pub repo_update_delay: Duration,
pub web_base: String,
} }
impl Config { impl Config {
@ -100,9 +109,10 @@ impl Config {
let web_base = config_file.web_base(); let web_base = config_file.web_base();
Ok(Self { Ok(Self {
web_base,
web_address: config_file.web.address,
repo_name: config_file.repo.name, repo_name: config_file.repo.name,
repo_update_delay: config_file.repo.update_delay, repo_update_delay: config_file.repo.update_delay,
web_base,
}) })
} }
} }

View file

@ -28,9 +28,9 @@ async fn recurring_task(state: &Server) {
.await; .await;
} }
pub async fn run(state: Server) { pub async fn run(server: Server) {
loop { loop {
recurring_task(&state).await; recurring_task(&server).await;
tokio::time::sleep(state.config.repo_update_delay).await; tokio::time::sleep(server.config.repo_update_delay).await;
} }
} }

View file

@ -39,7 +39,7 @@ impl Base {
} }
} }
pub async fn run(state: Server) -> somehow::Result<()> { pub async fn run(server: Server) -> somehow::Result<()> {
// TODO Add text body to body-less status codes // TODO Add text body to body-less status codes
let app = Router::new() let app = Router::new()
@ -50,9 +50,9 @@ pub async fn run(state: Server) -> somehow::Result<()> {
.route("/queue/table", get(queue::get_table)) .route("/queue/table", get(queue::get_table))
.route("/queue/:id", get(queue_id::get)) .route("/queue/:id", get(queue_id::get))
.fallback(get(r#static::static_handler)) .fallback(get(r#static::static_handler))
.with_state(state.clone()); .with_state(server.clone());
axum::Server::bind(&"0.0.0.0:8000".parse().unwrap()) axum::Server::bind(&server.config.web_address)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await?; .await?;