moved state to its own module

This commit is contained in:
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,
};
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<Utc>,
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<Mutex<HashMap<String, UploadRecord>>>,
}
impl AppState {
fn new(records: HashMap<String, UploadRecord>) -> Self {
Self {
records: Arc::new(Mutex::new(records)),
}
}
}
use state::{AppState, UploadRecord};
#[tokio::main]
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)),
}
}
}