End of 3.9

~ Move to PgPool over PgConnection
+ Add in SQL query
This commit is contained in:
Nick Bland 2024-03-12 16:07:18 +10:00
parent e8483d2d6b
commit 5835c02931
6 changed files with 657 additions and 338 deletions

View File

@ -19,4 +19,7 @@ rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]
# rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"]
[target.aarch64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/opt/homebrew/Cellar/llvm/16.0.6/bin/ld64.lld"]
rustflags = [
"-C",
"link-arg=-fuse-ld=/opt/homebrew/Cellar/llvm/17.0.6_1/bin/ld64.lld",
]

936
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,10 +14,12 @@ name = "mail_app"
actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"] }
config = "0.13"
config = { version = "0.13", default-features = false, features = ["yaml"] }
uuid = { version = "1", features = ["v4"] }
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
[dependencies.sqlx]
version = "0.6"
version = "0.7"
default-features = false
features = [
"runtime-tokio-rustls",
@ -25,8 +27,8 @@ features = [
"postgres",
"uuid",
"chrono",
"migrate"
"migrate",
]
[dev-dependencies]
reqwest = "0.11"
reqwest = { version = "0.11", features = ["json"] }

View File

@ -1,11 +1,15 @@
use mail_app::configuration::get_configuration;
use mail_app::startup::run;
use sqlx::PgPool;
use std::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let configuration = get_configuration().expect("Failed to read config");
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)?.await
run(listener, connection_pool)?.await
}

View File

@ -1,11 +1,32 @@
use actix_web::{web, HttpResponse};
use chrono::Utc;
use sqlx::PgPool;
use uuid::Uuid;
#[derive(serde::Deserialize)]
pub struct FormData {
// email: String,
// name: String,
email: String,
name: String,
}
pub async fn subscribe(_form: web::Form<FormData>) -> 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()
}
}
}

View File

@ -1,13 +1,16 @@
use crate::routes::{health_check, subscribe};
use actix_web::dev::Server;
use actix_web::{web, App, HttpServer};
use sqlx::PgPool;
use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
let connection = web::Data::new(db_pool);
let server = HttpServer::new(move || {
App::new()
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
.app_data(connection.clone())
})
.listen(listener)?
.run();