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:
194
src/api.rs
194
src/api.rs
@@ -1,11 +1,12 @@
|
||||
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
||||
use base64::{Engine, engine::general_purpose};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
DATA_STORAGE,
|
||||
logs::{log_asset_event, log_to_file},
|
||||
data_mgt::AssetTracker,
|
||||
logs::{LogEventType, log_event},
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -16,7 +17,11 @@ pub struct UploadRequest {
|
||||
}
|
||||
|
||||
#[post("/api/upload")]
|
||||
async fn api_upload(req: HttpRequest, body: web::Json<UploadRequest>) -> Result<HttpResponse, actix_web::Error> {
|
||||
async fn api_upload(
|
||||
req: HttpRequest,
|
||||
body: web::Json<UploadRequest>,
|
||||
assets: web::Data<AssetTracker>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
// Convert to bytes
|
||||
let content_bytes = if body.content_type == "text/plain" {
|
||||
body.content.as_bytes().to_vec() // UTF-8 bytes
|
||||
@@ -38,41 +43,27 @@ async fn api_upload(req: HttpRequest, body: web::Json<UploadRequest>) -> Result<
|
||||
Some(uploader_ip.clone()),
|
||||
);
|
||||
|
||||
let id = asset
|
||||
.save()
|
||||
.map_err(|e| actix_web::error::ErrorInternalServerError(format!("Failed to save asset: {}", e)))?;
|
||||
|
||||
log_asset_event(
|
||||
"upload",
|
||||
asset.id(),
|
||||
asset.mime(),
|
||||
asset.size_bytes(),
|
||||
asset.share_duration(),
|
||||
asset.created_at(),
|
||||
asset.expires_at(),
|
||||
asset.uploader_ip().unwrap_or("-"),
|
||||
);
|
||||
|
||||
log_event(LogEventType::AssetUploaded(&asset));
|
||||
let id = asset.id();
|
||||
assets.add_asset(asset).await;
|
||||
let response_body = json!({ "link": format!("/bhs/{}", id) });
|
||||
Ok(HttpResponse::Ok().json(response_body))
|
||||
}
|
||||
|
||||
#[get("/api/content/{id}")]
|
||||
async fn api_get_asset(req: HttpRequest, path: web::Path<String>) -> Result<HttpResponse, actix_web::Error> {
|
||||
let now = std::time::Instant::now();
|
||||
async fn api_get_asset(
|
||||
req: HttpRequest,
|
||||
path: web::Path<String>,
|
||||
assets: web::Data<AssetTracker>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
log_event(LogEventType::HttpRequest(&req.into()));
|
||||
|
||||
let id = path.into_inner();
|
||||
let asset_path = format!("{}{}", DATA_STORAGE, id);
|
||||
let data = std::fs::read(&asset_path).map_err(|_| actix_web::error::ErrorNotFound("Asset not found"))?;
|
||||
let asset = serde_json::from_slice::<crate::data_mgt::Asset>(&data)
|
||||
.map_err(|_| actix_web::error::ErrorInternalServerError("Failed to parse asset data"))?;
|
||||
|
||||
if asset.is_expired() {
|
||||
return Err(actix_web::error::ErrorNotFound("Asset has expired"));
|
||||
match assets.get_asset(&path.into_inner()).await {
|
||||
None => Ok(HttpResponse::NotFound().body("Asset not found")),
|
||||
Some(asset) => Ok(HttpResponse::Ok()
|
||||
.content_type(asset.mime())
|
||||
.body(asset.content().clone())),
|
||||
}
|
||||
|
||||
log_to_file(&req, now);
|
||||
Ok(HttpResponse::Ok().content_type(asset.mime()).body(asset.content()))
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
@@ -83,7 +74,6 @@ struct StatsResponse {
|
||||
storage_bytes: u64,
|
||||
image_count: usize,
|
||||
text_count: usize,
|
||||
avg_response_ms: f64,
|
||||
total_requests: usize,
|
||||
recent_activity: Vec<ActivityItem>,
|
||||
}
|
||||
@@ -92,78 +82,88 @@ struct StatsResponse {
|
||||
struct ActivityItem {
|
||||
action: String,
|
||||
mime: String,
|
||||
size_bytes: usize,
|
||||
share_duration: u32,
|
||||
timestamp: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LogEventLine {
|
||||
time: String,
|
||||
event: LogEventBody,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
enum LogEventBody {
|
||||
AssetUploaded(LogAsset),
|
||||
AssetDeleted(LogAsset),
|
||||
HttpRequest(LogHttpRequest),
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LogAsset {
|
||||
id: String,
|
||||
share_duration: u32,
|
||||
created_at: i64,
|
||||
expires_at: i64,
|
||||
mime: String,
|
||||
uploader_ip: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LogHttpRequest {
|
||||
method: String,
|
||||
path: String,
|
||||
query_string: String,
|
||||
scheme: String,
|
||||
ip: String,
|
||||
real_ip: String,
|
||||
user_agent: String,
|
||||
}
|
||||
|
||||
#[get("/api/stats")]
|
||||
async fn api_stats() -> Result<HttpResponse, actix_web::Error> {
|
||||
async fn api_stats(assets: web::Data<AssetTracker>) -> Result<HttpResponse, actix_web::Error> {
|
||||
use crate::LOG_DIR;
|
||||
use std::fs;
|
||||
|
||||
let mut active_assets = 0;
|
||||
let mut storage_bytes: u64 = 0;
|
||||
let mut image_count = 0;
|
||||
let mut text_count = 0;
|
||||
let (active_assets, storage_bytes, image_count, text_count) =
|
||||
assets.stats_summary().await;
|
||||
|
||||
// Count active assets and calculate storage
|
||||
if let Ok(entries) = fs::read_dir(DATA_STORAGE) {
|
||||
for entry in entries.flatten() {
|
||||
if let Ok(data) = fs::read(entry.path()) {
|
||||
if let Ok(asset) = serde_json::from_slice::<crate::data_mgt::Asset>(&data) {
|
||||
if !asset.is_expired() {
|
||||
active_assets += 1;
|
||||
storage_bytes += asset.size_bytes() as u64;
|
||||
if asset.mime().starts_with("image/") {
|
||||
image_count += 1;
|
||||
} else if asset.mime().starts_with("text/") {
|
||||
text_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse log for upload/delete counts, response times, and recent activity
|
||||
let mut total_uploads = 0;
|
||||
let mut total_deleted = 0;
|
||||
let mut recent_activity: Vec<ActivityItem> = Vec::new();
|
||||
let mut total_response_ms: f64 = 0.0;
|
||||
let mut request_count: usize = 0;
|
||||
|
||||
let log_path = format!("{}access.log", LOG_DIR);
|
||||
if let Ok(content) = fs::read_to_string(&log_path) {
|
||||
for line in content.lines() {
|
||||
// Parse response time from request logs
|
||||
if line.contains("dur_ms=") {
|
||||
if let Some(dur_str) = line.split("dur_ms=").nth(1) {
|
||||
if let Some(dur_val) = dur_str.split_whitespace().next() {
|
||||
if let Ok(ms) = dur_val.parse::<f64>() {
|
||||
total_response_ms += ms;
|
||||
request_count += 1;
|
||||
}
|
||||
if let Ok(entry) = serde_json::from_str::<LogEventLine>(line) {
|
||||
match entry.event {
|
||||
LogEventBody::HttpRequest(_req) => {
|
||||
request_count += 1;
|
||||
}
|
||||
LogEventBody::AssetUploaded(asset) => {
|
||||
total_uploads += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "upload".to_string(),
|
||||
mime: asset.mime,
|
||||
share_duration: asset.share_duration,
|
||||
timestamp: entry.time,
|
||||
});
|
||||
}
|
||||
LogEventBody::AssetDeleted(asset) => {
|
||||
total_deleted += 1;
|
||||
recent_activity.push(ActivityItem {
|
||||
action: "delete".to_string(),
|
||||
mime: asset.mime,
|
||||
share_duration: asset.share_duration,
|
||||
timestamp: entry.time,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if line.contains("event=asset") {
|
||||
if line.contains("action=upload") {
|
||||
total_uploads += 1;
|
||||
} else if line.contains("action=delete_expired") {
|
||||
total_deleted += 1;
|
||||
}
|
||||
|
||||
// Parse for recent activity (last 20)
|
||||
if let Some(activity) = parse_activity_line(line) {
|
||||
recent_activity.push(activity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let avg_response_ms = if request_count > 0 { total_response_ms / request_count as f64 } else { 0.0 };
|
||||
|
||||
// Keep only last 20, most recent first
|
||||
recent_activity.reverse();
|
||||
recent_activity.truncate(20);
|
||||
@@ -175,39 +175,9 @@ async fn api_stats() -> Result<HttpResponse, actix_web::Error> {
|
||||
storage_bytes,
|
||||
image_count,
|
||||
text_count,
|
||||
avg_response_ms,
|
||||
total_requests: request_count,
|
||||
recent_activity,
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok().json(response))
|
||||
}
|
||||
|
||||
fn parse_activity_line(line: &str) -> Option<ActivityItem> {
|
||||
let timestamp = line.split_whitespace().next()?.to_string();
|
||||
|
||||
let action = if line.contains("action=upload") {
|
||||
"upload".to_string()
|
||||
} else if line.contains("action=delete_expired") {
|
||||
"delete".to_string()
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let mime = line.split("mime=").nth(1)?.split_whitespace().next()?.to_string();
|
||||
|
||||
let size_bytes: usize = line
|
||||
.split("size_bytes=")
|
||||
.nth(1)?
|
||||
.split_whitespace()
|
||||
.next()?
|
||||
.parse()
|
||||
.ok()?;
|
||||
|
||||
Some(ActivityItem {
|
||||
action,
|
||||
mime,
|
||||
size_bytes,
|
||||
timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
115
src/data_mgt.rs
115
src/data_mgt.rs
@@ -1,22 +1,27 @@
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::{Duration, Utc};
|
||||
use futures::lock::Mutex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::DATA_STORAGE;
|
||||
use crate::logs::log_asset_event;
|
||||
use crate::logs::{LogEventType, log_event};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||
pub struct Asset {
|
||||
id: String,
|
||||
share_duration: u32,
|
||||
created_at: i64,
|
||||
expires_at: i64,
|
||||
mime: String,
|
||||
#[serde(skip)]
|
||||
content: Vec<u8>,
|
||||
#[serde(default)]
|
||||
uploader_ip: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Asset {
|
||||
pub fn new(share_duration: u32, mime: String, content: Vec<u8>, uploader_ip: Option<String>) -> Self {
|
||||
let id = uuid::Uuid::new_v4().to_string();
|
||||
@@ -36,8 +41,8 @@ impl Asset {
|
||||
Utc::now().timestamp_millis() > self.expires_at
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
pub fn id(&self) -> String {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
pub fn mime(&self) -> &str {
|
||||
@@ -82,29 +87,85 @@ impl Asset {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clear_assets() -> Result<()> {
|
||||
let entries = std::fs::read_dir(DATA_STORAGE)?;
|
||||
for entry in entries {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
let data = std::fs::read(&path)?;
|
||||
let asset = serde_json::from_slice::<Asset>(&data)?;
|
||||
if asset.is_expired() {
|
||||
println!("Removing expired asset: {}", asset.id());
|
||||
log_asset_event(
|
||||
"delete_expired",
|
||||
asset.id(),
|
||||
asset.mime(),
|
||||
asset.size_bytes(),
|
||||
asset.share_duration(),
|
||||
asset.created_at(),
|
||||
asset.expires_at(),
|
||||
asset.uploader_ip().unwrap_or("-"),
|
||||
);
|
||||
std::fs::remove_file(&path)?;
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct AssetTracker {
|
||||
assets: Arc<Mutex<Vec<Asset>>>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl AssetTracker {
|
||||
pub fn new() -> Self {
|
||||
AssetTracker {
|
||||
assets: Arc::new(Mutex::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_asset(&self, asset: Asset) {
|
||||
print!("[{}] Adding asset: {}", chrono::Local::now().to_rfc3339(), asset.id());
|
||||
self.assets.lock().await.push(asset);
|
||||
self.show_assets().await;
|
||||
}
|
||||
|
||||
pub async fn remove_expired(&self) {
|
||||
let mut assets = self.assets.lock().await;
|
||||
let removed_assets = assets.extract_if(.., |asset| asset.is_expired());
|
||||
for asset in removed_assets {
|
||||
log_event(LogEventType::AssetDeleted(&asset));
|
||||
println!("[{}] Removing asset: {}", chrono::Local::now().to_rfc3339(), asset.id());
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn active_assets(&self) -> usize {
|
||||
self.assets.lock().await.len()
|
||||
}
|
||||
|
||||
pub async fn stats_summary(&self) -> (usize, u64, usize, usize) {
|
||||
let assets = self.assets.lock().await;
|
||||
let mut active_assets = 0;
|
||||
let mut storage_bytes: u64 = 0;
|
||||
let mut image_count = 0;
|
||||
let mut text_count = 0;
|
||||
|
||||
for asset in assets.iter() {
|
||||
if asset.is_expired() {
|
||||
continue;
|
||||
}
|
||||
active_assets += 1;
|
||||
storage_bytes += asset.size_bytes() as u64;
|
||||
if asset.mime().starts_with("image/") {
|
||||
image_count += 1;
|
||||
} else if asset.mime().starts_with("text/") {
|
||||
text_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
(active_assets, storage_bytes, image_count, text_count)
|
||||
}
|
||||
|
||||
pub async fn show_assets(&self) {
|
||||
for asset in self.assets.lock().await.iter() {
|
||||
println!(
|
||||
"Asset ID: {}, Expires At: {}, MIME: {}, Size: {} bytes",
|
||||
asset.id(),
|
||||
asset.expires_at(),
|
||||
asset.mime(),
|
||||
asset.size_bytes()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_asset(&self, id: &str) -> Option<Asset> {
|
||||
let assets = self.assets.lock().await;
|
||||
for asset in assets.iter().cloned() {
|
||||
if asset.id() == id {
|
||||
return Some(asset.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clear_assets(assets: AssetTracker) -> Result<()> {
|
||||
assets.remove_expired().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
123
src/logs.rs
123
src/logs.rs
@@ -1,68 +1,72 @@
|
||||
use std::{fs::OpenOptions, io::Write, time::Instant};
|
||||
use std::{fs::OpenOptions, io::Write};
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::LOG_DIR;
|
||||
use crate::{LOG_DIR, data_mgt::Asset};
|
||||
|
||||
pub fn log_to_file(req: &HttpRequest, start: Instant) {
|
||||
let delta = start.elapsed().as_nanos();
|
||||
println!("Request processed in {} ns", delta);
|
||||
let duration_ms = delta as f64 / 1000_000.0;
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct LogHttpRequest {
|
||||
pub method: String,
|
||||
pub path: String,
|
||||
pub query_string: String,
|
||||
pub scheme: String,
|
||||
pub ip: String,
|
||||
pub real_ip: String,
|
||||
pub user_agent: String,
|
||||
}
|
||||
impl From<HttpRequest> for LogHttpRequest {
|
||||
fn from(req: HttpRequest) -> Self {
|
||||
let method = req.method().as_str().to_string();
|
||||
let uri = req.uri();
|
||||
let path = uri.path().to_string();
|
||||
let query_string = uri.query().unwrap_or("-").to_string();
|
||||
|
||||
let log_path = LOG_DIR.to_string() + "access.log";
|
||||
let connection_info = req.connection_info();
|
||||
let scheme = connection_info.scheme().to_string();
|
||||
let ip = connection_info.peer_addr().unwrap_or("-").to_string();
|
||||
let real_ip = connection_info.realip_remote_addr().unwrap_or("-").to_string();
|
||||
|
||||
// Ensure log directory exists
|
||||
if let Err(e) = std::fs::create_dir_all(LOG_DIR) {
|
||||
eprintln!("failed to create log dir: {}", e);
|
||||
return;
|
||||
let user_agent = req
|
||||
.headers()
|
||||
.get("user-agent")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("-")
|
||||
.to_string();
|
||||
|
||||
LogHttpRequest {
|
||||
method,
|
||||
path,
|
||||
query_string,
|
||||
scheme,
|
||||
ip,
|
||||
real_ip,
|
||||
user_agent,
|
||||
}
|
||||
}
|
||||
|
||||
let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_path) else {
|
||||
eprintln!("failed to open log file");
|
||||
return;
|
||||
};
|
||||
|
||||
let ts = chrono::Local::now().to_rfc3339();
|
||||
|
||||
let method = req.method();
|
||||
let uri = req.uri();
|
||||
let path = uri.path();
|
||||
let query = uri.query().unwrap_or("-");
|
||||
|
||||
let connection_info = req.connection_info();
|
||||
let scheme = connection_info.scheme();
|
||||
let ip = connection_info.peer_addr().unwrap_or("-");
|
||||
let real_ip = connection_info.realip_remote_addr().unwrap_or("-");
|
||||
|
||||
let ua = req
|
||||
.headers()
|
||||
.get("user-agent")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("-");
|
||||
|
||||
let line = format!(
|
||||
"{ts} scheme={scheme} ip={ip} real_ip={real_ip} method={method} path={path} qs={query} dur_ms={duration_ms} ua=\"{ua}\"\n"
|
||||
);
|
||||
|
||||
let _ = file.write_all(line.as_bytes());
|
||||
}
|
||||
|
||||
pub fn log_asset_event(
|
||||
action: &str,
|
||||
id: &str,
|
||||
mime: &str,
|
||||
size_bytes: usize,
|
||||
duration_min: u32,
|
||||
created_at_ms: i64,
|
||||
expires_at_ms: i64,
|
||||
uploader_ip: &str,
|
||||
) {
|
||||
// Ensure logging directory exists before writing
|
||||
if let Err(e) = std::fs::create_dir_all(LOG_DIR) {
|
||||
eprintln!("failed to create log dir for asset event: {}", e);
|
||||
return;
|
||||
}
|
||||
#[derive(Debug, Serialize)]
|
||||
pub enum LogEventType<'a> {
|
||||
AssetUploaded(&'a Asset),
|
||||
AssetDeleted(&'a Asset),
|
||||
HttpRequest(&'a LogHttpRequest),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct LogEvent<'a> {
|
||||
pub time: String,
|
||||
pub event: LogEventType<'a>,
|
||||
}
|
||||
|
||||
impl<'a> From<LogEventType<'a>> for LogEvent<'a> {
|
||||
fn from(event: LogEventType<'a>) -> Self {
|
||||
let time = chrono::Utc::now().to_rfc3339();
|
||||
LogEvent { time, event }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn log_event(event: LogEventType) {
|
||||
let log_path = LOG_DIR.to_string() + "access.log";
|
||||
|
||||
let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_path) else {
|
||||
@@ -70,11 +74,8 @@ pub fn log_asset_event(
|
||||
return;
|
||||
};
|
||||
|
||||
let ts = chrono::Local::now().to_rfc3339();
|
||||
let log_event: LogEvent = event.into();
|
||||
let line = serde_json::to_string(&log_event).unwrap_or_else(|e| e.to_string());
|
||||
|
||||
let line = format!(
|
||||
"{ts} event=asset action={action} id={id} mime={mime} size_bytes={size_bytes} duration_min={duration_min} created_at_ms={created_at_ms} expires_at_ms={expires_at_ms} uploader_ip={uploader_ip}\n"
|
||||
);
|
||||
|
||||
let _ = file.write_all(line.as_bytes());
|
||||
let _ = writeln!(file, "{}", line);
|
||||
}
|
||||
|
||||
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