-
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.
- Loading branch information
1 parent
131588a
commit a3ec731
Showing
6 changed files
with
91 additions
and
24 deletions.
There are no files selected for viewing
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,16 +1,23 @@ | ||
use std::net::TcpListener; | ||
use sqlx::{PgPool}; | ||
use newsletter_service::startup::run; | ||
use newsletter_service::configuration; | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
let configuration = configuration::get_configuration().expect("Failed to load configuration!"); | ||
|
||
let connection_string = configuration.database.connection_string(); | ||
|
||
let connection = PgPool::connect(&connection_string) | ||
.await | ||
.expect("Failed to connect to database"); | ||
|
||
let address = format!("127.0.0.1:{}", configuration.application_port); | ||
|
||
let listener = TcpListener::bind(address)?; | ||
|
||
println!("Server is running on 127.0.0.1:{}", listener.local_addr().unwrap().port()); | ||
|
||
run(listener)?.await | ||
run(listener, connection)?.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,11 +1,33 @@ | ||
use actix_web::{Responder, HttpResponse, web}; | ||
use actix_web::{HttpResponse, web}; | ||
use sqlx::PgPool; | ||
use sqlx::types::chrono::Utc; | ||
use uuid::Uuid; | ||
|
||
#[derive(serde::Deserialize)] | ||
pub struct FormData { | ||
email: String, | ||
name: String | ||
} | ||
|
||
pub async fn subscribe(_form: web::Form<FormData>) -> impl Responder { | ||
HttpResponse::Ok().finish() | ||
pub async fn subscribe(form: web::Form<FormData>, db_pool: web::Data<PgPool>) -> HttpResponse { | ||
let result = 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(db_pool.get_ref()) | ||
.await; | ||
|
||
match result { | ||
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