-
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.
Add PgPool connection, logic to subscribe and test isolation
- Loading branch information
Showing
7 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,13 +1,17 @@ | ||
use std::net::TcpListener; | ||
|
||
use sqlx::PgPool; | ||
use zero2prod::configuration::get_configuration; | ||
use zero2prod::startup::run; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
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).expect("Failed to bind address"); | ||
run(listener)?.await | ||
run(listener, connection_pool)?.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 |
---|---|---|
@@ -1,12 +1,33 @@ | ||
use actix_web::{web, HttpResponse}; | ||
use chrono::Utc; | ||
use serde::Deserialize; | ||
use sqlx::PgPool; | ||
use uuid::Uuid; | ||
|
||
#[derive(Deserialize)] | ||
pub struct SubscriptionData { | ||
pub struct FormData { | ||
name: String, | ||
email: String, | ||
} | ||
|
||
pub async fn subscribe(_params: web::Form<SubscriptionData>) -> HttpResponse { | ||
HttpResponse::Ok().finish() | ||
pub async fn subscribe(form: web::Form<FormData>, pool: web::Data<PgPool>) -> HttpResponse { | ||
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 | ||
{ | ||
Ok(_) => HttpResponse::Ok().finish(), | ||
Err(e) => { | ||
println!("Failed to execute query: {}", e); | ||
HttpResponse::InternalServerError().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
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