fix: refactor logging events to use owned asset instances and simplify log event structures
This commit is contained in:
22
src/api.rs
22
src/api.rs
@@ -6,7 +6,7 @@ use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
data_mgt::AssetTracker,
|
||||
logs::{LogEventBody, LogEventLine, LogEventType, log_event},
|
||||
logs::{ LogEvent, LogEventType, log_event},
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -45,7 +45,7 @@ async fn api_upload(
|
||||
Some(uploader_ip.clone()),
|
||||
);
|
||||
|
||||
log_event(LogEventType::AssetUploaded(&asset));
|
||||
log_event(LogEventType::AssetUploaded(asset.clone()));
|
||||
let id = asset.id();
|
||||
assets.add_asset(asset).await;
|
||||
let response_body = json!({ "link": format!("/bhs/{}", id) });
|
||||
@@ -58,7 +58,7 @@ async fn api_get_asset(
|
||||
path: web::Path<String>,
|
||||
assets: web::Data<AssetTracker>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
log_event(LogEventType::HttpRequest(req.into()));
|
||||
|
||||
match assets.get_asset(&path.into_inner()).await {
|
||||
None => Ok(HttpResponse::NotFound().body("Asset not found")),
|
||||
@@ -104,26 +104,26 @@ async fn api_stats(assets: web::Data<AssetTracker>) -> Result<HttpResponse, acti
|
||||
let log_path = format!("{}access.log", LOG_DIR);
|
||||
if let Ok(content) = fs::read_to_string(&log_path) {
|
||||
for line in content.lines() {
|
||||
if let Ok(entry) = serde_json::from_str::<LogEventLine>(line) {
|
||||
if let Ok(entry) = serde_json::from_str::<LogEvent>(line) {
|
||||
match entry.event {
|
||||
LogEventBody::HttpRequest(_req) => {
|
||||
LogEventType::HttpRequest(_req) => {
|
||||
request_count += 1;
|
||||
}
|
||||
LogEventBody::AssetUploaded(asset) => {
|
||||
LogEventType::AssetUploaded(asset) => {
|
||||
total_uploads += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "upload".to_string(),
|
||||
mime: asset.mime,
|
||||
share_duration: asset.share_duration,
|
||||
mime: asset.mime(),
|
||||
share_duration: asset.share_duration(),
|
||||
timestamp: entry.time,
|
||||
});
|
||||
}
|
||||
LogEventBody::AssetDeleted(asset) => {
|
||||
LogEventType::AssetDeleted(asset) => {
|
||||
total_deleted += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "delete".to_string(),
|
||||
mime: asset.mime,
|
||||
share_duration: asset.share_duration,
|
||||
mime: asset.mime(),
|
||||
share_duration: asset.share_duration(),
|
||||
timestamp: entry.time,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user