Skip to content

Commit

Permalink
feat: get rid of hardcoded UTC offset
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Nov 28, 2023
1 parent 8e69bbe commit 69a4ec8
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 44 deletions.
2 changes: 0 additions & 2 deletions .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ APP_BASE_URL='127.0.0.1'
PORT='8001'
SCHEMA_LOCATION='tests/schema.graphql'
DATABASE_URL='postgres://durin:SpeakFriendAndEnter@127.0.0.1:5432/tin'
# Indonesia is UTC+7
UTC_OFFSET_HOUR='7'
24 changes: 1 addition & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sqlx = { version = "0.7.3", features = ["runtime-tokio-rustls", "postgres", "uui

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

# Configurations
dotenv = "0.15.0"
Expand Down
1 change: 0 additions & 1 deletion docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ services:
- APP_ENV=dev
- APP_BASE_URL=0.0.0.0
- PORT=8001
- UTC_OFFSET_HOUR=7
- DATABASE_URL=postgres://durin:SpeakFriendAndEnter@db/tin

db:
Expand Down
6 changes: 0 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const ENV_HTTP_PORT: &str = "PORT";
const ENV_DATABASE_URL: &str = "DATABASE_URL";
const ENV_DATABASE_POOL_SIZE: &str = "DATABASE_POOL_SIZE";
const ENV_SCHEMA_LOCATION: &str = "SCHEMA_LOCATION";
const ENV_UTC_OFFSET_HOUR: &str = "UTC_OFFSET_HOUR";

const POSTGRES_SCHEME: &str = "postgres";

Expand All @@ -21,7 +20,6 @@ pub struct Config {
pub env: Env,
pub base_url: String,
pub schema_location: Option<String>,
pub utc_offset_hour: i8,
pub http: Http,
pub database: Database,
}
Expand Down Expand Up @@ -99,9 +97,6 @@ impl Config {
.parse::<Env>()?;
let base_url =
std::env::var(ENV_APP_BASE_URL).map_err(|_| env_not_found(ENV_APP_BASE_URL))?;
let utc_offset_hour =
std::env::var(ENV_UTC_OFFSET_HOUR).map_err(|_| env_not_found(ENV_UTC_OFFSET_HOUR))?;
let utc_offset_hour: i8 = utc_offset_hour.parse()?;

// GraphQL
let schema_location = match std::env::var(ENV_SCHEMA_LOCATION) {
Expand Down Expand Up @@ -133,7 +128,6 @@ impl Config {
let mut config = Self {
base_url,
schema_location,
utc_offset_hour,
env,
http,
database,
Expand Down
1 change: 0 additions & 1 deletion src/domain/meta/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ pub struct Config {
pub env: String,
pub base_url: String,
pub port: u16,
pub utc_offset_hour: i8,
}
1 change: 0 additions & 1 deletion src/domain/meta/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ pub struct Config {
pub env: String,
pub base_url: String,
pub port: u16,
pub utc_offset_hour: i8,
}
1 change: 0 additions & 1 deletion src/domain/meta/service/get_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ impl Service {
env: self.config.env.to_string(),
base_url: self.config.base_url.clone(),
port: self.config.http.port,
utc_offset_hour: self.config.utc_offset_hour,
};
let meta = entities::Meta {
build: option_env!("VCS_REVISION").unwrap_or("unknown").to_string(),
Expand Down
9 changes: 2 additions & 7 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use time::format_description::well_known::Rfc3339;
use tracing_subscriber::{
filter::{self},
fmt::{layer, time::OffsetTime},
fmt::{layer, time::ChronoLocal},
prelude::*,
registry,
};
Expand All @@ -21,13 +20,9 @@ pub fn init(config: &Config) -> Result<(), crate::Error> {
.add_directive("hyper=warn".parse()?)
.add_directive("reqwest=warn".parse()?);

let utc_offset_hour = config.utc_offset_hour;
let fmt_layer = layer()
.with_target(true)
.with_timer(OffsetTime::new(
time::UtcOffset::from_hms(utc_offset_hour, 0, 0).unwrap_or(time::UtcOffset::UTC),
Rfc3339,
))
.with_timer(ChronoLocal::rfc_3339())
.with_filter(env_filter);

registry().with(fmt_layer).init();
Expand Down
1 change: 0 additions & 1 deletion tests/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ type Config {
env: String!
baseUrl: String!
port: Int!
utcOffsetHour: Int!
}

input CreateUserInput {
Expand Down

0 comments on commit 69a4ec8

Please sign in to comment.