build pipeline, health check works again
This commit is contained in:
17
.sqlx/query-eab54567b7111647243528e961fdb11a68a6d4d3b689d75140a4c74d8b9f59e4.json
generated
Normal file
17
.sqlx/query-eab54567b7111647243528e961fdb11a68a6d4d3b689d75140a4c74d8b9f59e4.json
generated
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tINSERT INTO subscriptions (id, email, name, subscribed_at)\n\t\tVALUES ($1, $2, $3, $4)\n\t\t",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text",
|
||||
"Text",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "eab54567b7111647243528e961fdb11a68a6d4d3b689d75140a4c74d8b9f59e4"
|
||||
}
|
||||
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -0,0 +1,11 @@
|
||||
FROM rust:1.76.0
|
||||
|
||||
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"]
|
||||
@ -1,4 +1,5 @@
|
||||
application_port: 8000
|
||||
application:
|
||||
port: 8000
|
||||
database:
|
||||
host: "127.0.0.1"
|
||||
port: 5432
|
||||
2
configuration/development.yaml
Normal file
2
configuration/development.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
|
||||
@ -3,26 +3,32 @@ use secrecy::{ExposeSecret, Secret};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
pub application_port: u16,
|
||||
pub database: DatabaseSettings,
|
||||
pub application: ApplicationSettings,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct ApplicationSettings {
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: Secret<String>,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
pub username: String,
|
||||
pub password: Secret<String>,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password.expose_secret(), self.host, self.port, self.database_name
|
||||
))
|
||||
}
|
||||
pub fn connection_string(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password.expose_secret(), self.host, self.port, self.database_name
|
||||
))
|
||||
}
|
||||
|
||||
pub fn connection_string_without_db(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
@ -33,9 +39,45 @@ impl DatabaseSettings {
|
||||
}
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
let settings = Config::builder()
|
||||
.add_source(config::File::with_name("configuration"))
|
||||
.build()?;
|
||||
let run_mode = std::env::var("APP_ENVIRONMENT").unwrap_or("development".into());
|
||||
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
|
||||
let configuration_directory = base_path.join("configuration"); // "./configuration"
|
||||
|
||||
settings.try_deserialize()
|
||||
let base_config_file = configuration_directory.join("base"); // "./configuration/base"
|
||||
let base_config_file = base_config_file.to_str().expect("Invalid base config file path");
|
||||
let environment_config_file = configuration_directory.join(run_mode.to_lowercase()); // "./configuration/[development/production]"
|
||||
let environment_config_file = environment_config_file.to_str().expect("Invalid environment config file path");
|
||||
|
||||
let settings = Config::builder()
|
||||
.add_source(config::File::with_name(base_config_file).required(true))
|
||||
.add_source(config::File::with_name(environment_config_file).required(true))
|
||||
.build()?;
|
||||
|
||||
settings.try_deserialize()
|
||||
}
|
||||
|
||||
pub enum Environment {
|
||||
Development,
|
||||
Production,
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Environment::Development => "development",
|
||||
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() {
|
||||
"development" => Ok(Environment::Development),
|
||||
"production" => Ok(Environment::Production),
|
||||
_ => Err(format!("{} is not a valid environment", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -12,9 +12,9 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection_pool = PgPoolOptions::new()
|
||||
.max_connections(10).connect(config.database.connection_string().expose_secret()).await.expect("Failed to connect to Postgres.");
|
||||
.max_connections(10).connect_lazy(config.database.connection_string().expose_secret()).expect("Failed to connect to Postgres.");
|
||||
|
||||
let address = format!("127.0.0.1:{}", config.application_port);
|
||||
let address = format!("{}:{}", config.application.host, config.application.port);
|
||||
let listener = TcpListener::bind(address).expect("Failed to bind random port");
|
||||
run(listener, connection_pool)?.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user