mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Externalized web server config
This commit is contained in:
@@ -41,6 +41,13 @@ pub struct Notifications {
|
|||||||
pub notify_when_not_seen_for: DurationString,
|
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)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub database: Database,
|
pub database: Database,
|
||||||
@@ -48,6 +55,7 @@ pub struct Settings {
|
|||||||
pub log: Log,
|
pub log: Log,
|
||||||
pub timings: Timings,
|
pub timings: Timings,
|
||||||
pub notifications: Notifications,
|
pub notifications: Notifications,
|
||||||
|
pub web_server: WebServer,
|
||||||
}
|
}
|
||||||
// End configuration structure
|
// End configuration structure
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
|
|||||||
@@ -1,20 +1,19 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
use crate::settings::get_settings;
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
use axum::extract::Request;
|
use axum::extract::Request;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::middleware::Next;
|
use axum::middleware::Next;
|
||||||
use axum::response::Response;
|
use axum::response::Response;
|
||||||
use axum::routing::{delete, get, put};
|
use axum::routing::{delete, get, put};
|
||||||
use log::{debug, info};
|
use log::{debug, error, info};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
pub mod devices;
|
pub mod devices;
|
||||||
pub mod notifications;
|
pub mod notifications;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
const API_KEY: &str = "very_secret_key";
|
|
||||||
|
|
||||||
pub async fn serve() -> Result<(), Box<dyn Error>> {
|
pub async fn serve() -> Result<(), Box<dyn Error>> {
|
||||||
info!("Starting web server");
|
info!("Starting web server");
|
||||||
let static_files = ServeDir::new("./web");
|
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." }),
|
get(|| async { "Go to /web for the UI or to /api for the better UI." }),
|
||||||
)
|
)
|
||||||
.nest_service("/web", static_files);
|
.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
|
// 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");
|
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();
|
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 {
|
if auth_header == valid_header {
|
||||||
Ok(next.run(request).await)
|
Ok(next.run(request).await)
|
||||||
|
|||||||
@@ -19,3 +19,8 @@ notify_when_not_seen_for="1w" # Send a notification if a device comes back onlin
|
|||||||
[notifications.pushover]
|
[notifications.pushover]
|
||||||
token="" # Your pushover token goes here, just copy&paste from their website after creating the app
|
token="" # Your pushover token goes here, just copy&paste from their website after creating the app
|
||||||
user_key="" # User key goes here, this is the account wide code for pushover
|
user_key="" # User key goes here, this is the account wide code for pushover
|
||||||
|
|
||||||
|
[web_server]
|
||||||
|
ip_address="0.0.0.0" # IP to bind the web server for the API and web UI to, use 0.0.0.0 to bind it to all interfaces
|
||||||
|
port=3000 # Port to listen on
|
||||||
|
api_key="CHANGE_ME" # API Key to use the system's API, change this!
|
||||||
|
|||||||
Reference in New Issue
Block a user