Complete Chapter 4
+ Add in Tracing Logging + Logs are JSON formatted with events + Tests are confirmed working
This commit is contained in:
+19
-2
@@ -1,15 +1,32 @@
|
||||
use mail_app::configuration::{get_configuration, DatabaseSettings};
|
||||
use mail_app::startup::run;
|
||||
use mail_app::telemetry::{get_subscriber, init_subscriber};
|
||||
use once_cell::sync::Lazy;
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::{Executor, PgPool};
|
||||
use std::net::TcpListener;
|
||||
use uuid::Uuid;
|
||||
|
||||
static TRACING: Lazy<()> = Lazy::new(|| {
|
||||
let default_filter_level = "info".to_string();
|
||||
let subscriber_name = "test".to_string();
|
||||
if std::env::var("TEST_LOG").is_ok() {
|
||||
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout);
|
||||
init_subscriber(subscriber);
|
||||
} else {
|
||||
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink);
|
||||
init_subscriber(subscriber);
|
||||
};
|
||||
});
|
||||
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub db_pool: PgPool,
|
||||
}
|
||||
|
||||
async fn spawn_app() -> TestApp {
|
||||
Lazy::force(&TRACING);
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port.");
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let address = format!("http://127.0.0.1:{}", port);
|
||||
@@ -30,7 +47,7 @@ async fn spawn_app() -> TestApp {
|
||||
|
||||
pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
||||
// Create Database
|
||||
let connection = PgPool::connect(&config.connection_string_without_db())
|
||||
let connection = PgPool::connect(&config.connection_string_without_db().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
connection
|
||||
@@ -39,7 +56,7 @@ pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
||||
.expect("Failed to create database.");
|
||||
|
||||
// Migrate Database
|
||||
let connection_pool = PgPool::connect(&config.connection_string())
|
||||
let connection_pool = PgPool::connect(&config.connection_string().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
sqlx::migrate!("./migrations")
|
||||
|
||||
Reference in New Issue
Block a user