simplify proxy and update readme
This commit is contained in:
+2
-37
@@ -1,8 +1,6 @@
|
||||
use color_eyre::eyre::{anyhow, bail, Context, Result};
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
use mumble_web2_common::GuiConfig;
|
||||
use once_cell::sync::OnceCell;
|
||||
use rcgen::date_time_ymd;
|
||||
use salvo::conn::rustls::{Keycert, RustlsConfig};
|
||||
use salvo::cors::{AllowOrigin, Cors};
|
||||
use salvo::logging::Logger;
|
||||
@@ -47,40 +45,6 @@ struct Config {
|
||||
|
||||
static CONFIG: OnceCell<Config> = OnceCell::new();
|
||||
|
||||
#[handler]
|
||||
#[instrument]
|
||||
async fn serve_gui_index_html(req: &Request, res: &mut Response) {
|
||||
let config = CONFIG.get().unwrap();
|
||||
|
||||
let path = match &config.gui_path {
|
||||
Some(p) => p.join("index.html"),
|
||||
None => {
|
||||
res.status_code(StatusCode::NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Load the HTML file
|
||||
let html = match fs::read_to_string(&path).await {
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
error!("could not load {}: {:?}", path.display(), err);
|
||||
res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Insert the script tag with configuration
|
||||
let modified_html = html.replace(
|
||||
"</head>",
|
||||
&format!(
|
||||
"<script>window.config = {}</script>\n</head>",
|
||||
serde_json::to_string(&config.gui).unwrap(),
|
||||
),
|
||||
);
|
||||
res.render(Text::Html(modified_html));
|
||||
}
|
||||
|
||||
fn init_config() -> Result<()> {
|
||||
let mut config: Config = toml::from_str(
|
||||
&std::fs::read_to_string("./config.toml")
|
||||
@@ -160,11 +124,12 @@ async fn main() -> Result<()> {
|
||||
|
||||
// Server routing
|
||||
let mut router = Router::new()
|
||||
.get(serve_gui_index_html)
|
||||
.push(Router::with_path("/proxy").goal(connect_proxy))
|
||||
.push(Router::with_path("/config").get(config_craft.get_config()))
|
||||
.hoop(Logger::new());
|
||||
if let Some(gui_path) = config.gui_path.clone() {
|
||||
router =
|
||||
router.push(Router::with_path("/").get(StaticFile::new(gui_path.join("index.html"))));
|
||||
router = router.push(Router::with_path("/<*+rest>").get(StaticDir::new(gui_path)));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user