Skip to content

Commit 93dbcd7

Browse files
committed
refactor config module
1 parent 3e40925 commit 93dbcd7

File tree

15 files changed

+101
-97
lines changed

15 files changed

+101
-97
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ settings
8080
```
8181
#### Configure with environment variables
8282
```bash
83-
export APP__SERVER__PORT=8080
84-
export APP__SERVER__ADDR=127.0.0.1
83+
export PROD_APP__SERVER__PORT=8080
84+
export PROD_APP__SERVER__ADDR=127.0.0.1
8585
```
8686
#### Switching profiles
8787
Before running the application, export this variable:

docker/dev/.env

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
APP_PROFILE=dev
21
RUST_BACKTRACE=1
32
RUST_LOG=info
43
DATABASE_URL=postgres://username:password@postgres-db:5432/database_name
5-
APP__SERVER__PORT=8080
6-
APP__SERVER__ADDR=0.0.0.0
7-
APP__DB__USERNAME=username
8-
APP__DB__PASSWORD=password
9-
APP__DB__PORT=5432
10-
APP__DB__HOST=postgres-db
11-
APP__DB__DATABASE_NAME=database_name
12-
APP__REDIS__USERNAME=redis_user
13-
APP__REDIS__PASSWORD=
14-
APP__REDIS__PORT=6379
15-
APP__REDIS__HOST=redis-db
16-
APP__REDIS__DATABASE_NAME=
17-
APP__EMAIL__USERNAME=email_user
18-
APP__EMAIL__PASSWORD=email_pass
19-
APP__EMAIL__PORT=1025
20-
APP__EMAIL__HOST=mailhog
21-
APP__SECRET__PRIVATE_ACCESS_KEY=static/key/private_access_rsa_key.pem
22-
APP__SECRET__PUBLIC_ACCESS_KEY=static/key/public_access_rsa_key.pem
23-
APP__SECRET__PRIVATE_REFRESH_KEY=static/key/private_refresh_rsa_key.pem
24-
APP__SECRET__PUBLIC_REFRESH_KEY=static/key/public_refresh_rsa_key.pem
4+
DEV_APP__SERVER__PORT=8080
5+
DEV_APP__SERVER__ADDR=0.0.0.0
6+
DEV_APP__DB__USERNAME=username
7+
DEV_APP__DB__PASSWORD=password
8+
DEV_APP__DB__PORT=5432
9+
DEV_APP__DB__HOST=postgres-db
10+
DEV_APP__DB__DATABASE_NAME=database_name
11+
DEV_APPP__REDIS__USERNAME=redis_user
12+
DEV_APP__REDIS__PASSWORD=
13+
DEV_APP__REDIS__PORT=6379
14+
DEV_APP__REDIS__HOST=redis-db
15+
DEV_APP__REDIS__DATABASE_NAME=
16+
DEV_APP__EMAIL__USERNAME=email_user
17+
DEV_APP__EMAIL__PASSWORD=email_pass
18+
DEV_APP__EMAIL__PORT=1025
19+
DEV_APP__EMAIL__HOST=mailhog
20+
DEV_APP__SECRET__PRIVATE_ACCESS_KEY=static/key/private_access_rsa_key.pem
21+
DEV_APP__SECRET__PUBLIC_ACCESS_KEY=static/key/public_access_rsa_key.pem
22+
DEV_APP__SECRET__PRIVATE_REFRESH_KEY=static/key/private_refresh_rsa_key.pem
23+
DEV_APP__SECRET__PUBLIC_REFRESH_KEY=static/key/public_refresh_rsa_key.pem

docker/dev/docker-compose.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
image: "ghcr.io/robatipoor/rustfulapi:latest"
44
container_name: application
55
ports:
6-
- "${APP__SERVER__PORT}:${APP__SERVER__PORT}"
6+
- "${DEV_APP__SERVER__PORT}:${DEV_APP__SERVER__PORT}"
77
networks:
88
- server-side
99
depends_on:
@@ -19,31 +19,31 @@ services:
1919
networks:
2020
- server-side
2121
environment:
22-
- POSTGRES_USER=${APP__DB__USERNAME}
23-
- POSTGRES_PASSWORD=${APP__DB__PASSWORD}
24-
- POSTGRES_DB=${APP__DB__DATABASE_NAME}
25-
- POSTGRES_HOST=${APP__DB__HOST}
22+
- POSTGRES_USER=${DEV_APP__DB__USERNAME}
23+
- POSTGRES_PASSWORD=${DEV_APP__DB__PASSWORD}
24+
- POSTGRES_DB=${DEV_APP__DB__DATABASE_NAME}
25+
- POSTGRES_HOST=${DEV_APP__DB__HOST}
2626
ports:
27-
- "${APP__DB__PORT}:${APP__DB__PORT}"
27+
- "${DEV_APP__DB__PORT}:${DEV_APP__DB__PORT}"
2828
redis-db:
2929
image: "redis:latest"
3030
container_name: redis-database
3131
restart: always
3232
networks:
3333
- server-side
3434
environment:
35-
- REDIS_USER=${APP__REDIS__USERNAME}
36-
- REDIS_PASSWORD=${APP__REDIS__PASSWORD}
37-
- REDIS_DB=${APP__REDIS__DATABASE_NAME}
35+
- REDIS_USER=${DEV_APP__REDIS__USERNAME}
36+
- REDIS_PASSWORD=${DEV_APP__REDIS__PASSWORD}
37+
- REDIS_DB=${DEV_APP__REDIS__DATABASE_NAME}
3838
ports:
39-
- "${APP__REDIS__PORT}:${APP__REDIS__PORT}"
39+
- "${DEV_APP__REDIS__PORT}:${DEV_APP__REDIS__PORT}"
4040
mailhog:
4141
image: "mailhog/mailhog"
4242
networks:
4343
- server-side
4444
restart: always
4545
ports:
46-
- "${APP__EMAIL__PORT}:${APP__EMAIL__PORT}"
46+
- "${DEV_APP__EMAIL__PORT}:${DEV_APP__EMAIL__PORT}"
4747
- "8025:8025"
4848
pgadmin:
4949
image: dpage/pgadmin4

docker/prod/.env

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
APP_PROFILE=prod
21
RUST_BACKTRACE=1
32
RUST_LOG=info
43
DATABASE_URL=postgres://username:password@localhost:5432/database_name
5-
APP__SERVER__PORT=8080
6-
APP__SERVER__ADDR=0.0.0.0
7-
APP__DB__USERNAME=username
8-
APP__DB__PASSWORD=password
9-
APP__DB__PORT=5432
10-
APP__DB__HOST=postgres-db
11-
APP__DB__DATABASE_NAME=database_name
12-
APP__REDIS__USERNAME=redis_user
13-
APP__REDIS__PASSWORD=
14-
APP__REDIS__PORT=6379
15-
APP__REDIS__HOST=redis-db
16-
APP__REDIS__DATABASE_NAME=
17-
APP__EMAIL__USERNAME=username
18-
APP__EMAIL__PASSWORD=passowrd
19-
APP__EMAIL__PORT=587
20-
APP__EMAIL__HOST=mail.rustfulapi.com
21-
APP__SECRET__PRIVATE_ACCESS_KEY=static/key/private_access_rsa_key.pem
22-
APP__SECRET__PUBLIC_ACCESS_KEY=static/key/public_access_rsa_key.pem
23-
APP__SECRET__PRIVATE_REFRESH_KEY=static/key/private_refresh_rsa_key.pem
24-
APP__SECRET__PUBLIC_REFRESH_KEY=static/key/public_refresh_rsa_key.pem
4+
PROD_APP__SERVER__PORT=8080
5+
PROD_APP__SERVER__ADDR=0.0.0.0
6+
PROD_APP__DB__USERNAME=username
7+
PROD_APP__DB__PASSWORD=password
8+
PROD_APP__DB__PORT=5432
9+
PROD_APP__DB__HOST=postgres-db
10+
PROD_APP__DB__DATABASE_NAME=database_name
11+
PROD_APP__REDIS__USERNAME=redis_user
12+
PROD_APP__REDIS__PASSWORD=
13+
PROD_APP__REDIS__PORT=6379
14+
PROD_APP__REDIS__HOST=redis-db
15+
PROD_APP__REDIS__DATABASE_NAME=
16+
PROD_APP__EMAIL__USERNAME=username
17+
PROD_APP__EMAIL__PASSWORD=passowrd
18+
PROD_APP__EMAIL__PORT=587
19+
PROD_APP__EMAIL__HOST=mail.rustfulapi.com
20+
PROD_APP__SECRET__PRIVATE_ACCESS_KEY=static/key/private_access_rsa_key.pem
21+
PROD_APP__SECRET__PUBLIC_ACCESS_KEY=static/key/public_access_rsa_key.pem
22+
PROD_APP__SECRET__PRIVATE_REFRESH_KEY=static/key/private_refresh_rsa_key.pem
23+
PROD_APP__SECRET__PUBLIC_REFRESH_KEY=static/key/public_refresh_rsa_key.pem

docker/prod/docker-compose.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ services:
1818
networks:
1919
- server-side
2020
environment:
21-
- POSTGRES_USER=${APP__DB__USERNAME}
22-
- POSTGRES_PASSWORD=${APP__DB__PASSWORD}
23-
- POSTGRES_DB=${APP__DB__DATABASE_NAME}
24-
- POSTGRES_HOST=${APP__DB__HOST}
21+
- POSTGRES_USER=${PROD_APP__DB__USERNAME}
22+
- POSTGRES_PASSWORD=${PROD_APP__DB__PASSWORD}
23+
- POSTGRES_DB=${PROD_APP__DB__DATABASE_NAME}
24+
- POSTGRES_HOST=${PROD_APP__DB__HOST}
2525
ports:
26-
- "${APP_DB__PORT}:${APP_DB__PORT}"
26+
- "${PROD_APP_DB__PORT}:${PROD_APP_DB__PORT}"
2727
volumes:
2828
- pg_data:/var/lib/postgresql/data
2929
redis-db:
@@ -33,11 +33,11 @@ services:
3333
networks:
3434
- server-side
3535
environment:
36-
- REDIS_USER=${APP__REDIS__USERNAME}
37-
- REDIS_PASSWORD=${APP__REDIS__PASSWORD}
38-
- REDIS_DB=${APP__REDIS__DATABASE_NAME}
36+
- REDIS_USER=${PROD_APP__REDIS__USERNAME}
37+
- REDIS_PASSWORD=${PROD_APP__REDIS__PASSWORD}
38+
- REDIS_DB=${PROD_APP__REDIS__DATABASE_NAME}
3939
ports:
40-
- "${APP__REDIS__PORT}:${APP__REDIS__PORT}"
40+
- "${PROD_APP__REDIS__PORT}:${PROD_APP__REDIS__PORT}"
4141
pgadmin:
4242
image: dpage/pgadmin4
4343
container_name: pgadmin

settings/dev.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
profile = "dev"
2-
31
[server]
42
addr = "127.0.0.1"
53
port = 8_080

settings/prod.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
profile = "prod"
2-
31
[server]
42
addr = "0.0.0.0"
53
port = 8_080

settings/test.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
profile = "test"
2-
31
[server]
42
addr = "127.0.0.1"
53
port = 0

src/client/http.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::time::Duration;
2-
31
use reqwest::Response;
42
use serde::Serialize;
53

@@ -34,7 +32,7 @@ impl ClientBuilder for HttpClient {
3432
fn build_from_config(config: &AppConfig) -> AppResult<Self> {
3533
Ok(
3634
reqwest::Client::builder()
37-
.timeout(Duration::from_secs(config.http.timeout))
35+
.timeout(config.http.timeout)
3836
.build()?,
3937
)
4038
}

src/configure/deserialize.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use serde::{Deserialize, Deserializer};
2+
use std::time::Duration;
3+
4+
pub fn deserialize_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
5+
where
6+
D: Deserializer<'de>,
7+
{
8+
Ok(Duration::from_secs(u64::deserialize(deserializer)?))
9+
}

0 commit comments

Comments
 (0)