Skip to content

Commit f4211a0

Browse files
authored
Merge pull request #25 from dorianim/chore/refactor-to-modelsrs
Chore/refactor to modelsrs
2 parents 192d9df + fb3adfa commit f4211a0

File tree

4 files changed

+146
-136
lines changed

4 files changed

+146
-136
lines changed

src/main.rs

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use axum::{http::Request, response::Response, Router};
2-
use color::Color;
3-
use serde::{Deserialize, Serialize};
42
use tower_http::catch_panic::CatchPanicLayer;
53

64
use std::sync::Arc;
@@ -10,74 +8,13 @@ use tower_http::trace::TraceLayer;
108
use tracing::Span;
119
mod color;
1210
mod routes;
11+
mod models;
1312

14-
use tokio::sync::broadcast;
15-
16-
fn default_zero() -> u32 {
17-
0
18-
}
19-
20-
#[derive(Serialize, Deserialize, Clone, Debug)]
21-
pub struct Segment {
22-
label: String,
23-
time: u32,
24-
sound: bool,
25-
color: Option<Color>,
26-
#[serde(default = "default_zero")]
27-
count_to: u32,
28-
}
29-
30-
#[derive(Serialize, Deserialize, Clone, Debug)]
31-
enum PreStartBehaviour {
32-
ShowZero,
33-
RunNormally,
34-
}
35-
36-
impl Default for PreStartBehaviour {
37-
fn default() -> Self {
38-
PreStartBehaviour::ShowZero
39-
}
40-
}
13+
use models::*;
4114

42-
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
43-
pub struct DisplayOptions {
44-
#[serde(default)]
45-
clock: bool,
46-
#[serde(default)]
47-
pre_start_behaviour: PreStartBehaviour,
48-
}
49-
50-
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
51-
pub struct Timer {
52-
// Return after TimerRequest
53-
pub segments: Vec<Segment>,
54-
pub repeat: bool,
55-
pub display_options: Option<DisplayOptions>,
56-
pub start_at: u64,
57-
pub stop_at: Option<u64>,
58-
pub password: String,
59-
pub id: String, // 5 random chars
60-
}
61-
62-
#[derive(Serialize, Clone)]
63-
#[serde(tag = "type", content = "data")]
64-
enum DonationMethod {
65-
PayPal(String),
66-
}
15+
use tokio::sync::broadcast;
6716

68-
#[derive(Serialize, Clone)]
69-
struct InstanceProperties {
70-
demo: bool,
71-
donation: Option<Vec<DonationMethod>>,
72-
}
7317

74-
type SharedState = Arc<AppState>;
75-
pub struct AppState {
76-
redis: redis::aio::ConnectionManager,
77-
jwt_key: String,
78-
instance_properties: InstanceProperties,
79-
redis_task_rx: broadcast::Receiver<Timer>,
80-
}
8118

8219
#[tokio::main]
8320
async fn main() {

src/models.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use serde::{Deserialize, Serialize};
2+
use crate::color::Color;
3+
use std::sync::Arc;
4+
use tokio::sync::broadcast;
5+
6+
//main.rs
7+
fn default_zero() -> u32 {
8+
0
9+
}
10+
11+
#[derive(Serialize, Deserialize, Clone, Debug)]
12+
pub struct Segment {
13+
label: String,
14+
time: u32,
15+
sound: bool,
16+
color: Option<Color>,
17+
#[serde(default = "default_zero")]
18+
count_to: u32,
19+
}
20+
21+
#[derive(Serialize, Deserialize, Clone, Debug)]
22+
pub enum PreStartBehaviour {
23+
ShowZero,
24+
RunNormally,
25+
}
26+
27+
impl Default for PreStartBehaviour {
28+
fn default() -> Self {
29+
PreStartBehaviour::ShowZero
30+
}
31+
}
32+
33+
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
34+
pub struct DisplayOptions {
35+
#[serde(default)]
36+
clock: bool,
37+
#[serde(default)]
38+
pre_start_behaviour: PreStartBehaviour,
39+
}
40+
41+
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
42+
pub struct Timer {
43+
// Return after TimerRequest
44+
pub segments: Vec<Segment>,
45+
pub repeat: bool,
46+
pub display_options: Option<DisplayOptions>,
47+
pub start_at: u64,
48+
pub stop_at: Option<u64>,
49+
pub password: String,
50+
pub id: String, // 5 random chars
51+
}
52+
53+
#[derive(Serialize, Clone)]
54+
#[serde(tag = "type", content = "data")]
55+
pub enum DonationMethod {
56+
PayPal(String),
57+
}
58+
59+
#[derive(Serialize, Clone)]
60+
pub struct InstanceProperties {
61+
pub demo: bool,
62+
pub donation: Option<Vec<DonationMethod>>,
63+
}
64+
65+
pub type SharedState = Arc<AppState>;
66+
pub struct AppState {
67+
pub redis: redis::aio::ConnectionManager,
68+
pub jwt_key: String,
69+
pub instance_properties: InstanceProperties,
70+
pub redis_task_rx: broadcast::Receiver<Timer>,
71+
}
72+
73+
74+
//timer.rs
75+
76+
#[derive(Serialize, Deserialize, Debug)]
77+
pub struct TimerResponse {
78+
pub segments: Vec<Segment>,
79+
pub id: String,
80+
pub repeat: bool,
81+
pub display_options: DisplayOptions,
82+
pub start_at: u64,
83+
pub stop_at: Option<u64>,
84+
}
85+
86+
impl Into<TimerResponse> for Timer {
87+
fn into(self) -> TimerResponse {
88+
TimerResponse {
89+
segments: self.segments,
90+
id: self.id,
91+
repeat: self.repeat,
92+
display_options: self.display_options.unwrap_or(DisplayOptions::default()),
93+
start_at: self.start_at,
94+
stop_at: self.stop_at,
95+
}
96+
}
97+
}
98+
99+
#[derive(Serialize, Deserialize, Debug)]
100+
pub struct TimerCreationResponse {
101+
pub timer: TimerResponse,
102+
pub token: String,
103+
}
104+
105+
#[derive(Serialize, Deserialize)]
106+
pub struct TimerCreationRequest {
107+
// Get from User
108+
pub segments: Vec<Segment>,
109+
pub id: String,
110+
pub password: String,
111+
pub repeat: bool,
112+
pub start_at: u64,
113+
}
114+
115+
#[derive(Serialize, Deserialize)]
116+
pub struct TimerUpdateRequest {
117+
// Get from User
118+
pub segments: Vec<Segment>,
119+
pub repeat: bool,
120+
pub display_options: Option<DisplayOptions>,
121+
pub start_at: u64,
122+
pub stop_at: Option<u64>,
123+
}
124+
125+
#[derive(Serialize, Deserialize)]
126+
pub struct TokenRequest {
127+
pub id: String,
128+
pub password: String,
129+
}
130+
131+
#[derive(Serialize, Deserialize, Debug)]
132+
pub struct Claims {
133+
pub id: String,
134+
pub exp: usize,
135+
pub iss: String,
136+
}
137+
138+
#[derive(Serialize, Deserialize, Debug)]
139+
pub struct TokenResponse {
140+
pub token: String,
141+
}

src/routes/timer.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::{DisplayOptions, SharedState};
2-
use crate::{Segment, Timer};
31
use axum::extract::{Path, State};
42
use axum::http::{Request, StatusCode};
53
use axum::middleware::{self, Next};
@@ -15,80 +13,14 @@ use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header,
1513
use redis::aio::ConnectionManager;
1614
use redis::AsyncCommands;
1715
use regex::Regex;
18-
use serde::{Deserialize, Serialize};
1916
use std::str;
2017

2118
use argon2::{
2219
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
2320
Argon2,
2421
};
2522

26-
#[derive(Serialize, Deserialize, Debug)]
27-
pub struct TimerResponse {
28-
pub segments: Vec<Segment>,
29-
pub id: String,
30-
pub repeat: bool,
31-
pub display_options: DisplayOptions,
32-
pub start_at: u64,
33-
pub stop_at: Option<u64>,
34-
}
35-
36-
impl Into<TimerResponse> for Timer {
37-
fn into(self) -> TimerResponse {
38-
TimerResponse {
39-
segments: self.segments,
40-
id: self.id,
41-
repeat: self.repeat,
42-
display_options: self.display_options.unwrap_or(DisplayOptions::default()),
43-
start_at: self.start_at,
44-
stop_at: self.stop_at,
45-
}
46-
}
47-
}
48-
49-
#[derive(Serialize, Deserialize, Debug)]
50-
struct TimerCreationResponse {
51-
timer: TimerResponse,
52-
token: String,
53-
}
54-
55-
#[derive(Serialize, Deserialize)]
56-
struct TimerCreationRequest {
57-
// Get from User
58-
segments: Vec<Segment>,
59-
id: String,
60-
password: String,
61-
repeat: bool,
62-
start_at: u64,
63-
}
64-
65-
#[derive(Serialize, Deserialize)]
66-
struct TimerUpdateRequest {
67-
// Get from User
68-
segments: Vec<Segment>,
69-
repeat: bool,
70-
pub display_options: Option<DisplayOptions>,
71-
start_at: u64,
72-
stop_at: Option<u64>,
73-
}
74-
75-
#[derive(Serialize, Deserialize)]
76-
struct TokenRequest {
77-
id: String,
78-
password: String,
79-
}
80-
81-
#[derive(Serialize, Deserialize, Debug)]
82-
struct Claims {
83-
id: String,
84-
exp: usize,
85-
iss: String,
86-
}
87-
88-
#[derive(Serialize, Deserialize, Debug)]
89-
struct TokenResponse {
90-
token: String,
91-
}
23+
use crate::models::*;
9224

9325
async fn auth_middleware<B>(
9426
State(state): State<SharedState>,

src/routes/ws.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::Timer;
2626

2727
use std::time::{SystemTime, UNIX_EPOCH};
2828

29-
use super::timer::TimerResponse;
29+
use crate::models::*;
3030

3131
#[derive(Serialize, Deserialize, Debug)]
3232
#[serde(tag = "type", content = "data")]

0 commit comments

Comments
 (0)