feat(subscriptions): subscriptions routes + database + conf
continuous-integration/drone Build is failing Details

main
flavien 2022-11-12 15:28:35 +01:00
parent 34797f7342
commit 301e7f1e63
14 changed files with 374 additions and 16 deletions

22
.drone.yml Normal file
View File

@ -0,0 +1,22 @@
kind: pipeline
type: docker
name: default
steps:
- name: postgres.server
image: postgres
environment:
POSTGRES_PORT: 5432
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: newsletter
detach: true
- name: test
image: rust:1.65
environment:
POSTGRES_HOST: postgres.server
commands:
- SKIP_DOCKER=true ./scripts/init_db.sh
- cargo build --verbose --all
- cargo test --verbose --all

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.env

View File

@ -1,10 +1,43 @@
[package]
name = "zero_to_prod_in_rust"
name = "zero2prod"
version = "0.1.0"
authors = ["Flavien Henrion <flavien@coincoinmail.fr>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
# We could use any path here, but we are following the community convention
# We could specify a library name using the `name` field. If unspecified,
# cargo will default to `package.name`, which is what we want.
path = "src/lib.rs"
[[bin]]
path = "src/main.rs"
name = "zero2prod"
[dependencies]
actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"]}
validator = { version = "0.16", features = ["derive"] }
actix-web-validator = "5.0.1"
log = "0.4"
env_logger = "0.9.3"
config = "0.13"
uuid = { version = "1", features = ["v4"] }
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
dotenv = "0.15.0"
[dependencies.sqlx]
version = "0.6"
default-features = false
features = [
"runtime-tokio-rustls",
"macros",
"postgres",
"uuid",
"chrono",
"migrate"
]
[dev-dependencies]
reqwest = "0.11"

8
configuration.yaml Normal file
View File

@ -0,0 +1,8 @@
# configuration.yaml
application_port: 8000
database:
host: "127.0.0.1"
port: 5432
username: "postgres"
password: "password"
database_name: "newsletter"

View File

@ -0,0 +1,9 @@
-- Add migration script here
-- 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
);

52
scripts/init_db.sh Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env bash
set -x
set -eo pipefail
if ! [ -x "$(command -v psql)" ]; then
echo >&2 "Error: psql is not installed."
exit 1
fi
if ! [ -x "$(command -v sqlx)" ]; then
echo >&2 "Error: sqlx is not installed."
echo >&2 "Use:"
echo >&2 " cargo install --version='~0.6' sqlx-cli \
--no-default-features --features rustls,postgres"
echo >&2 "to install it."
exit 1
fi
DB_USER=${POSTGRES_USER:=postgres}
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
DB_NAME="${POSTGRES_DB:=newsletter}"
DB_PORT="${POSTGRES_PORT:=5432}"
POSTGRES_HOST="${POSTGRES_HOST:=localhost}"
if [[ -z "${SKIP_DOCKER}" ]]
then
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
fi
export PGPASSWORD="${DB_PASSWORD}"
until psql -h "${POSTGRES_HOST}" -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}!"
DB_SERVER_URL=postgres://${DB_USER}:${DB_PASSWORD}@${POSTGRES_HOST}:${DB_PORT}
DATABASE_URL=${DB_SERVER_URL}/${DB_NAME}
export DATABASE_URL
sqlx database create
sqlx migrate run
printf "DB_SERVER_URL=${DB_SERVER_URL}\n" > .env
printf "DATABASE_URL=${DATABASE_URL}\n" >> .env
>&2 echo "Postgres has been migrated, ready to go!"

64
src/configuration.rs Normal file
View File

@ -0,0 +1,64 @@
//! src/configuration.rs
use serde::Deserialize;
use sqlx::{PgPool, PgConnection, Connection, Executor};
use dotenv::dotenv;
use uuid::Uuid;
#[derive(Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
}
#[derive(Deserialize)]
pub struct DatabaseSettings {
pub username: String,
pub password: String,
pub port: u16,
pub host: String,
pub database_name: String,
}
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let settings = config::Config::builder()
.add_source(config::File::new(
"configuration.yaml",
config::FileFormat::Yaml,
))
.build()?;
settings.try_deserialize::<Settings>()
}
impl DatabaseSettings {
pub fn connection_string(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}",
self.username, self.password, self.host, self.port, self.database_name
)
}
}
pub async fn configure_test_database() -> PgPool {
dotenv().ok();
let database_name = Uuid::new_v4().to_string();
let db_server_url = std::env::var("DB_SERVER_URL").expect("DB_SERVER_URL must be set.");
let mut connection = PgConnection::connect(
&db_server_url
)
.await
.expect("Failed to connect to Postgres");
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, database_name).as_str())
.await
.expect("Failed to create database.");
let connection_pool = PgPool::connect(&format!("{}/{}", db_server_url, database_name))
.await
.expect("Failed to connect to Postgres.");
sqlx::migrate!("./migrations")
.run(&connection_pool)
.await
.expect("Failed to migrate the database");
connection_pool
}

4
src/lib.rs Normal file
View File

@ -0,0 +1,4 @@
//! src/lib.rs
pub mod configuration;
pub mod routes;
pub mod startup;

View File

@ -1,17 +1,16 @@
use actix_web::{App, HttpResponse, HttpServer, Responder, get};
#[get("/health_check")]
async fn health_check() -> impl Responder {
HttpResponse::Ok()
}
use std::net::TcpListener;
use sqlx::PgPool;
use zero2prod::configuration::get_configuration;
use zero2prod::startup::run;
#[tokio::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(health_check)
})
.bind("127.0.0.1:8000")?
.run()
env_logger::init();
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.");
let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await
}

View File

@ -0,0 +1,8 @@
use actix_web::{get, HttpResponse, Responder};
use log::info;
#[get("/health_check")]
pub async fn health_check() -> impl Responder {
info!("GET /health_check");
HttpResponse::Ok()
}

5
src/routes/mod.rs Normal file
View File

@ -0,0 +1,5 @@
//! src/routes/mod.rs
mod health_check;
mod subscriptions;
pub use health_check::*;
pub use subscriptions::*;

View File

@ -0,0 +1,43 @@
use actix_web::{post, HttpResponse, Responder, web};
use actix_web_validator::Form;
use log::info;
use serde::Deserialize;
use sqlx::PgPool;
use validator::Validate;
use chrono::Utc;
use uuid::Uuid;
/*
NOTES
- possible to use derive_more crate to easily derive Display
- possible to add custom validation function
*/
#[derive(Debug, Validate, Deserialize)]
pub struct FormData {
#[validate(email)]
email: String,
#[validate(length(min = 1, max = 32))]
name: String,
}
#[post("/subscriptions")]
pub async fn subscriptions(form: Form<FormData>, pool: web::Data<PgPool>) -> impl Responder {
info!("POST /subscriptions {:?}", form.0);
match 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.get_ref())
.await {
Err(e) => {
println!("Failed to execute query: {}", e);
HttpResponse::InternalServerError()
}
_ => HttpResponse::Created(),
}
}

17
src/startup.rs Normal file
View File

@ -0,0 +1,17 @@
use crate::routes::{health_check, subscriptions};
use actix_web::{dev::Server, 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 = web::Data::new(db_pool);
let server = HttpServer::new(move || {
App::new()
.service(health_check)
.service(subscriptions)
.app_data(db_pool.clone())
})
.listen(listener)?
.run();
Ok(server)
}

93
tests/tests.rs Normal file
View File

@ -0,0 +1,93 @@
use sqlx::PgPool;
use std::net::TcpListener;
use zero2prod::configuration::configure_test_database;
use zero2prod::startup::run;
pub struct TestApp {
pub address: String,
pub db_pool: PgPool,
}
async fn spawn_app() -> TestApp {
let connection_pool = configure_test_database().await;
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let address = format!("http://127.0.0.1:{}", port);
let server = run(listener, connection_pool.clone()).expect("Failed to bind address");
let _ = tokio::spawn(server);
TestApp {
address,
db_pool: connection_pool,
}
}
#[tokio::test]
async fn health_check_works() {
let test_app = spawn_app().await;
let client = reqwest::Client::new();
let response = client
.get(&format!("{}/health_check", &test_app.address))
.send()
.await
.expect("Failed to execute request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
#[tokio::test]
async fn subscribe_returns_a_201_for_valid_form_data() {
let test_app = spawn_app().await;
let client = reqwest::Client::new();
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
let response = client
.post(&format!("{}/subscriptions", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.");
assert_eq!(201, response.status().as_u16());
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
.fetch_one(&test_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");
}
#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
let test_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 {
let response = client
.post(&format!("{}/subscriptions", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request.");
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
);
}
}