Skip to content

Commit

Permalink
5.3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
josemoura212 committed Jun 9, 2024
1 parent d5c9581 commit 3e014fd
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/target

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

8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM rust:latest
WORKDIR /app
RUN apt update && apt install lld clang -y
COPY . .
ENV SQLX_OFFLINE true
RUN cargo build --release
ENV APP_ENVIRONMENT production
ENTRYPOINT ["./target/release/zero2prod"]
3 changes: 2 additions & 1 deletion configuration.yaml → configuration/base.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
application_port: 8000
application:
port: 8000
database:
host: "localhost"
port: 5432
Expand Down
2 changes: 2 additions & 0 deletions configuration/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
application:
host: 127.0.0.1
2 changes: 2 additions & 0 deletions configuration/production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
application:
host: 0.0.0.0
68 changes: 56 additions & 12 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use secrecy::{ExposeSecret, Secret};
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};

#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
pub application: ApplicationSettings,
}

#[derive(serde::Deserialize)]
pub struct ApplicationSettings {
pub port: u16,
pub host: String,
}

#[derive(serde::Deserialize)]
Expand Down Expand Up @@ -44,27 +50,65 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {

fn try_from(config: config::Config) -> Result<Self, Self::Error> {
let database = config.get::<DatabaseSettings>("database")?;
let application_port = config.get::<u16>("application_port")?;
let application = config.get::<ApplicationSettings>("application")?;

Ok(Settings {
database,
application_port,
application,
})
}
}

let mut settings = config::Config::builder();

// Add configuration values from a file named `configuration`.
// It will look for any top-level file with an extension
// that `config` knows how to parse: yaml, json, etc.
settings = settings.add_source(config::File::with_name("configuration"));
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
let configuration_directory = base_path.join("configuration");

// Read the "default" configuration file
settings = settings
.add_source(config::File::from(configuration_directory.join("base")).required(true));

let environment: Environment = std::env::var("APP_ENVIRONMENT")
.unwrap_or_else(|_| "local".into())
.try_into()
.expect("Failed to parse APP_ENVIRONMENT.");

// Layer on the environment-specific values.
settings = settings.add_source(
config::File::from(configuration_directory.join(environment.as_str())).required(true),
);

// Build the configuration and unwrap it, handling any errors
let settings = settings.build()?;

// Try to convert the configuration values you read into
// our Settings type
// nosso tipo Settings
settings.try_into()
}

/// The possible runtime environment for our application.
pub enum Environment {
Local,
Production,
}

impl Environment {
pub fn as_str(&self) -> &'static str {
match self {
Environment::Local => "local",
Environment::Production => "production",
}
}
}

impl TryFrom<String> for Environment {
type Error = String;

fn try_from(s: String) -> Result<Self, Self::Error> {
match s.to_lowercase().as_str() {
"local" => Ok(Self::Local),
"production" => Ok(Self::Production),
other => Err(format!(
"{} is not a supported environment. Use either `local` or `production`.",
other
)),
}
}
}
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ async fn main() -> std::io::Result<()> {

let configuration = get_configuration().expect("Failed to read configuration.");

let connection_pool = PgPool::connect(&configuration.database.connection_string())
.await
let connection_pool = PgPool::connect_lazy(&configuration.database.connection_string())
.expect("Failed to connect to Postgres.");

let address = format!("127.0.0.1:{}", configuration.application_port);
let address = format!(
"{}:{}",
configuration.application.host, configuration.application.port
);
let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await
}

0 comments on commit 3e014fd

Please sign in to comment.