feat(log): rudimentary logging
- Changed route `subscribe` into `subscriptions`
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
use std::net::TcpListener;
|
||||
|
||||
use email_newsletter_api::{configuration::get_configuration, startup};
|
||||
use env_logger::Env;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let configuration = get_configuration().expect("Failed to read configuration");
|
||||
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||
.await
|
||||
.expect("Failed to connect to PostgreSQL");
|
||||
|
@@ -13,6 +13,15 @@ pub async fn subscribe_route(
|
||||
form: web::Form<FormData>,
|
||||
db_conn_pool: web::Data<PgPool>,
|
||||
) -> HttpResponse {
|
||||
let request_id = Uuid::new_v4();
|
||||
|
||||
log::info!(
|
||||
"request_id {} - Saving '{}' '{}' as a new subscriber in PostgreSQL",
|
||||
request_id,
|
||||
form.name,
|
||||
form.email
|
||||
);
|
||||
|
||||
match sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
||||
@@ -26,9 +35,19 @@ pub async fn subscribe_route(
|
||||
.execute(db_conn_pool.get_ref())
|
||||
.await
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(e) => {
|
||||
println!("Failed to execute query: {}", e);
|
||||
Ok(_) => {
|
||||
log::info!(
|
||||
"request_id {} - Saved new subscriber details in PostgreSQL",
|
||||
request_id
|
||||
);
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
Err(err) => {
|
||||
log::info!(
|
||||
"request_id {} - Failed to execute query: {:?}",
|
||||
request_id,
|
||||
err
|
||||
);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
use crate::routes::{healthcheck_route, subscribe_route};
|
||||
use actix_web::dev::Server;
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use sqlx::PgPool;
|
||||
use std::net::TcpListener;
|
||||
@@ -11,8 +12,9 @@ pub fn run(listener: TcpListener, db_conn_pool: PgPool) -> Result<Server, std::i
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.route("/health_check", web::get().to(healthcheck_route))
|
||||
.route("/subscribe", web::post().to(subscribe_route))
|
||||
.route("/subscriptions", web::post().to(subscribe_route))
|
||||
.app_data(db_conn_pool.clone())
|
||||
})
|
||||
.listen(listener)?
|
||||
|
Reference in New Issue
Block a user