Externalized web server config

This commit is contained in:
rzuasti
2026-02-26 16:30:51 -05:00
parent 7f754b3d17
commit 874a70969f
3 changed files with 31 additions and 6 deletions
+8
View File
@@ -41,6 +41,13 @@ pub struct Notifications {
pub notify_when_not_seen_for: DurationString,
}
#[derive(Debug, Deserialize, Clone)]
pub struct WebServer {
pub ip_address: String,
pub port: u16,
pub api_key: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
pub database: Database,
@@ -48,6 +55,7 @@ pub struct Settings {
pub log: Log,
pub timings: Timings,
pub notifications: Notifications,
pub web_server: WebServer,
}
// End configuration structure
// -----------------------------------------------------------
+18 -6
View File
@@ -1,20 +1,19 @@
use std::error::Error;
use crate::settings::get_settings;
use axum::Router;
use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::Next;
use axum::response::Response;
use axum::routing::{delete, get, put};
use log::{debug, info};
use log::{debug, error, info};
use tower_http::services::ServeDir;
pub mod devices;
pub mod notifications;
pub mod utils;
const API_KEY: &str = "very_secret_key";
pub async fn serve() -> Result<(), Box<dyn Error>> {
info!("Starting web server");
let static_files = ServeDir::new("./web");
@@ -36,9 +35,16 @@ pub async fn serve() -> Result<(), Box<dyn Error>> {
get(|| async { "Go to /web for the UI or to /api for the better UI." }),
)
.nest_service("/web", static_files);
info!("Web server starting at http://0.0.0.0:3000");
let web_server_host_and_port = format!(
"{}:{}",
get_settings().web_server.ip_address,
get_settings().web_server.port
);
info!("Web server starting at http://{}", web_server_host_and_port);
// Start the server
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
let listener = tokio::net::TcpListener::bind(web_server_host_and_port).await?;
debug!("Web server bound to IP and port");
@@ -58,7 +64,13 @@ async fn auth(request: Request, next: Next) -> Result<Response, StatusCode> {
};
let mut valid_header = "Bearer ".to_string();
valid_header.push_str(API_KEY);
let api_key = get_settings().web_server.api_key.as_str();
if api_key == "CHANGE_ME" {
error!("The configured api_key is still 'CHANGE_ME', please change it.");
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
valid_header.push_str(api_key);
if auth_header == valid_header {
Ok(next.run(request).await)