mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
feat(docker): add docker support
This commit is contained in:
parent
58681f9220
commit
c2b4ac560a
2
.github/workflows/CI.yml
vendored
2
.github/workflows/CI.yml
vendored
@ -29,6 +29,8 @@ jobs:
|
|||||||
run: sudo apt-get update && sudo apt-get install -y postgresql-client
|
run: sudo apt-get update && sudo apt-get install -y postgresql-client
|
||||||
- name: Migrate database
|
- name: Migrate database
|
||||||
run: SKIP_DOCKER=true ./scripts/init_db
|
run: SKIP_DOCKER=true ./scripts/init_db
|
||||||
|
- name: Check sqlx-data.json is up to date
|
||||||
|
run: cargo sqlx prepare --workspace --check
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test
|
run: cargo test
|
||||||
fmt:
|
fmt:
|
||||||
|
|||||||
8
Dockerfile
Normal file
8
Dockerfile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
FROM rust:1.77.0
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apt-get update && apt-get 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]
|
[database]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
2
config/local.toml
Normal file
2
config/local.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[application]
|
||||||
|
host = "127.0.0.1"
|
||||||
2
config/production.toml
Normal file
2
config/production.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[application]
|
||||||
|
host = "0.0.0.0"
|
||||||
93
src/config.rs
Normal file
93
src/config.rs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use secrecy::{ExposeSecret, Secret};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Settings {
|
||||||
|
pub database: DatabaseSettings,
|
||||||
|
pub application: ApplicationSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct DatabaseSettings {
|
||||||
|
pub username: String,
|
||||||
|
pub password: Secret<String>,
|
||||||
|
pub port: u16,
|
||||||
|
pub host: String,
|
||||||
|
pub database_name: String,
|
||||||
|
}
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ApplicationSettings {
|
||||||
|
pub port: u16,
|
||||||
|
pub host: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DatabaseSettings {
|
||||||
|
pub fn to_string_no_db(&self) -> Secret<String> {
|
||||||
|
Secret::new(format!(
|
||||||
|
"postgres://{}:{}@{}:{}",
|
||||||
|
self.username,
|
||||||
|
self.password.expose_secret(),
|
||||||
|
self.host,
|
||||||
|
self.port
|
||||||
|
))
|
||||||
|
}
|
||||||
|
pub fn to_string(&self) -> Secret<String> {
|
||||||
|
Secret::new(format!(
|
||||||
|
"postgres://{}:{}@{}:{}/{}",
|
||||||
|
self.username,
|
||||||
|
self.password.expose_secret(),
|
||||||
|
self.host,
|
||||||
|
self.port,
|
||||||
|
self.database_name
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_config() -> Result<Settings, config::ConfigError> {
|
||||||
|
let base_path = std::env::current_dir().expect("Failed to determine current directory");
|
||||||
|
let config_directory = base_path.join("config");
|
||||||
|
let env: Environment = std::env::var("APP_ENVIRONMENT")
|
||||||
|
.unwrap_or_else(|_| "local".into())
|
||||||
|
.try_into()
|
||||||
|
.expect("Failed to parse APP_ENVIRONMENT");
|
||||||
|
|
||||||
|
let env_filename = format!("{}.toml", &env);
|
||||||
|
|
||||||
|
let settings = config::Config::builder()
|
||||||
|
.add_source(config::File::from(config_directory.join("base.toml")))
|
||||||
|
.add_source(config::File::from(config_directory.join(env_filename)))
|
||||||
|
.build()?;
|
||||||
|
settings.try_deserialize::<Settings>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Environment {
|
||||||
|
Local,
|
||||||
|
Production,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Environment {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Environment::Local => write!(f, "local"),
|
||||||
|
Environment::Production => write!(f, "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(Self::Local),
|
||||||
|
"production" => Ok(Self::Production),
|
||||||
|
other => Err(format!(
|
||||||
|
"{} is not supported environment. \
|
||||||
|
Use either `local` or `production`.",
|
||||||
|
other
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,49 +0,0 @@
|
|||||||
use secrecy::{ExposeSecret, Secret};
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct Settings {
|
|
||||||
pub database: DatabaseSettings,
|
|
||||||
pub application_port: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct DatabaseSettings {
|
|
||||||
pub username: String,
|
|
||||||
pub password: Secret<String>,
|
|
||||||
pub port: u16,
|
|
||||||
pub host: String,
|
|
||||||
pub database_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DatabaseSettings {
|
|
||||||
pub fn to_string_no_db(&self) -> Secret<String> {
|
|
||||||
Secret::new(format!(
|
|
||||||
"postgres://{}:{}@{}:{}",
|
|
||||||
self.username,
|
|
||||||
self.password.expose_secret(),
|
|
||||||
self.host,
|
|
||||||
self.port
|
|
||||||
))
|
|
||||||
}
|
|
||||||
pub fn to_string(&self) -> Secret<String> {
|
|
||||||
Secret::new(format!(
|
|
||||||
"postgres://{}:{}@{}:{}/{}",
|
|
||||||
self.username,
|
|
||||||
self.password.expose_secret(),
|
|
||||||
self.host,
|
|
||||||
self.port,
|
|
||||||
self.database_name
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
|
||||||
let settings = config::Config::builder()
|
|
||||||
.add_source(config::File::new(
|
|
||||||
"configuration.toml",
|
|
||||||
config::FileFormat::Toml,
|
|
||||||
))
|
|
||||||
.build()?;
|
|
||||||
settings.try_deserialize::<Settings>()
|
|
||||||
}
|
|
||||||
@ -1,3 +1,3 @@
|
|||||||
pub mod configuation;
|
pub mod config;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod telemetry;
|
pub mod telemetry;
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@ -1,10 +1,8 @@
|
|||||||
use std::net::SocketAddr;
|
|
||||||
|
|
||||||
use secrecy::ExposeSecret;
|
use secrecy::ExposeSecret;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use zero2prod::{
|
use zero2prod::{
|
||||||
configuation::get_configuration,
|
config::get_config,
|
||||||
routes::route,
|
routes::route,
|
||||||
telemetry::{get_subscriber, init_subscriber},
|
telemetry::{get_subscriber, init_subscriber},
|
||||||
};
|
};
|
||||||
@ -13,12 +11,11 @@ use zero2prod::{
|
|||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let subscriber = get_subscriber("zero2prod", "info", std::io::stdout);
|
let subscriber = get_subscriber("zero2prod", "info", std::io::stdout);
|
||||||
init_subscriber(subscriber);
|
init_subscriber(subscriber);
|
||||||
let configuation = get_configuration().expect("Failed to read configuation.");
|
let config = get_config().expect("Failed to read configuation.");
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.connect(&configuation.database.to_string().expose_secret())
|
.connect_lazy(&config.database.to_string().expose_secret())
|
||||||
.await
|
.expect("Failed to create Postgres connection pool.");
|
||||||
.expect("Failed to connect to Postgres.");
|
let addr = format!("{}:{}", config.application.host, config.application.port);
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], configuation.application_port));
|
|
||||||
let listener = TcpListener::bind(addr)
|
let listener = TcpListener::bind(addr)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to bind port 8000.");
|
.expect("Failed to bind port 8000.");
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use sqlx::{postgres::PgPoolOptions, Connection, Executor, PgConnection, PgPool};
|
|||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use zero2prod::{
|
use zero2prod::{
|
||||||
configuation::{get_configuration, DatabaseSettings},
|
config::{get_config, DatabaseSettings},
|
||||||
routes::route,
|
routes::route,
|
||||||
telemetry::{get_subscriber, init_subscriber},
|
telemetry::{get_subscriber, init_subscriber},
|
||||||
};
|
};
|
||||||
@ -28,8 +28,8 @@ async fn health_check() {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn subscribe_returns_200_for_valid_form_data() {
|
async fn subscribe_returns_200_for_valid_form_data() {
|
||||||
let app = spawn_app().await;
|
let app = spawn_app().await;
|
||||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
let config = get_config().expect("Failed to read configuration.");
|
||||||
let mut connection = PgConnection::connect(&configuration.database.to_string().expose_secret())
|
let mut connection = PgConnection::connect(&config.database.to_string().expose_secret())
|
||||||
.await
|
.await
|
||||||
.expect("Failed to connect to Postgres.");
|
.expect("Failed to connect to Postgres.");
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
@ -106,7 +106,7 @@ async fn spawn_app() -> TestApp {
|
|||||||
.expect("Failed to bind random port");
|
.expect("Failed to bind random port");
|
||||||
let port = listener.local_addr().unwrap().port();
|
let port = listener.local_addr().unwrap().port();
|
||||||
let address = format!("http://127.0.0.1:{}", port);
|
let address = format!("http://127.0.0.1:{}", port);
|
||||||
let mut config = get_configuration().expect("Failed to read configuration.");
|
let mut config = get_config().expect("Failed to read configuration.");
|
||||||
|
|
||||||
config.database.database_name = Uuid::new_v4().to_string();
|
config.database.database_name = Uuid::new_v4().to_string();
|
||||||
let pool = configure_database(&config.database).await;
|
let pool = configure_database(&config.database).await;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user