mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-15 21:21:00 +00:00
Add address.
This commit is contained in:
parent
55da2e2e2e
commit
e562b7df6f
3 changed files with 46 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
use zero2prod::configuration::get_configuration;
|
use zero2prod::configuration::get_configuration;
|
||||||
use zero2prod::startup::build;
|
use zero2prod::startup::Application;
|
||||||
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
@ -8,7 +8,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
init_subscriber(subscriber);
|
init_subscriber(subscriber);
|
||||||
|
|
||||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
let configuration = get_configuration().expect("Failed to read configuration.");
|
||||||
let server = build(configuration).await?;
|
let application = Application::build(configuration).await?;
|
||||||
server.await?;
|
application.run_until_stopped().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,27 +9,44 @@ use sqlx::PgPool;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use tracing_actix_web::TracingLogger;
|
use tracing_actix_web::TracingLogger;
|
||||||
|
|
||||||
pub async fn build(configuration: Settings) -> Result<Server, std::io::Error> {
|
pub struct Application {
|
||||||
let connection_pool = get_connection_pool(&configuration.database)
|
address: String,
|
||||||
.await
|
server: Server,
|
||||||
.expect("Failed to connect to Postgres.");
|
}
|
||||||
|
|
||||||
let sender_email = configuration
|
impl Application {
|
||||||
.email_client
|
pub async fn build(configuration: Settings) -> Result<Self, std::io::Error> {
|
||||||
.sender()
|
let connection_pool = get_connection_pool(&configuration.database)
|
||||||
.expect("Invalid sender email address.");
|
.await
|
||||||
let email_client = EmailClient::new(
|
.expect("Failed to connect to Postgres.");
|
||||||
configuration.email_client.base_url,
|
|
||||||
sender_email,
|
|
||||||
configuration.email_client.authorization_token,
|
|
||||||
);
|
|
||||||
|
|
||||||
let address = format!(
|
let sender_email = configuration
|
||||||
"{}:{}",
|
.email_client
|
||||||
configuration.application.host, configuration.application.port
|
.sender()
|
||||||
);
|
.expect("Invalid sender email address.");
|
||||||
let listener = TcpListener::bind(address)?;
|
let email_client = EmailClient::new(
|
||||||
run(listener, connection_pool, email_client)
|
configuration.email_client.base_url,
|
||||||
|
sender_email,
|
||||||
|
configuration.email_client.authorization_token,
|
||||||
|
);
|
||||||
|
|
||||||
|
let address = format!(
|
||||||
|
"{}:{}",
|
||||||
|
configuration.application.host, configuration.application.port
|
||||||
|
);
|
||||||
|
let listener = TcpListener::bind(&address)?;
|
||||||
|
let server = run(listener, connection_pool, email_client)?;
|
||||||
|
|
||||||
|
Ok(Self { address, server })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn address(&self) -> &str {
|
||||||
|
&self.address
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run_until_stopped(self) -> Result<(), std::io::Error> {
|
||||||
|
self.server.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgPool, sqlx::Error> {
|
pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgPool, sqlx::Error> {
|
||||||
|
@ -39,7 +56,7 @@ pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgP
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
fn run(
|
||||||
listener: TcpListener,
|
listener: TcpListener,
|
||||||
db_pool: PgPool,
|
db_pool: PgPool,
|
||||||
email_client: EmailClient,
|
email_client: EmailClient,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use zero2prod::configuration::{get_configuration, DatabaseSettings};
|
use zero2prod::configuration::{get_configuration, DatabaseSettings};
|
||||||
use zero2prod::startup::{build, get_connection_pool};
|
use zero2prod::startup::{get_connection_pool, Application};
|
||||||
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||||
|
|
||||||
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
||||||
|
@ -33,12 +33,14 @@ pub async fn spawn_app() -> TestApp {
|
||||||
configure_database(&configuration.database).await;
|
configure_database(&configuration.database).await;
|
||||||
|
|
||||||
// Launch the application as a background task
|
// Launch the application as a background task
|
||||||
let server = build(configuration.clone())
|
let application = Application::build(configuration.clone())
|
||||||
.await
|
.await
|
||||||
.expect("Failed to build application.");
|
.expect("Failed to build application.");
|
||||||
let _ = tokio::spawn(server);
|
let address = application.address().to_owned();
|
||||||
|
let _ = tokio::spawn(application.run_until_stopped());
|
||||||
|
|
||||||
TestApp {
|
TestApp {
|
||||||
address: todo!(),
|
address,
|
||||||
db_pool: get_connection_pool(&configuration.database)
|
db_pool: get_connection_pool(&configuration.database)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to connect to the database"),
|
.expect("Failed to connect to the database"),
|
||||||
|
|
Loading…
Reference in a new issue