Skip to content

Commit

Permalink
[net_util, ModConfig] Add support for exporting the configuration file
Browse files Browse the repository at this point in the history
Other changes:
- [config_util] Make the `CONFIG_PATH` static public
  • Loading branch information
m4heshd committed Jul 6, 2024
1 parent 3a293f2 commit 5aa32ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/src/config_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub enum ConfigUpdate {
}

// Statics
static CONFIG_PATH: Lazy<PathBuf> =
pub static CONFIG_PATH: Lazy<PathBuf> =
Lazy::new(|| get_app_root_dir().join("config").join("config.json"));
static CONFIG: Lazy<ArcSwap<UFCRConfig>> =
Lazy::new(|| ArcSwap::from_pointee(UFCRConfig::default()));
Expand Down
25 changes: 24 additions & 1 deletion backend/src/net_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use std::{net::SocketAddr, sync::Arc};

use anyhow::{anyhow, Context};
use arc_swap::ArcSwap;
use axum::{http::Method, Router};
use axum::{
body::Body,
http::{header, Method},
response::IntoResponse,
routing::get,
Router,
};
use axum_embed::{FallbackBehavior::Redirect, ServeEmbed};
use once_cell::sync::Lazy;
use reqwest::{header::HeaderMap, Client, Proxy, Response};
Expand All @@ -15,6 +21,8 @@ use tower_http::cors::{Any, CorsLayer};

use ufcr_libs::{log_err, log_success};

use crate::config_util::CONFIG_PATH;
use crate::fs_util::read_config_file_to_string;
use crate::{
app_util::{get_app_metadata, get_os_id, is_container},
bin_util::BINS,
Expand Down Expand Up @@ -93,6 +101,7 @@ pub async fn init_server() {
// Axum router
let app = Router::new()
.nest_service("/", web_assets)
.route("/export_config", get(handle_config_dl_req))
.layer(create_ws_layer())
.layer(create_cors_layer());

Expand Down Expand Up @@ -157,6 +166,20 @@ pub fn update_proxied_client() -> anyhow::Result<()> {
Ok(())
}

/// Sends the config file as a download.
async fn handle_config_dl_req() -> impl IntoResponse {
let body = Body::from(read_config_file_to_string(&CONFIG_PATH).await);
let headers = [
(header::CONTENT_TYPE, "text/json; charset=utf-8"),
(
header::CONTENT_DISPOSITION,
"attachment; filename=\"config.json\"",
),
];

(headers, body)
}

/// Fetches UFC Ripper's update information from the GitHub repo.
pub async fn get_latest_app_meta() -> anyhow::Result<JSON> {
let req_url = format!("{}/raw/master/package.json", get_app_metadata().repo);
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/components/ModConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
Import
<input ref="configFileInput" type="file" id="config-import" accept=".json"/>
</button>
<button @click="onConfigFileExport">
<i>upload_file</i>
Export
</button>
</article>

<article class="border round mod-config__content__section">
Expand Down Expand Up @@ -533,6 +537,11 @@ async function onConfigFileImport() {
}
}
// Config export
function onConfigFileExport() {
window.location.href = '/export_config';
}
// Misc functions
function save(config) {
saveConfig(config || modConfig.data)
Expand Down

0 comments on commit 5aa32ca

Please sign in to comment.