mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-21 16:21:01 +00:00
Refactor structure to use one root branch per chapter.
This commit is contained in:
parent
95e403575a
commit
1f0b14c248
54 changed files with 103 additions and 951 deletions
93
Cargo.lock
generated
93
Cargo.lock
generated
|
@ -473,76 +473,6 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chapter03-0"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"actix-rt",
|
|
||||||
"actix-web",
|
|
||||||
"reqwest",
|
|
||||||
"tokio",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chapter03-1"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"actix-rt",
|
|
||||||
"actix-web",
|
|
||||||
"chrono",
|
|
||||||
"config",
|
|
||||||
"reqwest",
|
|
||||||
"serde",
|
|
||||||
"sqlx",
|
|
||||||
"tokio",
|
|
||||||
"uuid",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chapter04"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"actix-rt",
|
|
||||||
"actix-web",
|
|
||||||
"chrono",
|
|
||||||
"config",
|
|
||||||
"lazy_static",
|
|
||||||
"reqwest",
|
|
||||||
"serde",
|
|
||||||
"sqlx",
|
|
||||||
"tokio",
|
|
||||||
"tracing",
|
|
||||||
"tracing-actix-web",
|
|
||||||
"tracing-bunyan-formatter",
|
|
||||||
"tracing-futures",
|
|
||||||
"tracing-log",
|
|
||||||
"tracing-subscriber",
|
|
||||||
"uuid",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chapter05"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"actix-rt",
|
|
||||||
"actix-web",
|
|
||||||
"chrono",
|
|
||||||
"config",
|
|
||||||
"lazy_static",
|
|
||||||
"reqwest",
|
|
||||||
"serde",
|
|
||||||
"serde-aux",
|
|
||||||
"sqlx",
|
|
||||||
"tokio",
|
|
||||||
"tracing",
|
|
||||||
"tracing-actix-web",
|
|
||||||
"tracing-bunyan-formatter",
|
|
||||||
"tracing-futures",
|
|
||||||
"tracing-log",
|
|
||||||
"tracing-subscriber",
|
|
||||||
"uuid",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.19"
|
version = "0.4.19"
|
||||||
|
@ -2730,3 +2660,26 @@ checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"linked-hash-map",
|
"linked-hash-map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zero2prod"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"actix-rt",
|
||||||
|
"actix-web",
|
||||||
|
"chrono",
|
||||||
|
"config",
|
||||||
|
"lazy_static",
|
||||||
|
"reqwest",
|
||||||
|
"serde",
|
||||||
|
"serde-aux",
|
||||||
|
"sqlx",
|
||||||
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
"tracing-actix-web",
|
||||||
|
"tracing-bunyan-formatter",
|
||||||
|
"tracing-futures",
|
||||||
|
"tracing-log",
|
||||||
|
"tracing-subscriber",
|
||||||
|
"uuid",
|
||||||
|
]
|
||||||
|
|
36
Cargo.toml
36
Cargo.toml
|
@ -1,2 +1,34 @@
|
||||||
[workspace]
|
[package]
|
||||||
members = ["chapter03-0", "chapter03-1", "chapter04", "chapter05"]
|
name = "zero2prod"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["LukeMathWalker <rust@lpalmieri.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[lib]
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
path = "src/main.rs"
|
||||||
|
name = "zero2prod"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = "3.0.0"
|
||||||
|
actix-rt = "1.1.1"
|
||||||
|
tokio = "0.2.22"
|
||||||
|
serde = "1.0.115"
|
||||||
|
config = { version = "0.10.1", default-features = false, features = ["yaml"] }
|
||||||
|
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
|
||||||
|
uuid = { version = "0.8.1", features = ["v4"] }
|
||||||
|
chrono = "0.4.15"
|
||||||
|
tracing = "0.1.19"
|
||||||
|
tracing-futures = "0.2.4"
|
||||||
|
tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] }
|
||||||
|
tracing-bunyan-formatter = "0.1.6"
|
||||||
|
tracing-log = "0.1.1"
|
||||||
|
tracing-actix-web = "0.2.0"
|
||||||
|
serde-aux = "1.0.1"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
reqwest = { version = "0.10.7", features = ["json"] }
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -24,7 +24,7 @@ COPY --from=cacher /usr/local/cargo /usr/local/cargo
|
||||||
COPY . .
|
COPY . .
|
||||||
# Build our application, leveraging the cached deps!
|
# Build our application, leveraging the cached deps!
|
||||||
ENV SQLX_OFFLINE true
|
ENV SQLX_OFFLINE true
|
||||||
RUN cargo build --release --bin chapter05
|
RUN cargo build --release --bin zero2prod
|
||||||
|
|
||||||
FROM debian:buster-slim AS runtime
|
FROM debian:buster-slim AS runtime
|
||||||
WORKDIR app
|
WORKDIR app
|
||||||
|
@ -32,7 +32,7 @@ RUN apt-get update -y \
|
||||||
&& apt-get install -y --no-install-recommends openssl \
|
&& apt-get install -y --no-install-recommends openssl \
|
||||||
# Clean up
|
# Clean up
|
||||||
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
|
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
|
||||||
COPY --from=builder /app/target/release/chapter05 zero2prod
|
COPY --from=builder /app/target/release/zero2prod zero2prod
|
||||||
COPY configuration configuration
|
COPY configuration configuration
|
||||||
ENV APP_ENVIRONMENT production
|
ENV APP_ENVIRONMENT production
|
||||||
ENTRYPOINT ["./zero2prod"]
|
ENTRYPOINT ["./zero2prod"]
|
45
README.md
45
README.md
|
@ -6,15 +6,42 @@
|
||||||
|
|
||||||
This repository serves as supplementary material for [the book](https://zero2prod.com/): it hosts snapshots of the codebase of our email newsletter project at end of each chapter.
|
This repository serves as supplementary material for [the book](https://zero2prod.com/): it hosts snapshots of the codebase of our email newsletter project at end of each chapter.
|
||||||
|
|
||||||
It is structured as a `cargo` workspace: running `cargo build` will build the codebase for **all** chapters.
|
## Chapter snapshots
|
||||||
If you want to build/test/run the code for a _specific_ chapter, just move into its folder! E.g.:
|
|
||||||
|
The `master` branch (where you are right now!) shows the project at the end of the last published chapter _(Chapter 5, right now)_.
|
||||||
|
|
||||||
|
You can browse the project at end of previous chapters by switching to their dedicated branches:
|
||||||
|
|
||||||
|
- [Chapter 3, Part 0](https://github.com/LukeMathWalker/zero-to-production/tree/root-chapter-03-part0)
|
||||||
|
- [Chapter 3, Part 1](https://github.com/LukeMathWalker/zero-to-production/tree/root-chapter-03-part1)
|
||||||
|
- [Chapter 4](https://github.com/LukeMathWalker/zero-to-production/tree/root-chapter-04)
|
||||||
|
- [Chapter 5](https://github.com/LukeMathWalker/zero-to-production/tree/root-chapter-05)
|
||||||
|
|
||||||
|
## Pre-requisite
|
||||||
|
|
||||||
|
You'll need to install:
|
||||||
|
|
||||||
|
- [Rust](https://www.rust-lang.org/tools/install)
|
||||||
|
- [Docker](https://docs.docker.com/get-docker/)
|
||||||
|
|
||||||
|
Launch a (migrated) Postgres database via Docker:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./scripts/init_db.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to build
|
||||||
|
|
||||||
|
Using `cargo`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo build
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to test
|
||||||
|
|
||||||
|
Using `cargo`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Run tests for the first part of Chapter 3
|
|
||||||
cd chapter03-0
|
|
||||||
cargo test
|
cargo test
|
||||||
```
|
```
|
||||||
Alternatively, from the top-level folder, you can specify the binary you are interested into:
|
|
||||||
```bash
|
|
||||||
# Run the application as it is at end of the first part of Chapter 3
|
|
||||||
cargo run --bin chapter03-0
|
|
||||||
```
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "chapter03-0"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["LukeMathWalker <rust@lpalmieri.com>"]
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
[lib]
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
path = "src/main.rs"
|
|
||||||
name = "chapter03-0"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-web = "3.0.0"
|
|
||||||
actix-rt = "1.1.1"
|
|
||||||
tokio = "0.2.22"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
reqwest = "0.10.7"
|
|
|
@ -1,14 +0,0 @@
|
||||||
use actix_web::dev::Server;
|
|
||||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
async fn health_check() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
|
||||||
let server = HttpServer::new(|| App::new().route("/health_check", web::get().to(health_check)))
|
|
||||||
.listen(listener)?
|
|
||||||
.run();
|
|
||||||
Ok(server)
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
use chapter03_0::run;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
let address = TcpListener::bind("127.0.0.1:8000")?;
|
|
||||||
run(address)?.await
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
use chapter03_0::run;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
fn spawn_app() -> String {
|
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
|
||||||
// We retrieve the port assigned to us by the OS
|
|
||||||
let port = listener.local_addr().unwrap().port();
|
|
||||||
let server = run(listener).expect("Failed to bind address");
|
|
||||||
let _ = tokio::spawn(server);
|
|
||||||
// We return the application address to the caller!
|
|
||||||
format!("http://127.0.0.1:{}", port)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn health_check_works() {
|
|
||||||
// Arrange
|
|
||||||
let address = spawn_app();
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
// Use the returned application address
|
|
||||||
.get(&format!("{}/health_check", &address))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert!(response.status().is_success());
|
|
||||||
assert_eq!(Some(0), response.content_length());
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "chapter03-1"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["LukeMathWalker <rust@lpalmieri.com>"]
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
[lib]
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
path = "src/main.rs"
|
|
||||||
name = "chapter03-1"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-web = "3.0.0"
|
|
||||||
actix-rt = "1.1.1"
|
|
||||||
tokio = "0.2.22"
|
|
||||||
serde = "1.0.115"
|
|
||||||
config = { version = "0.10.1", default-features = false, features = ["yaml"] }
|
|
||||||
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate"] }
|
|
||||||
uuid = { version = "0.8.1", features = ["v4"] }
|
|
||||||
chrono = "0.4.15"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
reqwest = { version = "0.10.7", features = ["json"] }
|
|
|
@ -1,7 +0,0 @@
|
||||||
application_port: 8000
|
|
||||||
database:
|
|
||||||
host: "localhost"
|
|
||||||
port: 5432
|
|
||||||
username: "postgres"
|
|
||||||
password: "password"
|
|
||||||
database_name: "newsletter"
|
|
|
@ -1,38 +0,0 @@
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct Settings {
|
|
||||||
pub database: DatabaseSettings,
|
|
||||||
pub application_port: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct DatabaseSettings {
|
|
||||||
pub username: String,
|
|
||||||
pub password: String,
|
|
||||||
pub port: u16,
|
|
||||||
pub host: String,
|
|
||||||
pub database_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DatabaseSettings {
|
|
||||||
pub fn connection_string(&self) -> String {
|
|
||||||
format!(
|
|
||||||
"postgres://{}:{}@{}:{}/{}",
|
|
||||||
self.username, self.password, self.host, self.port, self.database_name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn connection_string_without_db(&self) -> String {
|
|
||||||
format!(
|
|
||||||
"postgres://{}:{}@{}:{}",
|
|
||||||
self.username, self.password, self.host, self.port
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
|
||||||
let mut settings = config::Config::default();
|
|
||||||
|
|
||||||
settings.merge(config::File::with_name("configuration"))?;
|
|
||||||
|
|
||||||
settings.try_into()
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
#![allow(clippy::toplevel_ref_arg)]
|
|
||||||
pub mod configuration;
|
|
||||||
pub mod routes;
|
|
||||||
pub mod startup;
|
|
|
@ -1,23 +0,0 @@
|
||||||
use chapter03_1::configuration::get_configuration;
|
|
||||||
use chapter03_1::startup::run;
|
|
||||||
use sqlx::postgres::PgPool;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
|
||||||
let connection_pool = PgPool::connect(&configuration.database.connection_string())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres.");
|
|
||||||
|
|
||||||
// Here we choose to bind explicitly to localhost, 127.0.0.1, for security
|
|
||||||
// reasons. This binding may cause issues in some environments. For example,
|
|
||||||
// it causes connectivity issues running in WSL2, where you cannot reach the
|
|
||||||
// server when it is bound to WSL2's localhost interface. As a workaround,
|
|
||||||
// you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware
|
|
||||||
// of the security implications when you expose the server on all interfaces.
|
|
||||||
let address = format!("127.0.0.1:{}", configuration.application_port);
|
|
||||||
let listener = TcpListener::bind(address)?;
|
|
||||||
run(listener, connection_pool)?.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
use actix_web::{web, HttpResponse};
|
|
||||||
use chrono::Utc;
|
|
||||||
use sqlx::PgPool;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct FormData {
|
|
||||||
email: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn subscribe(
|
|
||||||
form: web::Form<FormData>,
|
|
||||||
pool: web::Data<PgPool>,
|
|
||||||
) -> Result<HttpResponse, HttpResponse> {
|
|
||||||
sqlx::query!(
|
|
||||||
r#"
|
|
||||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
|
||||||
VALUES ($1, $2, $3, $4)
|
|
||||||
"#,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
form.email,
|
|
||||||
form.name,
|
|
||||||
Utc::now()
|
|
||||||
)
|
|
||||||
.execute(pool.as_ref())
|
|
||||||
.await
|
|
||||||
.map_err(|e| {
|
|
||||||
println!("Failed to execute query: {}", e);
|
|
||||||
HttpResponse::InternalServerError().finish()
|
|
||||||
})?;
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
use crate::routes::{health_check, subscribe};
|
|
||||||
use actix_web::dev::Server;
|
|
||||||
use actix_web::web::Data;
|
|
||||||
use actix_web::{web, App, HttpServer};
|
|
||||||
use sqlx::PgPool;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
|
|
||||||
let db_pool = Data::new(db_pool);
|
|
||||||
let server = HttpServer::new(move || {
|
|
||||||
App::new()
|
|
||||||
.route("/health_check", web::get().to(health_check))
|
|
||||||
.route("/subscriptions", web::post().to(subscribe))
|
|
||||||
.app_data(db_pool.clone())
|
|
||||||
})
|
|
||||||
.listen(listener)?
|
|
||||||
.run();
|
|
||||||
Ok(server)
|
|
||||||
}
|
|
|
@ -1,129 +0,0 @@
|
||||||
use chapter03_1::configuration::{get_configuration, DatabaseSettings};
|
|
||||||
use chapter03_1::startup::run;
|
|
||||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
|
||||||
use std::net::TcpListener;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub struct TestApp {
|
|
||||||
pub address: String,
|
|
||||||
pub db_pool: PgPool,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn spawn_app() -> TestApp {
|
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
|
||||||
// We retrieve the port assigned to us by the OS
|
|
||||||
let port = listener.local_addr().unwrap().port();
|
|
||||||
let address = format!("http://127.0.0.1:{}", port);
|
|
||||||
|
|
||||||
let mut configuration = get_configuration().expect("Failed to read configuration.");
|
|
||||||
configuration.database.database_name = Uuid::new_v4().to_string();
|
|
||||||
let connection_pool = configure_database(&configuration.database).await;
|
|
||||||
|
|
||||||
let server = run(listener, connection_pool.clone()).expect("Failed to bind address");
|
|
||||||
let _ = tokio::spawn(server);
|
|
||||||
TestApp {
|
|
||||||
address,
|
|
||||||
db_pool: connection_pool,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
|
||||||
// Create database
|
|
||||||
let mut connection = PgConnection::connect(&config.connection_string_without_db())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres");
|
|
||||||
connection
|
|
||||||
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
|
|
||||||
.await
|
|
||||||
.expect("Failed to create database.");
|
|
||||||
|
|
||||||
// Migrate database
|
|
||||||
let connection_pool = PgPool::connect(&config.connection_string())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres.");
|
|
||||||
sqlx::migrate!("./migrations")
|
|
||||||
.run(&connection_pool)
|
|
||||||
.await
|
|
||||||
.expect("Failed to migrate the database");
|
|
||||||
|
|
||||||
connection_pool
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn health_check_works() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
// Use the returned application address
|
|
||||||
.get(&format!("{}/health_check", &app.address))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert!(response.status().is_success());
|
|
||||||
assert_eq!(Some(0), response.content_length());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
||||||
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert_eq!(200, response.status().as_u16());
|
|
||||||
|
|
||||||
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
|
|
||||||
.fetch_one(&app.db_pool)
|
|
||||||
.await
|
|
||||||
.expect("Failed to fetch saved subscription.");
|
|
||||||
|
|
||||||
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
|
|
||||||
assert_eq!(saved.name, "le guin");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn subscribe_returns_a_400_when_data_is_missing() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let test_cases = vec![
|
|
||||||
("name=le%20guin", "missing the email"),
|
|
||||||
("email=ursula_le_guin%40gmail.com", "missing the name"),
|
|
||||||
("", "missing both name and email"),
|
|
||||||
];
|
|
||||||
|
|
||||||
for (invalid_body, error_message) in test_cases {
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(invalid_body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert_eq!(
|
|
||||||
400,
|
|
||||||
response.status().as_u16(),
|
|
||||||
// Additional customised error message on test failure
|
|
||||||
"The API did not fail with 400 Bad Request when the payload was {}.",
|
|
||||||
error_message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter"
|
|
|
@ -1,33 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "chapter04"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["LukeMathWalker <rust@lpalmieri.com>"]
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
[lib]
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
path = "src/main.rs"
|
|
||||||
name = "chapter04"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-web = "3.0.0"
|
|
||||||
actix-rt = "1.1.1"
|
|
||||||
tokio = "0.2.22"
|
|
||||||
serde = "1.0.115"
|
|
||||||
config = { version = "0.10.1", default-features = false, features = ["yaml"] }
|
|
||||||
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate"] }
|
|
||||||
uuid = { version = "0.8.1", features = ["v4"] }
|
|
||||||
chrono = "0.4.15"
|
|
||||||
tracing = "0.1.19"
|
|
||||||
tracing-futures = "0.2.4"
|
|
||||||
tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] }
|
|
||||||
tracing-bunyan-formatter = "0.1.6"
|
|
||||||
tracing-log = "0.1.1"
|
|
||||||
tracing-actix-web = "0.2.0"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
reqwest = { version = "0.10.7", features = ["json"] }
|
|
||||||
lazy_static = "1.4.0"
|
|
|
@ -1,7 +0,0 @@
|
||||||
application_port: 8000
|
|
||||||
database:
|
|
||||||
host: "localhost"
|
|
||||||
port: 5432
|
|
||||||
username: "postgres"
|
|
||||||
password: "password"
|
|
||||||
database_name: "newsletter"
|
|
|
@ -1,8 +0,0 @@
|
||||||
-- Create Subscriptions Table
|
|
||||||
CREATE TABLE subscriptions(
|
|
||||||
id uuid NOT NULL,
|
|
||||||
PRIMARY KEY (id),
|
|
||||||
email TEXT NOT NULL UNIQUE,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
subscribed_at timestamptz NOT NULL
|
|
||||||
);
|
|
|
@ -1,40 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -x
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
# Check if a custom user has been set, otherwise default to 'postgres'
|
|
||||||
DB_USER=${POSTGRES_USER:=postgres}
|
|
||||||
# Check if a custom password has been set, otherwise default to 'password'
|
|
||||||
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
|
||||||
# Check if a custom password has been set, otherwise default to 'newsletter'
|
|
||||||
DB_NAME="${POSTGRES_DB:=newsletter}"
|
|
||||||
# Check if a custom port has been set, otherwise default to '5432'
|
|
||||||
DB_PORT="${POSTGRES_PORT:=5432}"
|
|
||||||
|
|
||||||
# Allow to skip Docker if a dockerized Postgres database is already running
|
|
||||||
if [[ -z "${SKIP_DOCKER}" ]]
|
|
||||||
then
|
|
||||||
# Launch postgres using Docker
|
|
||||||
docker run \
|
|
||||||
-e POSTGRES_USER=${DB_USER} \
|
|
||||||
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
|
||||||
-e POSTGRES_DB=${DB_NAME} \
|
|
||||||
-p "${DB_PORT}":5432 \
|
|
||||||
-d postgres \
|
|
||||||
postgres -N 1000
|
|
||||||
# ^ Increased maximum number of connections for testing purposes
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Keep pinging Postgres until it's ready to accept commands
|
|
||||||
until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
|
|
||||||
>&2 echo "Postgres is still unavailable - sleeping"
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
|
|
||||||
|
|
||||||
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}
|
|
||||||
sqlx database create
|
|
||||||
sqlx migrate run
|
|
||||||
|
|
||||||
>&2 echo "Postgres has been migrated, ready to go!"
|
|
|
@ -1,38 +0,0 @@
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct Settings {
|
|
||||||
pub database: DatabaseSettings,
|
|
||||||
pub application_port: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct DatabaseSettings {
|
|
||||||
pub username: String,
|
|
||||||
pub password: String,
|
|
||||||
pub port: u16,
|
|
||||||
pub host: String,
|
|
||||||
pub database_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DatabaseSettings {
|
|
||||||
pub fn connection_string(&self) -> String {
|
|
||||||
format!(
|
|
||||||
"postgres://{}:{}@{}:{}/{}",
|
|
||||||
self.username, self.password, self.host, self.port, self.database_name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn connection_string_without_db(&self) -> String {
|
|
||||||
format!(
|
|
||||||
"postgres://{}:{}@{}:{}",
|
|
||||||
self.username, self.password, self.host, self.port
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
|
||||||
let mut settings = config::Config::default();
|
|
||||||
|
|
||||||
settings.merge(config::File::with_name("configuration"))?;
|
|
||||||
|
|
||||||
settings.try_into()
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
use chapter04::configuration::get_configuration;
|
|
||||||
use chapter04::startup::run;
|
|
||||||
use chapter04::telemetry::{get_subscriber, init_subscriber};
|
|
||||||
use sqlx::postgres::PgPool;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
let subscriber = get_subscriber("zero2prod".into(), "info".into());
|
|
||||||
init_subscriber(subscriber);
|
|
||||||
|
|
||||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
|
||||||
let connection_pool = PgPool::connect(&configuration.database.connection_string())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres.");
|
|
||||||
|
|
||||||
// Here we choose to bind explicitly to localhost, 127.0.0.1, for security
|
|
||||||
// reasons. This binding may cause issues in some environments. For example,
|
|
||||||
// it causes connectivity issues running in WSL2, where you cannot reach the
|
|
||||||
// server when it is bound to WSL2's localhost interface. As a workaround,
|
|
||||||
// you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware
|
|
||||||
// of the security implications when you expose the server on all interfaces.
|
|
||||||
let address = format!("127.0.0.1:{}", configuration.application_port);
|
|
||||||
let listener = TcpListener::bind(address)?;
|
|
||||||
run(listener, connection_pool)?.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
use actix_web::HttpResponse;
|
|
||||||
|
|
||||||
pub async fn health_check() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().finish()
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
mod health_check;
|
|
||||||
mod subscriptions;
|
|
||||||
|
|
||||||
pub use health_check::*;
|
|
||||||
pub use subscriptions::*;
|
|
|
@ -1,143 +0,0 @@
|
||||||
use chapter04::configuration::{get_configuration, DatabaseSettings};
|
|
||||||
use chapter04::startup::run;
|
|
||||||
use chapter04::telemetry::{get_subscriber, init_subscriber};
|
|
||||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
|
||||||
use std::net::TcpListener;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
static ref TRACING: () = {
|
|
||||||
let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" };
|
|
||||||
let subscriber = get_subscriber("test".into(), filter.into());
|
|
||||||
init_subscriber(subscriber);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TestApp {
|
|
||||||
pub address: String,
|
|
||||||
pub db_pool: PgPool,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn spawn_app() -> TestApp {
|
|
||||||
// The first time `initialize` is invoked the code in `TRACING` is executed.
|
|
||||||
// All other invocations will instead skip execution.
|
|
||||||
lazy_static::initialize(&TRACING);
|
|
||||||
|
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
|
||||||
// We retrieve the port assigned to us by the OS
|
|
||||||
let port = listener.local_addr().unwrap().port();
|
|
||||||
let address = format!("http://127.0.0.1:{}", port);
|
|
||||||
|
|
||||||
let mut configuration = get_configuration().expect("Failed to read configuration.");
|
|
||||||
configuration.database.database_name = Uuid::new_v4().to_string();
|
|
||||||
let connection_pool = configure_database(&configuration.database).await;
|
|
||||||
|
|
||||||
let server = run(listener, connection_pool.clone()).expect("Failed to bind address");
|
|
||||||
let _ = tokio::spawn(server);
|
|
||||||
TestApp {
|
|
||||||
address,
|
|
||||||
db_pool: connection_pool,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
|
||||||
// Create database
|
|
||||||
let mut connection = PgConnection::connect(&config.connection_string_without_db())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres");
|
|
||||||
connection
|
|
||||||
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
|
|
||||||
.await
|
|
||||||
.expect("Failed to create database.");
|
|
||||||
|
|
||||||
// Migrate database
|
|
||||||
let connection_pool = PgPool::connect(&config.connection_string())
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to Postgres.");
|
|
||||||
sqlx::migrate!("./migrations")
|
|
||||||
.run(&connection_pool)
|
|
||||||
.await
|
|
||||||
.expect("Failed to migrate the database");
|
|
||||||
|
|
||||||
connection_pool
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn health_check_works() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
// Use the returned application address
|
|
||||||
.get(&format!("{}/health_check", &app.address))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert!(response.status().is_success());
|
|
||||||
assert_eq!(Some(0), response.content_length());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
||||||
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert_eq!(200, response.status().as_u16());
|
|
||||||
|
|
||||||
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
|
|
||||||
.fetch_one(&app.db_pool)
|
|
||||||
.await
|
|
||||||
.expect("Failed to fetch saved subscription.");
|
|
||||||
|
|
||||||
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
|
|
||||||
assert_eq!(saved.name, "le guin");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn subscribe_returns_a_400_when_data_is_missing() {
|
|
||||||
// Arrange
|
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let test_cases = vec![
|
|
||||||
("name=le%20guin", "missing the email"),
|
|
||||||
("email=ursula_le_guin%40gmail.com", "missing the name"),
|
|
||||||
("", "missing both name and email"),
|
|
||||||
];
|
|
||||||
|
|
||||||
for (invalid_body, error_message) in test_cases {
|
|
||||||
// Act
|
|
||||||
let response = client
|
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(invalid_body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert_eq!(
|
|
||||||
400,
|
|
||||||
response.status().as_u16(),
|
|
||||||
// Additional customised error message on test failure
|
|
||||||
"The API did not fail with 400 Bad Request when the payload was {}.",
|
|
||||||
error_message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter"
|
|
|
@ -1,34 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "chapter05"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["LukeMathWalker <rust@lpalmieri.com>"]
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
[lib]
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
path = "src/main.rs"
|
|
||||||
name = "chapter05"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-web = "3.0.0"
|
|
||||||
actix-rt = "1.1.1"
|
|
||||||
tokio = "0.2.22"
|
|
||||||
serde = "1.0.115"
|
|
||||||
config = { version = "0.10.1", default-features = false, features = ["yaml"] }
|
|
||||||
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
|
|
||||||
uuid = { version = "0.8.1", features = ["v4"] }
|
|
||||||
chrono = "0.4.15"
|
|
||||||
tracing = "0.1.19"
|
|
||||||
tracing-futures = "0.2.4"
|
|
||||||
tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] }
|
|
||||||
tracing-bunyan-formatter = "0.1.6"
|
|
||||||
tracing-log = "0.1.1"
|
|
||||||
tracing-actix-web = "0.2.0"
|
|
||||||
serde-aux = "1.0.1"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
reqwest = { version = "0.10.7", features = ["json"] }
|
|
||||||
lazy_static = "1.4.0"
|
|
|
@ -1,8 +0,0 @@
|
||||||
-- Create Subscriptions Table
|
|
||||||
CREATE TABLE subscriptions(
|
|
||||||
id uuid NOT NULL,
|
|
||||||
PRIMARY KEY (id),
|
|
||||||
email TEXT NOT NULL UNIQUE,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
subscribed_at timestamptz NOT NULL
|
|
||||||
);
|
|
|
@ -1,40 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -x
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
# Check if a custom user has been set, otherwise default to 'postgres'
|
|
||||||
DB_USER=${POSTGRES_USER:=postgres}
|
|
||||||
# Check if a custom password has been set, otherwise default to 'password'
|
|
||||||
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
|
||||||
# Check if a custom password has been set, otherwise default to 'newsletter'
|
|
||||||
DB_NAME="${POSTGRES_DB:=newsletter}"
|
|
||||||
# Check if a custom port has been set, otherwise default to '5432'
|
|
||||||
DB_PORT="${POSTGRES_PORT:=5432}"
|
|
||||||
|
|
||||||
# Allow to skip Docker if a dockerized Postgres database is already running
|
|
||||||
if [[ -z "${SKIP_DOCKER}" ]]
|
|
||||||
then
|
|
||||||
# Launch postgres using Docker
|
|
||||||
docker run \
|
|
||||||
-e POSTGRES_USER=${DB_USER} \
|
|
||||||
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
|
||||||
-e POSTGRES_DB=${DB_NAME} \
|
|
||||||
-p "${DB_PORT}":5432 \
|
|
||||||
-d postgres \
|
|
||||||
postgres -N 1000
|
|
||||||
# ^ Increased maximum number of connections for testing purposes
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Keep pinging Postgres until it's ready to accept commands
|
|
||||||
until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
|
|
||||||
>&2 echo "Postgres is still unavailable - sleeping"
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
|
|
||||||
|
|
||||||
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}
|
|
||||||
sqlx database create
|
|
||||||
sqlx migrate run
|
|
||||||
|
|
||||||
>&2 echo "Postgres has been migrated, ready to go!"
|
|
|
@ -1,5 +0,0 @@
|
||||||
#![allow(clippy::toplevel_ref_arg)]
|
|
||||||
pub mod configuration;
|
|
||||||
pub mod routes;
|
|
||||||
pub mod startup;
|
|
||||||
pub mod telemetry;
|
|
|
@ -1,5 +0,0 @@
|
||||||
use actix_web::HttpResponse;
|
|
||||||
|
|
||||||
pub async fn health_check() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().finish()
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
mod health_check;
|
|
||||||
mod subscriptions;
|
|
||||||
|
|
||||||
pub use health_check::*;
|
|
||||||
pub use subscriptions::*;
|
|
|
@ -1,52 +0,0 @@
|
||||||
use actix_web::{web, HttpResponse};
|
|
||||||
use chrono::Utc;
|
|
||||||
use sqlx::PgPool;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
pub struct FormData {
|
|
||||||
email: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(
|
|
||||||
name = "Adding a new subscriber",
|
|
||||||
skip(form, pool),
|
|
||||||
fields(
|
|
||||||
email = %form.email,
|
|
||||||
name = %form.name
|
|
||||||
)
|
|
||||||
)]
|
|
||||||
pub async fn subscribe(
|
|
||||||
form: web::Form<FormData>,
|
|
||||||
pool: web::Data<PgPool>,
|
|
||||||
) -> Result<HttpResponse, HttpResponse> {
|
|
||||||
insert_subscriber(&pool, &form)
|
|
||||||
.await
|
|
||||||
.map_err(|_| HttpResponse::InternalServerError().finish())?;
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(
|
|
||||||
name = "Saving new subscriber details in the database",
|
|
||||||
skip(form, pool)
|
|
||||||
)]
|
|
||||||
pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> {
|
|
||||||
sqlx::query!(
|
|
||||||
r#"
|
|
||||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
|
||||||
VALUES ($1, $2, $3, $4)
|
|
||||||
"#,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
form.email,
|
|
||||||
form.name,
|
|
||||||
Utc::now()
|
|
||||||
)
|
|
||||||
.execute(pool)
|
|
||||||
.await
|
|
||||||
.map_err(|e| {
|
|
||||||
tracing::error!("Failed to execute query: {:?}", e);
|
|
||||||
e
|
|
||||||
})?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
use crate::routes::{health_check, subscribe};
|
|
||||||
use actix_web::dev::Server;
|
|
||||||
use actix_web::web::Data;
|
|
||||||
use actix_web::{web, App, HttpServer};
|
|
||||||
use sqlx::PgPool;
|
|
||||||
use std::net::TcpListener;
|
|
||||||
use tracing_actix_web::TracingLogger;
|
|
||||||
|
|
||||||
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
|
|
||||||
let db_pool = Data::new(db_pool);
|
|
||||||
let server = HttpServer::new(move || {
|
|
||||||
App::new()
|
|
||||||
.wrap(TracingLogger)
|
|
||||||
.route("/health_check", web::get().to(health_check))
|
|
||||||
.route("/subscriptions", web::post().to(subscribe))
|
|
||||||
.app_data(db_pool.clone())
|
|
||||||
})
|
|
||||||
.listen(listener)?
|
|
||||||
.run();
|
|
||||||
Ok(server)
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
use tracing::subscriber::set_global_default;
|
|
||||||
use tracing::Subscriber;
|
|
||||||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
|
||||||
use tracing_log::LogTracer;
|
|
||||||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
|
||||||
|
|
||||||
/// Compose multiple layers into a `tracing`'s subscriber.
|
|
||||||
///
|
|
||||||
/// # Implementation Notes
|
|
||||||
///
|
|
||||||
/// We are using `impl Subscriber` as return type to avoid having to spell out the actual
|
|
||||||
/// type of the returned subscriber, which is indeed quite complex.
|
|
||||||
pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sync + Send {
|
|
||||||
let env_filter =
|
|
||||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
|
|
||||||
let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout);
|
|
||||||
Registry::default()
|
|
||||||
.with(env_filter)
|
|
||||||
.with(JsonStorageLayer)
|
|
||||||
.with(formatting_layer)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register a subscriber as global default to process span data.
|
|
||||||
///
|
|
||||||
/// It should only be called once!
|
|
||||||
pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) {
|
|
||||||
LogTracer::init().expect("Failed to set logger");
|
|
||||||
set_global_default(subscriber).expect("Failed to set subscriber");
|
|
||||||
}
|
|
|
@ -6,10 +6,10 @@ region: fra
|
||||||
services:
|
services:
|
||||||
- name: zero2prod
|
- name: zero2prod
|
||||||
# Relative to the repository root
|
# Relative to the repository root
|
||||||
dockerfile_path: chapter05/Dockerfile
|
dockerfile_path: Dockerfile
|
||||||
source_dir: chapter05
|
source_dir: .
|
||||||
github:
|
github:
|
||||||
branch: ch-05
|
branch: master
|
||||||
deploy_on_push: true
|
deploy_on_push: true
|
||||||
repo: LukeMathWalker/zero-to-production
|
repo: LukeMathWalker/zero-to-production
|
||||||
# Active probe used by DigitalOcean's to ensure our application is healthy
|
# Active probe used by DigitalOcean's to ensure our application is healthy
|
|
@ -1,8 +1,8 @@
|
||||||
use chapter05::configuration::get_configuration;
|
|
||||||
use chapter05::startup::run;
|
|
||||||
use chapter05::telemetry::{get_subscriber, init_subscriber};
|
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
|
use zero2prod::configuration::get_configuration;
|
||||||
|
use zero2prod::startup::run;
|
||||||
|
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
|
@ -1,9 +1,9 @@
|
||||||
use chapter05::configuration::{get_configuration, DatabaseSettings};
|
|
||||||
use chapter05::startup::run;
|
|
||||||
use chapter05::telemetry::{get_subscriber, init_subscriber};
|
|
||||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use zero2prod::configuration::{get_configuration, DatabaseSettings};
|
||||||
|
use zero2prod::startup::run;
|
||||||
|
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||||
|
|
||||||
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
Loading…
Reference in a new issue