Add asset local server service

This commit is contained in:
2025-01-04 07:53:40 +08:00
parent fae080ea87
commit 7e5888e7e5
13 changed files with 146 additions and 3560 deletions

View File

@ -1,9 +1,6 @@
use actix_web::{
HttpResponse,
HttpRequest,
http::header::HeaderValue,
http::header::ContentType
};
use actix_web::{HttpResponse, HttpRequest, http::header::HeaderValue, http::header::ContentType, Responder};
use actix_files::NamedFile;
use std::path::PathBuf;
use json::{JsonValue, object};
use crate::include_file;
@ -163,6 +160,28 @@ pub fn main(req: HttpRequest) -> HttpResponse {
.body(include_file!("webui/dist/index.html"))
}
pub async fn asset(req: HttpRequest) -> impl Responder {
let args = crate::get_args();
if req.path() == "/" {
return HttpResponse::Ok()
.insert_header(ContentType::html())
.body(include_file!("webui/dist/asset_index.html"));
}
let path = req.path();
let mut modified_path = format!("{}{}", args.asset_path, path);
modified_path = modified_path.trim_end_matches('/').to_string();
let filename: PathBuf = modified_path.trim_start_matches('/').into();
match NamedFile::open(&filename) {
Ok(file) => file.into_response(&req),
Err(_) => HttpResponse::NotFound().body("File not found"),
}
}
pub fn export(req: HttpRequest) -> HttpResponse {
if !get_config()["export"].as_bool().unwrap() {
return error("Exporting accounts is disabled on this server.");