feat(docker): add docker support

This commit is contained in:
Kristofers Solo 2024-03-24 20:49:41 +02:00
parent 58681f9220
commit c2b4ac560a
10 changed files with 119 additions and 63 deletions

View File

@ -29,6 +29,8 @@ jobs:
run: sudo apt-get update && sudo apt-get install -y postgresql-client
- name: Migrate database
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
run: cargo test
fmt:

8
Dockerfile Normal file
View 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"]

View File

@ -1,4 +1,5 @@
application_port = 8000
[application]
port = 8000
[database]
host = "127.0.0.1"

2
config/local.toml Normal file
View File

@ -0,0 +1,2 @@
[application]
host = "127.0.0.1"

2
config/production.toml Normal file
View File

@ -0,0 +1,2 @@
[application]
host = "0.0.0.0"

93
src/config.rs Normal file
View 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
)),
}
}
}

View File

@ -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>()
}

View File

@ -1,3 +1,3 @@
pub mod configuation;
pub mod config;
pub mod routes;
pub mod telemetry;

View File

@ -1,10 +1,8 @@
use std::net::SocketAddr;
use secrecy::ExposeSecret;
use sqlx::postgres::PgPoolOptions;
use tokio::net::TcpListener;
use zero2prod::{
configuation::get_configuration,
config::get_config,
routes::route,
telemetry::{get_subscriber, init_subscriber},
};
@ -13,12 +11,11 @@ use zero2prod::{
async fn main() -> Result<(), std::io::Error> {
let subscriber = get_subscriber("zero2prod", "info", std::io::stdout);
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()
.connect(&configuation.database.to_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");
let addr = SocketAddr::from(([127, 0, 0, 1], configuation.application_port));
.connect_lazy(&config.database.to_string().expose_secret())
.expect("Failed to create Postgres connection pool.");
let addr = format!("{}:{}", config.application.host, config.application.port);
let listener = TcpListener::bind(addr)
.await
.expect("Failed to bind port 8000.");

View File

@ -5,7 +5,7 @@ use sqlx::{postgres::PgPoolOptions, Connection, Executor, PgConnection, PgPool};
use tokio::net::TcpListener;
use uuid::Uuid;
use zero2prod::{
configuation::{get_configuration, DatabaseSettings},
config::{get_config, DatabaseSettings},
routes::route,
telemetry::{get_subscriber, init_subscriber},
};
@ -28,8 +28,8 @@ async fn health_check() {
#[tokio::test]
async fn subscribe_returns_200_for_valid_form_data() {
let app = spawn_app().await;
let configuration = get_configuration().expect("Failed to read configuration.");
let mut connection = PgConnection::connect(&configuration.database.to_string().expose_secret())
let config = get_config().expect("Failed to read configuration.");
let mut connection = PgConnection::connect(&config.database.to_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");
let client = Client::new();
@ -106,7 +106,7 @@ async fn spawn_app() -> TestApp {
.expect("Failed to bind random port");
let port = listener.local_addr().unwrap().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();
let pool = configure_database(&config.database).await;