Docker for deployment
This commit is contained in:
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/target
|
||||||
|
.env
|
||||||
|
/tests
|
||||||
|
Dockerfile
|
||||||
|
/scripts
|
||||||
|
/migrations
|
||||||
25
Dockerfile
Normal file
25
Dockerfile
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
FROM lukemathwalker/cargo-chef:latest-rust-1.89.0 AS chef
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
FROM chef AS planner
|
||||||
|
COPY . .
|
||||||
|
RUN cargo chef prepare --recipe-path recipe.json
|
||||||
|
|
||||||
|
FROM chef AS builder
|
||||||
|
COPY --from=planner /app/recipe.json recipe.json
|
||||||
|
RUN cargo chef cook --release --recipe-path recipe.json
|
||||||
|
COPY . .
|
||||||
|
ENV SQLX_OFFLINE=true
|
||||||
|
RUN cargo build --release --bin zero2prod
|
||||||
|
|
||||||
|
FROM debian:bookworm-slim AS runtime
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apt update -y \
|
||||||
|
&& apt install -y --no-install-recommends openssl ca-certificates \
|
||||||
|
&& apt autoremove -y \
|
||||||
|
&& apt clean -y \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
COPY --from=builder /app/target/release/zero2prod zero2prod
|
||||||
|
COPY configuration configuration
|
||||||
|
ENV APP_ENVIRONMENT=production
|
||||||
|
ENTRYPOINT [ "./zero2prod" ]
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
application_port: 8000
|
application:
|
||||||
|
port: 8000
|
||||||
database:
|
database:
|
||||||
host: "127.0.0.1"
|
host: "127.0.0.1"
|
||||||
port: 5432
|
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"
|
||||||
@@ -2,19 +2,61 @@ use secrecy::{ExposeSecret, SecretString};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
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()
|
let settings = config::Config::builder()
|
||||||
.add_source(config::File::new(
|
.add_source(config::File::from(config_dir.join("base.yaml")))
|
||||||
"configuration.yaml",
|
.add_source(config::File::from(config_dir.join(environment_filename)))
|
||||||
config::FileFormat::Yaml,
|
|
||||||
))
|
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
settings.try_deserialize::<Settings>()
|
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)]
|
#[derive(Deserialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
pub application: ApplicationSettings,
|
||||||
pub database: DatabaseSettings,
|
pub database: DatabaseSettings,
|
||||||
pub application_port: u16,
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ApplicationSettings {
|
||||||
|
pub port: u16,
|
||||||
|
pub host: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|||||||
@@ -7,14 +7,15 @@ use zero2prod::{configuration::get_configuration, startup::run, telemetry::init_
|
|||||||
async fn main() {
|
async fn main() {
|
||||||
init_subscriber(std::io::stdout);
|
init_subscriber(std::io::stdout);
|
||||||
let configuration = get_configuration().expect("Failed to read configuration");
|
let configuration = get_configuration().expect("Failed to read configuration");
|
||||||
let listener = TcpListener::bind(format!("127.0.0.1:{}", configuration.application_port))
|
let listener = TcpListener::bind(format!(
|
||||||
|
"{}:{}",
|
||||||
|
configuration.application.host, configuration.application.port
|
||||||
|
))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||||
let connection_pool =
|
let connection_pool =
|
||||||
PgPool::connect(configuration.database.connection_string().expose_secret())
|
PgPool::connect_lazy(configuration.database.connection_string().expose_secret()).unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
run(listener, connection_pool).await
|
run(listener, connection_pool).await
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user