Skip to content

Commit

Permalink
modified: Cargo.lock
Browse files Browse the repository at this point in the history
    modified:   Cargo.toml
	new file:   migrations/20231123123813_create_subscriptions_table.sql
	new file:   scripts/init_db.sh
	new file:   src/configuration.rs
	modified:   src/lib.rs
	modified:   src/main.rs
	new file:   src/routes/health_check.rs
	new file:   src/routes/mod.rs
	new file:   src/routes/subscriptions.rs
	new file:   src/startup.rs
	modified:   tests/health_check.rs
  • Loading branch information
Devesh Rawat committed Nov 23, 2023
1 parent cfcecbf commit f52b579
Show file tree
Hide file tree
Showing 12 changed files with 799 additions and 28 deletions.
665 changes: 655 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ name="newsLetter"

[dependencies]
actix-web = "4.4.0"
serde = { version = "1.0.193", features = ["derive"] }
sqlx = { version = "0.5.13", features = ["runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
tokio = {version ="1.34.0", features = ["macros", "rt-multi-thread"]}



[dev-dependencies]
reqwest = "0.11.22"

Expand Down
8 changes: 8 additions & 0 deletions migrations/20231123123813_create_subscriptions_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Add migration script here
CREATE TABLE subscriptions(
id uuid NOT NULL,
PRIMARY KEY (id),
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
subscribed_at timestamptz NOT NULL
);
44 changes: 44 additions & 0 deletions scripts/init_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

set -eo pipefail

check_command() {
if ! command -v "$1" &> /dev/null; then
echo >&2 "Error: $1 is not installed."
exit 1
fi
}

check_command "psql"
check_command "sqlx"

DB_USER=${POSTGRES_USER:=postgres}
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
DB_NAME="${POSTGRES_DB:=newsletter}"
DB_PORT="${POSTGRES_PORT:=5432}"

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 "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!"
Empty file added src/configuration.rs
Empty file.
17 changes: 3 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
//! src/lib.rs
#![allow(non_snake_case)]
use actix_web::{dev::Server, 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)
}
pub mod configuration;
pub mod routes;
pub mod startup;
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! src/main.rs
#![allow(non_snake_case)]
use newsLetter::run;
use newsLetter::startup::run;
use std::net::TcpListener;

#[tokio::main]
async fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
println!("http://127.0.0.1:{}", port);
println!("listening on http://127.0.0.1:{}", port);
run(listener)?.await
}
4 changes: 4 additions & 0 deletions src/routes/health_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use actix_web::HttpResponse;
pub async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
7 changes: 7 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! src/routes/mod.rs
mod health_check;
mod subscriptions;

pub use health_check::*;
pub use subscriptions::*;
12 changes: 12 additions & 0 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use actix_web::{web, HttpResponse};
use serde::Deserialize;

#[derive(Deserialize)]
pub struct FormData {
pub email: String,
pub name: String,
}

pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}
15 changes: 15 additions & 0 deletions src/startup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use actix_web::{dev::Server, web, App, HttpServer};
use std::net::TcpListener;

use crate::routes::{health_check, subscribe};

pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
})
.listen(listener)?
.run();
Ok(server)
}
48 changes: 46 additions & 2 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! tests/health_check.rs
use std::net::TcpListener;

use tokio;
// `tokio::test` is the testing equivalent of `tokio::main`.
// `cargo expand --test health_check` (<- name of the test file)

fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let server = newsLetter::run(listener).expect("Failed to bind address");
let server = newsLetter::startup::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}
Expand All @@ -26,3 +25,48 @@ async fn health_check_works() {
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}

#[tokio::test]
async fn subscribe_returns_a_200_for_valid_form_data() {
let app_address = spawn_app();
let client = reqwest::Client::new();

let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
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_eq!(200, response.status().as_u16());
}

#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
let app_address = spawn_app();
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", &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(),
"The API did not fail with 400 Bad Request when the payload was {}.",
error_message
);
}
}

0 comments on commit f52b579

Please sign in to comment.