-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
12 changed files
with
799 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters