Refactor statistics page and enhance logging
- Updated the layout and styling of the statistics page for better responsiveness and visual appeal. - Introduced a new error page for 404 errors with user-friendly messaging and navigation options. - Enhanced logging functionality to capture detailed events related to asset uploads, deletions, and HTTP requests. - Implemented an AssetTracker to manage assets in memory, allowing for efficient tracking and retrieval. - Improved the API for uploading and retrieving assets, ensuring better error handling and response formatting. - Added auto-refresh functionality to the statistics page to keep data up-to-date.
This commit is contained in:
48
src/main.rs
48
src/main.rs
@@ -4,9 +4,10 @@ mod logs;
|
||||
use actix_files::NamedFile;
|
||||
|
||||
use actix_web::{
|
||||
App, HttpRequest, HttpResponse, HttpServer, get, route,
|
||||
App, HttpRequest, HttpServer, get, route,
|
||||
web::{self},
|
||||
};
|
||||
|
||||
use serde_json::Value;
|
||||
use std::{env, fs, path::PathBuf, sync::LazyLock};
|
||||
|
||||
@@ -44,38 +45,44 @@ pub static STATIC_PAGES: LazyLock<Vec<String>> = LazyLock::new(|| {
|
||||
|
||||
use crate::{
|
||||
api::{api_get_asset, api_stats, api_upload},
|
||||
logs::log_to_file,
|
||||
logs::{LogEventType, log_event},
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
async fn index(reg: HttpRequest) -> actix_web::Result<NamedFile> {
|
||||
let now = std::time::Instant::now();
|
||||
async fn index(req: HttpRequest) -> actix_web::Result<NamedFile> {
|
||||
let path: PathBuf = PathBuf::from(HTML_DIR.to_string() + "index.html");
|
||||
log_to_file(®, now);
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
Ok(NamedFile::open(path)?)
|
||||
}
|
||||
|
||||
#[get("/stats")]
|
||||
async fn stats(req: HttpRequest) -> actix_web::Result<NamedFile> {
|
||||
let path: PathBuf = PathBuf::from(HTML_DIR.to_string() + "stats.html");
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
Ok(NamedFile::open(path)?)
|
||||
}
|
||||
|
||||
#[get("/bhs/{id}")]
|
||||
async fn view_asset(req: HttpRequest) -> actix_web::Result<NamedFile> {
|
||||
let now = std::time::Instant::now();
|
||||
let path: PathBuf = PathBuf::from(HTML_DIR.to_string() + "view.html");
|
||||
log_to_file(&req, now);
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
Ok(NamedFile::open(path)?)
|
||||
}
|
||||
|
||||
#[route("/{tail:.*}", method = "GET", method = "POST")]
|
||||
async fn catch_all(req: HttpRequest, _payload: Option<web::Json<Value>>) -> actix_web::Result<HttpResponse> {
|
||||
let now = std::time::Instant::now();
|
||||
|
||||
async fn catch_all(req: HttpRequest, _payload: Option<web::Json<Value>>) -> actix_web::Result<NamedFile> {
|
||||
let response = match req.uri().path() {
|
||||
path if STATIC_PAGES.contains(&path[1..].into()) => {
|
||||
let file_path = HTML_DIR.to_string() + path;
|
||||
Ok(NamedFile::open(file_path)?.into_response(&req))
|
||||
Ok(NamedFile::open(file_path)?)
|
||||
}
|
||||
_ => {
|
||||
let file_path = PathBuf::from(HTML_DIR.to_string() + "error.html");
|
||||
Ok(NamedFile::open(file_path)?)
|
||||
}
|
||||
_ => Ok(HttpResponse::NotFound().body("Not Found")),
|
||||
};
|
||||
|
||||
log_to_file(&req, now);
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
response
|
||||
}
|
||||
|
||||
@@ -84,21 +91,26 @@ async fn main() -> std::io::Result<()> {
|
||||
let _ = fs::create_dir_all(DATA_STORAGE);
|
||||
let _ = fs::create_dir_all(LOG_DIR);
|
||||
|
||||
println!("Starting server at http://{}:{}/", *BIND_ADDR, *BIND_PORT);
|
||||
let assets = data_mgt::AssetTracker::new();
|
||||
|
||||
tokio::spawn(async {
|
||||
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(60));
|
||||
println!("Starting server at http://{}:{}/", *BIND_ADDR, *BIND_PORT);
|
||||
let assets_clone = assets.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
if let Err(e) = data_mgt::clear_assets().await {
|
||||
if let Err(e) = data_mgt::clear_assets(assets_clone.clone()).await {
|
||||
eprintln!("Error clearing assets: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
HttpServer::new(|| {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::JsonConfig::default().limit(1024 * 1024 * 3))
|
||||
.app_data(web::Data::new(assets.clone()))
|
||||
.service(index)
|
||||
.service(stats)
|
||||
.service(view_asset)
|
||||
.service(api_get_asset)
|
||||
.service(api_upload)
|
||||
|
||||
Reference in New Issue
Block a user