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:
parent
1f06f5e66f
commit
c9e564d48f
2
.Dockerignore
Normal file
2
.Dockerignore
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
.vscode
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
/target
|
||||
.vscode
|
||||
.vscode
|
||||
.env
|
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -547,6 +547,9 @@ name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
dependencies = [
|
||||
"serde 1.0.130",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
@ -1631,6 +1634,7 @@ version = "1.0.68"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde 1.0.130",
|
||||
@ -1810,9 +1814,12 @@ dependencies = [
|
||||
"dotenv",
|
||||
"either",
|
||||
"heck",
|
||||
"hex",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde 1.0.130",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"sqlx-core",
|
||||
"sqlx-rt",
|
||||
|
@ -29,7 +29,8 @@ features = [
|
||||
"postgres",
|
||||
"uuid",
|
||||
"chrono",
|
||||
"migrate"
|
||||
"migrate",
|
||||
"offline"
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
|
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -0,0 +1,11 @@
|
||||
FROM rust:1.56
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
ENV SQLX_OFFLINE true
|
||||
RUN CARGO BUILD --release
|
||||
|
||||
ENV APP_ENVIRONMENT production
|
||||
|
||||
ENTRYPOINT ["./target/release/mail_app"]
|
@ -1,4 +1,5 @@
|
||||
application_port: 8000
|
||||
application:
|
||||
port: 8000
|
||||
database:
|
||||
host: "localhost"
|
||||
port: 5432
|
2
configuration/local.yaml
Normal file
2
configuration/local.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
application:
|
||||
host: 127.0.0.1
|
2
configuration/production.yaml
Normal file
2
configuration/production.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
application:
|
||||
host: 0.0.0.0
|
18
sqlx-data.json
Normal file
18
sqlx-data.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"db": "PostgreSQL",
|
||||
"5db1f9dfcdee685b02851f144e29d3e2726127d5a1d614880a0be89d8fa6904b": {
|
||||
"query": "\n INSERT INTO subscriptions (id, email, name, subscribed_at)\n VALUES($1, $2, $3, $4)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text",
|
||||
"Text",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
}
|
||||
}
|
@ -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!(
|
||||
|
@ -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(())
|
||||
|
Loading…
Reference in New Issue
Block a user