Skip to content

Commit 437cc5f

Browse files
committed
chore: added database and automate stuff
1 parent 88db4fe commit 437cc5f

12 files changed

+696
-35
lines changed

Diff for: Cargo.lock

+586-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,17 @@ actix-web = "4"
1717
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
1818
serde = {version = "1.0.163", features = ["derive"]}
1919

20+
[dependencies.sqlx]
21+
version = "0.6.3"
22+
default-features = false
23+
features = [
24+
"runtime-actix-rustls",
25+
"macros",
26+
"postgres",
27+
"uuid",
28+
"chrono",
29+
"migrate"
30+
]
31+
2032
[dev-dependencies]
2133
reqwest = "0.11.18"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Add migration script here
2+
-- Create Subscriptions Table
3+
CREATE TABLE subscriptions(
4+
id uuid NOT NULL,
5+
PRIMARY KEY (id),
6+
email TEXT NOT NULL UNIQUE,
7+
name TEXT NOT NULL,
8+
subscribed_at timestamptz NOT NULL
9+
);

Diff for: scripts/init_db.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
set -eo pipefail
4+
5+
if ! [ -x "$(command -v psql)" ]; then
6+
echo >&2 "Error: psql is not installed."
7+
exit 1
8+
fi
9+
if ! [ -x "$(command -v sqlx)" ]; then
10+
echo >&2 "Error: sqlx is not installed."
11+
echo >&2 "Use:"
12+
echo >&2 " cargo install --version=0.5.7 sqlx-cli --no-default-features --features postgres"
13+
echo >&2 "to install it."
14+
exit 1
15+
fi
16+
17+
# Check if a custom user has been set, otherwise default to 'postgres'
18+
DB_USER=${POSTGRES_USER:=postgres}
19+
# Check if a custom password has been set, otherwise default to 'password'
20+
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
21+
# Check if a custom database name has been set, otherwise default to 'newsletter'
22+
DB_NAME="${POSTGRES_DB:=newsletter}"
23+
# Check if a custom port has been set, otherwise default to '5432'
24+
DB_PORT="${POSTGRES_PORT:=5432}"
25+
# Launch postgres using Docker
26+
docker run \
27+
-e POSTGRES_USER=${DB_USER} \
28+
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
29+
-e POSTGRES_DB=${DB_NAME} \
30+
-p "${DB_PORT}":5432 \
31+
-d postgres \
32+
postgres -N 1000
33+
# ^ Increased maximum number of connections for testing purposes
34+
# Keep pinging Postgres until it's ready to accept commands
35+
export PGPASSWORD="${DB_PASSWORD}"
36+
until psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
37+
>&2 echo "Postgres is still unavailable - sleeping"
38+
sleep 1
39+
done
40+
41+
>&2 echo "Postgres is up and running on port ${DB_PORT}!"
42+
43+
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}
44+
sqlx database create
45+
sqlx migrate run
46+
47+
>&2 echo "Postgres has been migrated, ready to go!"

Diff for: src/configuration.rs

Whitespace-only changes.

Diff for: src/lib.rs

+3-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
use actix_web::{dev::Server, web, App, HttpResponse, HttpServer};
2-
use std::net::TcpListener;
3-
4-
#[allow(dead_code)]
5-
#[derive(serde::Deserialize)]
6-
struct FormData {
7-
email: String,
8-
name: String,
9-
}
10-
11-
async fn health_check() -> HttpResponse {
12-
HttpResponse::Ok().into()
13-
}
14-
15-
async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
16-
HttpResponse::Ok().finish()
17-
}
18-
19-
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
20-
let server = HttpServer::new(|| {
21-
App::new()
22-
.route("/health_check", web::get().to(health_check))
23-
.route("/subscriptions", web::post().to(subscribe))
24-
})
25-
.listen(listener)?
26-
.run();
27-
Ok(server)
28-
}
1+
pub mod routes;
2+
pub mod configuration;
3+
pub mod startup;

Diff for: src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use robust_rust::run;
1+
use robust_rust::startup::run;
22
use std::net::TcpListener;
33

44
#[tokio::main]

Diff for: src/routes/health_check.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use actix_web::HttpResponse;
2+
3+
pub async fn health_check() -> HttpResponse {
4+
HttpResponse::Ok().into()
5+
}

Diff for: src/routes/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod health_check;
2+
mod subscriptions;
3+
4+
pub use health_check::*;
5+
pub use subscriptions::*;

Diff for: src/routes/subscriptions.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use actix_web::{web, HttpResponse};
2+
3+
#[allow(dead_code)]
4+
#[derive(serde::Deserialize)]
5+
pub struct FormData {
6+
email: String,
7+
name: String,
8+
}
9+
10+
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
11+
HttpResponse::Ok().finish()
12+
}

Diff for: src/startup.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use actix_web::{dev::Server, web, App, HttpServer};
2+
use std::net::TcpListener;
3+
use crate::routes::{health_check, subscribe};
4+
5+
6+
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
7+
let server = HttpServer::new(|| {
8+
App::new()
9+
.route("/health_check", web::get().to(health_check))
10+
.route("/subscriptions", web::post().to(subscribe))
11+
})
12+
.listen(listener)?
13+
.run();
14+
Ok(server)
15+
}

Diff for: tests/health_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use reqwest::Client;
2-
use robust_rust::run;
2+
use robust_rust::startup::run;
33
use std::net::TcpListener;
44

55
#[allow(clippy::let_underscore_future)]

0 commit comments

Comments
 (0)