Add injected config file
This commit is contained in:
+31
-9
@@ -1,7 +1,11 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::http::{Response, StatusCode};
|
||||||
|
use axum::response::IntoResponse;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::future::IntoFuture;
|
use std::future::IntoFuture;
|
||||||
use std::net::{SocketAddr, ToSocketAddrs};
|
use std::net::{SocketAddr, ToSocketAddrs};
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
@@ -83,13 +87,14 @@ impl ServerCertVerifier for NoCertificateVerification {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct Config {
|
struct Config {
|
||||||
proxy_listen_address: String,
|
proxy_listen_address: String,
|
||||||
http_listen_address: String,
|
http_listen_address: String,
|
||||||
cert_path: String,
|
cert_path: String,
|
||||||
key_path: String,
|
key_path: String,
|
||||||
mumble_server_address: SocketAddr,
|
mumble_server_address: SocketAddr,
|
||||||
gui_path: String,
|
gui_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
@@ -106,13 +111,32 @@ impl Config {
|
|||||||
.to_socket_addrs()?
|
.to_socket_addrs()?
|
||||||
.next()
|
.next()
|
||||||
.ok_or(anyhow::anyhow!("Invalid mumble server address"))?,
|
.ok_or(anyhow::anyhow!("Invalid mumble server address"))?,
|
||||||
gui_path: env::var("MUMBLE_WEBTRANSPORT_GUI_PATH").unwrap_or("gui".to_string()),
|
gui_path: PathBuf::from(
|
||||||
|
env::var("MUMBLE_WEBTRANSPORT_GUI_PATH").unwrap_or("gui".to_string()),
|
||||||
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn redirect() -> axum::response::Redirect {
|
//async fn serve_index_html_with_config(State(config): State<Config>) -> impl IntoResponse {
|
||||||
axum::response::Redirect::to("/static/index.html")
|
async fn serve_index_html_with_config(State(config): State<Config>) -> impl IntoResponse {
|
||||||
|
// Load the HTML file
|
||||||
|
let html = match tokio::fs::read_to_string(config.gui_path.join("index.html")).await {
|
||||||
|
Ok(content) => content,
|
||||||
|
Err(_) => return (StatusCode::NOT_FOUND, "File not found").into_response(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Insert the script tag with configuration
|
||||||
|
let config_script = "<script>window.config = {\"foo\": \"bar\"}</script>";
|
||||||
|
let modified_html = html.replace("</head>", &format!("{}\n</head>", config_script));
|
||||||
|
|
||||||
|
// Create a response with the modified HTML
|
||||||
|
Response::builder()
|
||||||
|
.status(StatusCode::OK)
|
||||||
|
.header("Content-Type", "text/html")
|
||||||
|
.body(modified_html)
|
||||||
|
.unwrap()
|
||||||
|
.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -124,11 +148,9 @@ async fn main() -> Result<()> {
|
|||||||
// Setup HTTP Server
|
// Setup HTTP Server
|
||||||
//let http = axum::Router::new().route("/", axum::routing::get(serve_gui));
|
//let http = axum::Router::new().route("/", axum::routing::get(serve_gui));
|
||||||
let app = axum::Router::new()
|
let app = axum::Router::new()
|
||||||
.nest_service(
|
.route("/", axum::routing::get(serve_index_html_with_config))
|
||||||
"/gui",
|
.fallback_service(tower_http::services::ServeDir::new(&proxy_config.gui_path))
|
||||||
tower_http::services::ServeDir::new(proxy_config.gui_path),
|
.with_state(proxy_config.clone());
|
||||||
)
|
|
||||||
.route("/", axum::routing::get(redirect));
|
|
||||||
let listener = tokio::net::TcpListener::bind(proxy_config.http_listen_address)
|
let listener = tokio::net::TcpListener::bind(proxy_config.http_listen_address)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user