Skip to content

Commit 69a4ec8

Browse files
committed
feat: get rid of hardcoded UTC offset
Thanks to the new `chrono` support in `tracing-subscriber`. - https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/time/struct.ChronoLocal.html - tokio-rs/tracing#2690
1 parent 8e69bbe commit 69a4ec8

File tree

10 files changed

+4
-44
lines changed

10 files changed

+4
-44
lines changed

.example.env

-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ APP_BASE_URL='127.0.0.1'
55
PORT='8001'
66
SCHEMA_LOCATION='tests/schema.graphql'
77
DATABASE_URL='postgres://durin:[email protected]:5432/tin'
8-
# Indonesia is UTC+7
9-
UTC_OFFSET_HOUR='7'

Cargo.lock

+1-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sqlx = { version = "0.7.3", features = ["runtime-tokio-rustls", "postgres", "uui
3838

3939
# Logging
4040
tracing = "0.1.40"
41-
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "time", "local-time"] }
41+
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] }
4242

4343
# Configurations
4444
dotenv = "0.15.0"

docker-compose.local.yml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ services:
1414
- APP_ENV=dev
1515
- APP_BASE_URL=0.0.0.0
1616
- PORT=8001
17-
- UTC_OFFSET_HOUR=7
1817
- DATABASE_URL=postgres://durin:SpeakFriendAndEnter@db/tin
1918

2019
db:

src/config.rs

-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const ENV_HTTP_PORT: &str = "PORT";
1212
const ENV_DATABASE_URL: &str = "DATABASE_URL";
1313
const ENV_DATABASE_POOL_SIZE: &str = "DATABASE_POOL_SIZE";
1414
const ENV_SCHEMA_LOCATION: &str = "SCHEMA_LOCATION";
15-
const ENV_UTC_OFFSET_HOUR: &str = "UTC_OFFSET_HOUR";
1615

1716
const POSTGRES_SCHEME: &str = "postgres";
1817

@@ -21,7 +20,6 @@ pub struct Config {
2120
pub env: Env,
2221
pub base_url: String,
2322
pub schema_location: Option<String>,
24-
pub utc_offset_hour: i8,
2523
pub http: Http,
2624
pub database: Database,
2725
}
@@ -99,9 +97,6 @@ impl Config {
9997
.parse::<Env>()?;
10098
let base_url =
10199
std::env::var(ENV_APP_BASE_URL).map_err(|_| env_not_found(ENV_APP_BASE_URL))?;
102-
let utc_offset_hour =
103-
std::env::var(ENV_UTC_OFFSET_HOUR).map_err(|_| env_not_found(ENV_UTC_OFFSET_HOUR))?;
104-
let utc_offset_hour: i8 = utc_offset_hour.parse()?;
105100

106101
// GraphQL
107102
let schema_location = match std::env::var(ENV_SCHEMA_LOCATION) {
@@ -133,7 +128,6 @@ impl Config {
133128
let mut config = Self {
134129
base_url,
135130
schema_location,
136-
utc_offset_hour,
137131
env,
138132
http,
139133
database,

src/domain/meta/entities.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ pub struct Config {
1212
pub env: String,
1313
pub base_url: String,
1414
pub port: u16,
15-
pub utc_offset_hour: i8,
1615
}

src/domain/meta/model/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ pub struct Config {
1313
pub env: String,
1414
pub base_url: String,
1515
pub port: u16,
16-
pub utc_offset_hour: i8,
1716
}

src/domain/meta/service/get_meta.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ impl Service {
77
env: self.config.env.to_string(),
88
base_url: self.config.base_url.clone(),
99
port: self.config.http.port,
10-
utc_offset_hour: self.config.utc_offset_hour,
1110
};
1211
let meta = entities::Meta {
1312
build: option_env!("VCS_REVISION").unwrap_or("unknown").to_string(),

src/logger.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use time::format_description::well_known::Rfc3339;
21
use tracing_subscriber::{
32
filter::{self},
4-
fmt::{layer, time::OffsetTime},
3+
fmt::{layer, time::ChronoLocal},
54
prelude::*,
65
registry,
76
};
@@ -21,13 +20,9 @@ pub fn init(config: &Config) -> Result<(), crate::Error> {
2120
.add_directive("hyper=warn".parse()?)
2221
.add_directive("reqwest=warn".parse()?);
2322

24-
let utc_offset_hour = config.utc_offset_hour;
2523
let fmt_layer = layer()
2624
.with_target(true)
27-
.with_timer(OffsetTime::new(
28-
time::UtcOffset::from_hms(utc_offset_hour, 0, 0).unwrap_or(time::UtcOffset::UTC),
29-
Rfc3339,
30-
))
25+
.with_timer(ChronoLocal::rfc_3339())
3126
.with_filter(env_filter);
3227

3328
registry().with(fmt_layer).init();

tests/schema.graphql

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ type Config {
33
env: String!
44
baseUrl: String!
55
port: Int!
6-
utcOffsetHour: Int!
76
}
87

98
input CreateUserInput {

0 commit comments

Comments
 (0)