Docker for deployment
This commit is contained in:
@@ -2,19 +2,61 @@ use secrecy::{ExposeSecret, SecretString};
|
||||
use serde::Deserialize;
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
|
||||
let config_dir = base_path.join("configuration");
|
||||
let environment: Environment = std::env::var("APP_ENVIRONMENT")
|
||||
.unwrap_or_else(|_| "local".into())
|
||||
.try_into()
|
||||
.expect("Failed to parse APP_ENVIRONMENT");
|
||||
let environment_filename = format!("{}.yaml", environment.as_str());
|
||||
|
||||
let settings = config::Config::builder()
|
||||
.add_source(config::File::new(
|
||||
"configuration.yaml",
|
||||
config::FileFormat::Yaml,
|
||||
))
|
||||
.add_source(config::File::from(config_dir.join("base.yaml")))
|
||||
.add_source(config::File::from(config_dir.join(environment_filename)))
|
||||
.build()?;
|
||||
|
||||
settings.try_deserialize::<Settings>()
|
||||
}
|
||||
|
||||
pub enum Environment {
|
||||
Local,
|
||||
Production,
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Environment::Local => "local",
|
||||
Environment::Production => "production",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for Environment {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
match value.to_lowercase().as_str() {
|
||||
"local" => Ok(Environment::Local),
|
||||
"production" => Ok(Environment::Production),
|
||||
other => Err(format!(
|
||||
"{} is not a supported environment. Use either `local` or `production`.",
|
||||
other
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Settings {
|
||||
pub application: ApplicationSettings,
|
||||
pub database: DatabaseSettings,
|
||||
pub application_port: u16,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ApplicationSettings {
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@@ -7,14 +7,15 @@ use zero2prod::{configuration::get_configuration, startup::run, telemetry::init_
|
||||
async fn main() {
|
||||
init_subscriber(std::io::stdout);
|
||||
let configuration = get_configuration().expect("Failed to read configuration");
|
||||
let listener = TcpListener::bind(format!("127.0.0.1:{}", configuration.application_port))
|
||||
.await
|
||||
.unwrap();
|
||||
let listener = TcpListener::bind(format!(
|
||||
"{}:{}",
|
||||
configuration.application.host, configuration.application.port
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
let connection_pool =
|
||||
PgPool::connect(configuration.database.connection_string().expose_secret())
|
||||
.await
|
||||
.unwrap();
|
||||
PgPool::connect_lazy(configuration.database.connection_string().expose_secret()).unwrap();
|
||||
|
||||
run(listener, connection_pool).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user