From 0bc80831e745dc7ad3ddf174dca81d7b6c0dacbc Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Wed, 12 Apr 2023 05:07:37 -0700 Subject: [PATCH] moved state to its own module --- src/main.rs | 53 +++++++--------------------------------------------- src/state.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 src/state.rs diff --git a/src/main.rs b/src/main.rs index 0785db8..4d8814f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use axum::{ Router, }; -use chrono::{DateTime, Utc}; use futures::TryStreamExt; use rand::distributions::{Alphanumeric, DistString}; @@ -18,64 +17,26 @@ use rand::rngs::SmallRng; use rand::SeedableRng; use sanitize_filename_reader_friendly::sanitize; -use serde::{Deserialize, Serialize}; + +use serde::Serialize; + use tokio::io::AsyncReadExt; -use tokio::sync::Mutex; +use tokio_util::compat::FuturesAsyncWriteCompatExt; use std::collections::HashMap; use std::io; use std::net::SocketAddr; -use std::path::{Path, PathBuf}; -use std::sync::Arc; +use std::path::Path; -use tokio_util::compat::FuturesAsyncWriteCompatExt; use tokio_util::io::StreamReader; use tower_http::{limit::RequestBodyLimitLayer, services::ServeDir, trace::TraceLayer}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; -#[allow(dead_code)] -#[derive(Debug, Serialize, Deserialize)] -struct UploadRecord { - uploaded: DateTime, - file: PathBuf, - downloads: u8, - max_downloads: u8, -} +mod state; -impl UploadRecord { - fn new(file: PathBuf) -> Self { - Self { - file, - ..Default::default() - } - } -} - -impl Default for UploadRecord { - fn default() -> Self { - Self { - uploaded: Utc::now(), - file: Path::new("").to_owned(), - downloads: 0, - max_downloads: 5, - } - } -} - -#[derive(Clone)] -struct AppState { - records: Arc>>, -} - -impl AppState { - fn new(records: HashMap) -> Self { - Self { - records: Arc::new(Mutex::new(records)), - } - } -} +use state::{AppState, UploadRecord}; #[tokio::main] async fn main() -> io::Result<()> { diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..c333fbe --- /dev/null +++ b/src/state.rs @@ -0,0 +1,51 @@ +use std::{ + collections::HashMap, + path::{Path, PathBuf}, + sync::Arc, +}; + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use tokio::sync::Mutex; + +#[allow(dead_code)] +#[derive(Debug, Serialize, Deserialize)] +pub struct UploadRecord { + pub uploaded: DateTime, + pub file: PathBuf, + pub downloads: u8, + pub max_downloads: u8, +} + +impl UploadRecord { + pub fn new(file: PathBuf) -> Self { + Self { + file, + ..Default::default() + } + } +} + +impl Default for UploadRecord { + fn default() -> Self { + Self { + uploaded: Utc::now(), + file: Path::new("").to_owned(), + downloads: 0, + max_downloads: 5, + } + } +} + +#[derive(Clone)] +pub struct AppState { + pub records: Arc>>, +} + +impl AppState { + pub fn new(records: HashMap) -> Self { + Self { + records: Arc::new(Mutex::new(records)), + } + } +}