Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 3 additions & 66 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Color>,
#[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<Segment>,
pub repeat: bool,
pub display_options: Option<DisplayOptions>,
pub start_at: u64,
pub stop_at: Option<u64>,
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<Vec<DonationMethod>>,
}

type SharedState = Arc<AppState>;
pub struct AppState {
redis: redis::aio::ConnectionManager,
jwt_key: String,
instance_properties: InstanceProperties,
redis_task_rx: broadcast::Receiver<Timer>,
}

#[tokio::main]
async fn main() {
Expand Down
141 changes: 141 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -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<Color>,
#[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<Segment>,
pub repeat: bool,
pub display_options: Option<DisplayOptions>,
pub start_at: u64,
pub stop_at: Option<u64>,
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<Vec<DonationMethod>>,
}

pub type SharedState = Arc<AppState>;
pub struct AppState {
pub redis: redis::aio::ConnectionManager,
pub jwt_key: String,
pub instance_properties: InstanceProperties,
pub redis_task_rx: broadcast::Receiver<Timer>,
}


//timer.rs

#[derive(Serialize, Deserialize, Debug)]
pub struct TimerResponse {
pub segments: Vec<Segment>,
pub id: String,
pub repeat: bool,
pub display_options: DisplayOptions,
pub start_at: u64,
pub stop_at: Option<u64>,
}

impl Into<TimerResponse> 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<Segment>,
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<Segment>,
pub repeat: bool,
pub display_options: Option<DisplayOptions>,
pub start_at: u64,
pub stop_at: Option<u64>,
}

#[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,
}
70 changes: 1 addition & 69 deletions src/routes/timer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -15,80 +13,14 @@ 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::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};

#[derive(Serialize, Deserialize, Debug)]
pub struct TimerResponse {
pub segments: Vec<Segment>,
pub id: String,
pub repeat: bool,
pub display_options: DisplayOptions,
pub start_at: u64,
pub stop_at: Option<u64>,
}

impl Into<TimerResponse> 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<Segment>,
id: String,
password: String,
repeat: bool,
start_at: u64,
}

#[derive(Serialize, Deserialize)]
struct TimerUpdateRequest {
// Get from User
segments: Vec<Segment>,
repeat: bool,
pub display_options: Option<DisplayOptions>,
start_at: u64,
stop_at: Option<u64>,
}

#[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<B>(
State(state): State<SharedState>,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down