use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::path::PathBuf;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use tokio::sync::{broadcast, oneshot, Notify};
use crate::error::FsvError;
pub struct Config {
pub path: PathBuf,
pub port: u16,
}
pub struct Server {
pub ips: Vec<IpAddr>,
pub port: u16,
pub(crate) shutdown_tx: Option<oneshot::Sender<()>>,
pub(crate) ws_tx: broadcast::Sender<String>,
pub shutdown_notify: Arc<Notify>,
}
impl Server {
pub fn shutdown(&mut self) -> Result<(), FsvError> {
self.shutdown_tx
.take()
.ok_or_else(|| {
FsvError::Shutdown("Server already shut down or handle uninitialized".into())
})?
.send(())
.map_err(|_| FsvError::Shutdown("Failed to send shutdown signal".into()))
}
pub fn send(&self, message: &str) -> Result<usize, FsvError> {
match self.ws_tx.send(message.to_string()) {
Ok(count) => Ok(count),
Err(_) => Ok(0),
}
}
}
#[derive(Clone)]
pub struct AppState {
pub root_path: PathBuf,
pub ws_tx: broadcast::Sender<String>,
pub ws_connections: Arc<AtomicUsize>,
pub shutdown_notify: Arc<Notify>,
}
#[derive(Serialize)]
pub struct FileInfo {
pub name: String,
pub path: String,
pub is_dir: bool,
pub size: u64,
pub modified: Option<u64>,
}
#[derive(Deserialize, Default)]
pub struct FileParams {
pub path: Option<String>,
}