-
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
Devesh Rawat
committed
Dec 11, 2023
1 parent
d2d7bf0
commit 35ca33b
Showing
10 changed files
with
145 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
pub mod configuration; | ||
pub mod routes; | ||
pub mod startup; | ||
pub mod telemetry; |
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,21 +1,23 @@ | ||
//! src/main.rs | ||
#![allow(non_snake_case)] | ||
use dotenv::dotenv; | ||
use env_logger::Env; | ||
use newsLetter::configuration::get_configuration; | ||
use newsLetter::startup::run; | ||
use sqlx::PgPool; | ||
use newsLetter::telemetry; | ||
use sqlx::postgres::PgPool; | ||
use std::net::TcpListener; | ||
|
||
#[tokio::main] | ||
async fn main() -> std::io::Result<()> { | ||
dotenv().ok(); | ||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); | ||
let subscriber = telemetry::get_subscriber("newsLetter".into(), "info".into(), std::io::stdout); | ||
telemetry::init_subscriber(subscriber); | ||
let configuration = get_configuration().expect("Failed to read configuration."); | ||
let connenction = PgPool::connect(&configuration.database.connection_string()) | ||
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, connenction)?.await | ||
let listener = TcpListener::bind(address)?; | ||
run(listener, connection_pool)?.await?; | ||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//!src/telemetry.rs | ||
use tracing::{subscriber::set_global_default, Subscriber}; | ||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; | ||
use tracing_log::LogTracer; | ||
use tracing_subscriber::fmt::MakeWriter; | ||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; | ||
|
||
pub fn get_subscriber<Sink>( | ||
name: String, | ||
env_filter: String, | ||
sink: Sink, | ||
) -> impl Subscriber + Sync + Send | ||
where | ||
Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static, | ||
{ | ||
let env_filter = | ||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter)); | ||
let formatting_layer = BunyanFormattingLayer::new(name, sink); | ||
Registry::default() | ||
.with(env_filter) | ||
.with(JsonStorageLayer) | ||
.with(formatting_layer) | ||
} | ||
|
||
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) { | ||
LogTracer::init().expect("Failed to set logger"); | ||
set_global_default(subscriber).expect("Failed to set subscriber"); | ||
} |
Oops, something went wrong.