Make application dockerised
Update mail_app to also have different forms of configuration outside of a single base yaml file. Allows local or production configurations to be established.
This commit is contained in:
+52
-3
@@ -1,7 +1,15 @@
|
||||
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)]
|
||||
@@ -16,14 +24,55 @@ pub struct DatabaseSettings {
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
// Initialise configuration reader
|
||||
let mut settings = config::Config::default();
|
||||
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
|
||||
let configuration_directory = base_path.join("configuration");
|
||||
|
||||
// Add config from `configuration` file (yaml, json, etc.)
|
||||
settings.merge(config::File::with_name("configuration"))?;
|
||||
// Read default config file
|
||||
settings.merge(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.");
|
||||
|
||||
settings.merge(
|
||||
config::File::from(configuration_directory.join(environment.as_str())).required(true)
|
||||
)?;
|
||||
|
||||
settings.merge(config::Environment::with_prefix("app").separator("__"))?;
|
||||
|
||||
// Try convert into Settings type
|
||||
settings.try_into()
|
||||
}
|
||||
|
||||
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 nto a supported environment. Use either `local` or `production`.", other
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
|
||||
+2
-3
@@ -13,12 +13,11 @@ async fn main() -> std::io::Result<()> {
|
||||
let configuration = get_configuration().expect("Failed to read configuration data.");
|
||||
|
||||
// Configure connection to database for our startup
|
||||
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.");
|
||||
|
||||
// Take port from settings file
|
||||
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?;
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user