Skip to content

Commit 3ea0670

Browse files
committed
#41 Ran project successfully
1 parent 9d3412b commit 3ea0670

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

deploy/dev/.env

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DATABASE_URL=postgres://dgs:lmao@db/dgs
22
PORT=8000
33
ALLOWED_ORIGINS = "http://localhost:3000"
4+
MODE=DEV

deploy/prod/.env

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DATABASE_URL=postgres://dgs:lmao@db/dgs
22
PORT=8100
33
ALLOWED_ORIGINS = "https://dominux.github.io"
4+
MODE=PROD

deploy/test/.env

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DATABASE_URL=postgres://dgs:lmao@db
22
PORT=8000
33
ALLOWED_ORIGINS = "http://localhost:3000"
4+
MODE=TEST
45

server/src/app.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::Level;
99

1010
use crate::{
1111
apps::{games::routers::GamesRouter, rooms::routers::RoomsRouter, users::routers::UsersRouter},
12-
common::routing::app_state::AppState,
12+
common::{config::Mode, routing::app_state::AppState},
1313
};
1414

1515
pub fn create_app(app_state: Arc<AppState>) -> Router {
@@ -25,14 +25,21 @@ pub fn create_app(app_state: Arc<AppState>) -> Router {
2525
.collect::<Vec<_>>(),
2626
);
2727

28-
Router::new()
28+
let mode = app_state.config.mode.clone();
29+
30+
let router = Router::new()
2931
.nest("/users", UsersRouter::get_router(app_state.clone()))
3032
.nest("/games", GamesRouter::get_router(app_state.clone()))
3133
.nest("/rooms", RoomsRouter::get_router(app_state))
3234
.layer(
3335
TraceLayer::new_for_http()
3436
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
3537
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
36-
)
37-
// .layer(dgs_cors)
38+
);
39+
40+
if matches!(mode, Mode::Dev) {
41+
router.layer(dgs_cors)
42+
} else {
43+
router
44+
}
3845
}

server/src/common/config.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::{env, str::FromStr};
22

33
use super::errors::{DGSError, DGSResult};
4-
54
#[derive(Debug, Clone)]
65
pub struct Config {
76
pub db_uri: String,
87
pub port: u16,
98
pub allowed_origins: Vec<String>,
9+
pub mode: Mode,
1010
}
1111

1212
impl Config {
@@ -17,11 +17,13 @@ impl Config {
1717
.split(" ")
1818
.map(|origin| origin.to_string())
1919
.collect();
20+
let mode = Self::get_env_var::<String>("MODE")?.as_str().parse()?;
2021

2122
Ok(Self {
2223
db_uri,
2324
port,
2425
allowed_origins,
26+
mode,
2527
})
2628
}
2729

@@ -33,3 +35,23 @@ impl Config {
3335
.map_err(|_| DGSError::EnvVarParsingError(env_var.to_owned()))
3436
}
3537
}
38+
39+
#[derive(Debug, Clone)]
40+
pub enum Mode {
41+
Test,
42+
Dev,
43+
Prod,
44+
}
45+
46+
impl FromStr for Mode {
47+
type Err = DGSError;
48+
49+
fn from_str(s: &str) -> Result<Self, Self::Err> {
50+
match s.to_lowercase().as_str() {
51+
"test" => Ok(Self::Test),
52+
"dev" => Ok(Self::Dev),
53+
"prod" => Ok(Self::Prod),
54+
_ => Err(DGSError::EnvVarParsingError(s.to_string())),
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)