moved state to its own module

main
Zynh0722 2023-04-12 05:07:37 -07:00
parent 47a51c5c15
commit 0bc80831e7
2 changed files with 58 additions and 46 deletions

View File

@ -10,7 +10,6 @@ use axum::{
Router, Router,
}; };
use chrono::{DateTime, Utc};
use futures::TryStreamExt; use futures::TryStreamExt;
use rand::distributions::{Alphanumeric, DistString}; use rand::distributions::{Alphanumeric, DistString};
@ -18,64 +17,26 @@ use rand::rngs::SmallRng;
use rand::SeedableRng; use rand::SeedableRng;
use sanitize_filename_reader_friendly::sanitize; use sanitize_filename_reader_friendly::sanitize;
use serde::{Deserialize, Serialize};
use serde::Serialize;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio::sync::Mutex; use tokio_util::compat::FuturesAsyncWriteCompatExt;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::{Path, PathBuf}; use std::path::Path;
use std::sync::Arc;
use tokio_util::compat::FuturesAsyncWriteCompatExt;
use tokio_util::io::StreamReader; use tokio_util::io::StreamReader;
use tower_http::{limit::RequestBodyLimitLayer, services::ServeDir, trace::TraceLayer}; use tower_http::{limit::RequestBodyLimitLayer, services::ServeDir, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[allow(dead_code)] mod state;
#[derive(Debug, Serialize, Deserialize)]
struct UploadRecord {
uploaded: DateTime<Utc>,
file: PathBuf,
downloads: u8,
max_downloads: u8,
}
impl UploadRecord { use state::{AppState, 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<Mutex<HashMap<String, UploadRecord>>>,
}
impl AppState {
fn new(records: HashMap<String, UploadRecord>) -> Self {
Self {
records: Arc::new(Mutex::new(records)),
}
}
}
#[tokio::main] #[tokio::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {

51
src/state.rs Normal file
View File

@ -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<Utc>,
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<Mutex<HashMap<String, UploadRecord>>>,
}
impl AppState {
pub fn new(records: HashMap<String, UploadRecord>) -> Self {
Self {
records: Arc::new(Mutex::new(records)),
}
}
}