Skip to content

Commit

Permalink
Feat/migrations (#4)
Browse files Browse the repository at this point in the history
* Added migrations

* Added configuration.yaml

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Updated tests

* Added logs

* Added logs
  • Loading branch information
tapiocaboy authored Oct 24, 2024
1 parent c017df4 commit a548966
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ config = "0.14.0"
uuid = { version="1.10.0", features = ["v4"] }
chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
serde_json = "1.0.128"
env_logger = "0.11.5"
log = "0.4.22"
tracing = { version = "0.1.40", features = ["log"] }
[dev-dependencies]
reqwest = { version = "0.12.7", features = ["json"] }

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::net::TcpListener;
use isotopes::configuration::get_configuration;
use sqlx::PgPool;
use isotopes::startup::run;
use env_logger::Env;

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
// let address = "127.0.0.1:4000";

env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let configuration = get_configuration()
.expect("Failed to read configuration.");

Expand Down
27 changes: 23 additions & 4 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::{web, HttpResponse};
use sqlx::{ PgPool};
use chrono::Utc;
use tracing::Instrument;
use uuid::Uuid;

#[derive(serde::Deserialize)]
Expand All @@ -12,6 +13,21 @@ pub async fn subscribe(
form: web::Form<FormData>,
pool: web::Data<PgPool>
) -> HttpResponse {

let request_id = Uuid::new_v4();

let request_span = tracing::info_span!(
"Adding a new subscriber",
%request_id,
subscriber_email = %form.email,
subscriber_name = %form.name
);

let _request_span_guard = request_span.enter();

tracing::info!("request_id {} - Adding new subscriber: {} - {}",
request_id, form.email, form.name);
let query_span = tracing::info_span!("Saving new subscriber details in the database");
match sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
Expand All @@ -21,11 +37,14 @@ pub async fn subscribe(
form.email,
form.name,
Utc::now()
).execute(pool.get_ref()).await {
Ok(_) => HttpResponse::Ok().finish(),
).execute(pool.get_ref())
.instrument(query_span)
.await {
Ok(_) => {
tracing::info!("request_id {} - New subscriber added: {} - {}", request_id, form.email, form.name);
HttpResponse::Ok().finish()},
Err(e) => {

println!("Failed to execute query: {}", e);
tracing::error!("request_id {} - Failed to execute query: {:?}", request_id, e);
HttpResponse::InternalServerError().finish()

}
Expand Down
2 changes: 2 additions & 0 deletions src/startup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::routes::{health_check, subscribe};
use actix_web::dev::Server;
use actix_web::{web, App, HttpServer};
use actix_web::middleware::Logger;


use sqlx::PgPool;
Expand All @@ -12,6 +13,7 @@ pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Er
// Capture `connection` from the surrounding environment
let server = HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
// Get a pointer copy and attach it to the application state
Expand Down

0 comments on commit a548966

Please sign in to comment.