build: make project deployable via docker/podman

This commit is contained in:
Sandro Eiler 2024-02-07 12:07:03 +01:00
parent 2ae860f176
commit 9dde52c1cd
7 changed files with 102 additions and 109 deletions

View file

@ -1,17 +1,27 @@
use secrecy::{ExposeSecret, Secret};
#[derive(serde::Deserialize)]
/// The application's settings
/// The setting collection.
///
/// * `database`: database settings
/// * `application_port`: the port the app is running on
/// * `application`: application settings
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
pub application: ApplicationSettings,
}
#[derive(serde::Deserialize)]
/// The database settings
/// The application settings.
///
/// * `port`: The port to listen on
/// * `host`: The host address to listen on
pub struct ApplicationSettings {
pub port: u16,
pub host: String,
}
#[derive(serde::Deserialize)]
/// The database settings.
///
/// * `username`: the DB username
/// * `password`: the DB pasword
@ -28,14 +38,58 @@ pub struct DatabaseSettings {
pub require_ssl: bool,
}
/// Provides the application settings
/// 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
)),
}
}
}
/// Provides the application settings.
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
let configuration_directory = base_path.join("configuration");
// Detect the running environment.
// Default to `local` if unspecified.
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(
configuration_directory.join("base.yaml"),
))
.add_source(config::File::from(
configuration_directory.join(environment_filename),
))
.build()?;
settings.try_deserialize::<Settings>()
}

View file

@ -2,7 +2,7 @@ use learn_axum::configuration::get_configuration;
use learn_axum::startup;
use learn_axum::telemetry::{get_subscriber, init_subscriber};
use secrecy::ExposeSecret;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use tokio::net::TcpListener;
#[tokio::main]
@ -13,11 +13,14 @@ async fn main() {
init_subscriber(subscriber);
let configuration = get_configuration().expect("Failed to read configuration.");
let addr = format!("127.0.0.1:{}", configuration.application_port);
let addr = format!(
"{}:{}",
configuration.application.host, configuration.application.port
);
let listener = TcpListener::bind(addr).await.unwrap(); //.expect("Unable to bind to port");
let connection_pool =
PgPool::connect(configuration.database.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");
let connection_pool = PgPoolOptions::new()
.acquire_timeout(std::time::Duration::from_secs(2))
.connect_lazy(configuration.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");
startup::run(listener, connection_pool).await.unwrap();
}