diff --git a/src/main.rs b/src/main.rs index 6e8d590..645a3c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ use axum::{http::Request, response::Response, Router}; -use color::Color; -use serde::{Deserialize, Serialize}; use tower_http::catch_panic::CatchPanicLayer; use std::sync::Arc; @@ -10,74 +8,13 @@ use tower_http::trace::TraceLayer; use tracing::Span; mod color; mod routes; +mod models; -use tokio::sync::broadcast; - -fn default_zero() -> u32 { - 0 -} - -#[derive(Serialize, Deserialize, Clone, Debug)] -pub struct Segment { - label: String, - time: u32, - sound: bool, - color: Option, - #[serde(default = "default_zero")] - count_to: u32, -} - -#[derive(Serialize, Deserialize, Clone, Debug)] -enum PreStartBehaviour { - ShowZero, - RunNormally, -} - -impl Default for PreStartBehaviour { - fn default() -> Self { - PreStartBehaviour::ShowZero - } -} +use models::*; -#[derive(Serialize, Deserialize, Clone, Default, Debug)] -pub struct DisplayOptions { - #[serde(default)] - clock: bool, - #[serde(default)] - pre_start_behaviour: PreStartBehaviour, -} - -#[derive(Serialize, Deserialize, Clone, Default, Debug)] -pub struct Timer { - // Return after TimerRequest - pub segments: Vec, - pub repeat: bool, - pub display_options: Option, - pub start_at: u64, - pub stop_at: Option, - pub password: String, - pub id: String, // 5 random chars -} - -#[derive(Serialize, Clone)] -#[serde(tag = "type", content = "data")] -enum DonationMethod { - PayPal(String), -} +use tokio::sync::broadcast; -#[derive(Serialize, Clone)] -struct InstanceProperties { - demo: bool, - donation: Option>, -} -type SharedState = Arc; -pub struct AppState { - redis: redis::aio::ConnectionManager, - jwt_key: String, - instance_properties: InstanceProperties, - redis_task_rx: broadcast::Receiver, -} #[tokio::main] async fn main() { diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..a46805d --- /dev/null +++ b/src/models.rs @@ -0,0 +1,141 @@ +use serde::{Deserialize, Serialize}; +use crate::color::Color; +use std::sync::Arc; +use tokio::sync::broadcast; + +//main.rs +fn default_zero() -> u32 { + 0 +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Segment { + label: String, + time: u32, + sound: bool, + color: Option, + #[serde(default = "default_zero")] + count_to: u32, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum PreStartBehaviour { + ShowZero, + RunNormally, +} + +impl Default for PreStartBehaviour { + fn default() -> Self { + PreStartBehaviour::ShowZero + } +} + +#[derive(Serialize, Deserialize, Clone, Default, Debug)] +pub struct DisplayOptions { + #[serde(default)] + clock: bool, + #[serde(default)] + pre_start_behaviour: PreStartBehaviour, +} + +#[derive(Serialize, Deserialize, Clone, Default, Debug)] +pub struct Timer { + // Return after TimerRequest + pub segments: Vec, + pub repeat: bool, + pub display_options: Option, + pub start_at: u64, + pub stop_at: Option, + pub password: String, + pub id: String, // 5 random chars +} + +#[derive(Serialize, Clone)] +#[serde(tag = "type", content = "data")] +pub enum DonationMethod { + PayPal(String), +} + +#[derive(Serialize, Clone)] +pub struct InstanceProperties { + pub demo: bool, + pub donation: Option>, +} + +pub type SharedState = Arc; +pub struct AppState { + pub redis: redis::aio::ConnectionManager, + pub jwt_key: String, + pub instance_properties: InstanceProperties, + pub redis_task_rx: broadcast::Receiver, +} + + +//timer.rs + +#[derive(Serialize, Deserialize, Debug)] +pub struct TimerResponse { + pub segments: Vec, + pub id: String, + pub repeat: bool, + pub display_options: DisplayOptions, + pub start_at: u64, + pub stop_at: Option, +} + +impl Into for Timer { + fn into(self) -> TimerResponse { + TimerResponse { + segments: self.segments, + id: self.id, + repeat: self.repeat, + display_options: self.display_options.unwrap_or(DisplayOptions::default()), + start_at: self.start_at, + stop_at: self.stop_at, + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct TimerCreationResponse { + pub timer: TimerResponse, + pub token: String, +} + +#[derive(Serialize, Deserialize)] +pub struct TimerCreationRequest { + // Get from User + pub segments: Vec, + pub id: String, + pub password: String, + pub repeat: bool, + pub start_at: u64, +} + +#[derive(Serialize, Deserialize)] +pub struct TimerUpdateRequest { + // Get from User + pub segments: Vec, + pub repeat: bool, + pub display_options: Option, + pub start_at: u64, + pub stop_at: Option, +} + +#[derive(Serialize, Deserialize)] +pub struct TokenRequest { + pub id: String, + pub password: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Claims { + pub id: String, + pub exp: usize, + pub iss: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct TokenResponse { + pub token: String, +} diff --git a/src/routes/timer.rs b/src/routes/timer.rs index 8e071a2..276a99f 100644 --- a/src/routes/timer.rs +++ b/src/routes/timer.rs @@ -1,5 +1,3 @@ -use crate::{DisplayOptions, SharedState}; -use crate::{Segment, Timer}; use axum::extract::{Path, State}; use axum::http::{Request, StatusCode}; use axum::middleware::{self, Next}; @@ -15,7 +13,6 @@ use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, use redis::aio::ConnectionManager; use redis::AsyncCommands; use regex::Regex; -use serde::{Deserialize, Serialize}; use std::str; use argon2::{ @@ -23,72 +20,7 @@ use argon2::{ Argon2, }; -#[derive(Serialize, Deserialize, Debug)] -pub struct TimerResponse { - pub segments: Vec, - pub id: String, - pub repeat: bool, - pub display_options: DisplayOptions, - pub start_at: u64, - pub stop_at: Option, -} - -impl Into for Timer { - fn into(self) -> TimerResponse { - TimerResponse { - segments: self.segments, - id: self.id, - repeat: self.repeat, - display_options: self.display_options.unwrap_or(DisplayOptions::default()), - start_at: self.start_at, - stop_at: self.stop_at, - } - } -} - -#[derive(Serialize, Deserialize, Debug)] -struct TimerCreationResponse { - timer: TimerResponse, - token: String, -} - -#[derive(Serialize, Deserialize)] -struct TimerCreationRequest { - // Get from User - segments: Vec, - id: String, - password: String, - repeat: bool, - start_at: u64, -} - -#[derive(Serialize, Deserialize)] -struct TimerUpdateRequest { - // Get from User - segments: Vec, - repeat: bool, - pub display_options: Option, - start_at: u64, - stop_at: Option, -} - -#[derive(Serialize, Deserialize)] -struct TokenRequest { - id: String, - password: String, -} - -#[derive(Serialize, Deserialize, Debug)] -struct Claims { - id: String, - exp: usize, - iss: String, -} - -#[derive(Serialize, Deserialize, Debug)] -struct TokenResponse { - token: String, -} +use crate::models::*; async fn auth_middleware( State(state): State, diff --git a/src/routes/ws.rs b/src/routes/ws.rs index 4573113..587a21d 100644 --- a/src/routes/ws.rs +++ b/src/routes/ws.rs @@ -26,7 +26,7 @@ use crate::Timer; use std::time::{SystemTime, UNIX_EPOCH}; -use super::timer::TimerResponse; +use crate::models::*; #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type", content = "data")]