Skip to content

Commit

Permalink
feat(config): Allow the use of environment variables in the configura…
Browse files Browse the repository at this point in the history
…tion file

I also added a way of deserilizing numbers
  • Loading branch information
Yag000 committed Sep 1, 2023
1 parent 1999f96 commit fbcd171
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing-log = "0.1"
once_cell = "1"
secrecy = { version = "0.8", features = ["serde"] }
tracing-actix-web = "0.7"
serde-aux = "4"

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }
Expand Down
51 changes: 51 additions & 0 deletions spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: zero2prod
# Check https://www.digitalocean.com/docs/app-platform/#regional-availability
# for a list of all the available options.
# You can get region slugs from
# https://www.digitalocean.com/docs/platform/availability-matrix/
# They must specified lowercased.
# `fra` stands for Frankfurt (Germany - EU)
region: fra
services:
- name: zero2prod
# Relative to the repository root
dockerfile_path: Dockerfile
source_dir: .
github:
# Depending on when you created the repository,
# the default branch on GitHub might have been named `master`
branch: master
# Deploy a new version on every commit to `main`!
# Continuous Deployment, here we come!
deploy_on_push: true
# !!! Fill in with your details
# e.g. LukeMathWalker/zero-to-production
repo: Yag000/zero2prod
# Active probe used by DigitalOcean's to ensure our application is healthy
health_check:
# The path to our health check endpoint!
# It turned out to be useful in the end!
http_path: /health_check
# The port the application will be listening on for incoming requests
# It should match what we specified in our configuration/production.yaml file!
http_port: 8000
# For production workloads we'd go for at least two!
# But let's try to keep the bill under control for now...
instance_count: 1
instance_size_slug: basic-xxs
# All incoming requests should be routed to our app
routes:
- path: /

databases:
# PG = Postgres
- engine: PG
# Database name
name:
newsletter
# Again, let's keep the bill lean
num_nodes: 1
size:
db-s-dev-database
# Postgres version - using the latest here
version: "12"
14 changes: 11 additions & 3 deletions src/configurations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use config::Config;
use secrecy::{ExposeSecret, Secret};

use serde_aux::field_attributes::deserialize_number_from_string;
#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
Expand All @@ -9,15 +9,17 @@ pub struct Settings {

#[derive(serde::Deserialize)]
pub struct DatabaseSettings {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub username: String,
pub password: Secret<String>,
pub port: u16,
pub host: String,
pub database_name: String,
}

#[derive(serde::Deserialize)]
pub struct ApplicationSettings {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub host: String,
}
Expand Down Expand Up @@ -53,11 +55,17 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
.unwrap_or_else(|_| "local".into())
.try_into()
.expect("Failed to parse APP_ENVIRONMENT.");
let environment_filename = format!("{}.yaml", environment.as_str());

let settings = Config::builder()
.add_source(config::File::from(configuration_directory.join("base")).required(true))
.add_source(
config::File::from(configuration_directory.join(environment.as_str())).required(true),
config::File::from(configuration_directory.join(environment_filename)).required(true),
)
.add_source(
config::Environment::with_prefix("APP")
.prefix_separator("_")
.separator("__"),
);

match settings.build() {
Expand Down

0 comments on commit fbcd171

Please sign in to comment.