mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 05:56:35 +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::startup::build;
|
||||
use zero2prod::startup::Application;
|
||||
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||
|
||||
#[actix_web::main]
|
||||
|
@ -8,7 +8,7 @@ async fn main() -> std::io::Result<()> {
|
|||
init_subscriber(subscriber);
|
||||
|
||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
||||
let server = build(configuration).await?;
|
||||
server.await?;
|
||||
let application = Application::build(configuration).await?;
|
||||
application.run_until_stopped().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -9,27 +9,44 @@ use sqlx::PgPool;
|
|||
use std::net::TcpListener;
|
||||
use tracing_actix_web::TracingLogger;
|
||||
|
||||
pub async fn build(configuration: Settings) -> Result<Server, std::io::Error> {
|
||||
let connection_pool = get_connection_pool(&configuration.database)
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
pub struct Application {
|
||||
address: String,
|
||||
server: Server,
|
||||
}
|
||||
|
||||
let sender_email = configuration
|
||||
.email_client
|
||||
.sender()
|
||||
.expect("Invalid sender email address.");
|
||||
let email_client = EmailClient::new(
|
||||
configuration.email_client.base_url,
|
||||
sender_email,
|
||||
configuration.email_client.authorization_token,
|
||||
);
|
||||
impl Application {
|
||||
pub async fn build(configuration: Settings) -> Result<Self, std::io::Error> {
|
||||
let connection_pool = get_connection_pool(&configuration.database)
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
|
||||
let address = format!(
|
||||
"{}:{}",
|
||||
configuration.application.host, configuration.application.port
|
||||
);
|
||||
let listener = TcpListener::bind(address)?;
|
||||
run(listener, connection_pool, email_client)
|
||||
let sender_email = configuration
|
||||
.email_client
|
||||
.sender()
|
||||
.expect("Invalid sender email address.");
|
||||
let email_client = EmailClient::new(
|
||||
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> {
|
||||
|
@ -39,7 +56,7 @@ pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgP
|
|||
.await
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
fn run(
|
||||
listener: TcpListener,
|
||||
db_pool: PgPool,
|
||||
email_client: EmailClient,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use uuid::Uuid;
|
||||
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};
|
||||
|
||||
// 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;
|
||||
|
||||
// Launch the application as a background task
|
||||
let server = build(configuration.clone())
|
||||
let application = Application::build(configuration.clone())
|
||||
.await
|
||||
.expect("Failed to build application.");
|
||||
let _ = tokio::spawn(server);
|
||||
let address = application.address().to_owned();
|
||||
let _ = tokio::spawn(application.run_until_stopped());
|
||||
|
||||
TestApp {
|
||||
address: todo!(),
|
||||
address,
|
||||
db_pool: get_connection_pool(&configuration.database)
|
||||
.await
|
||||
.expect("Failed to connect to the database"),
|
||||
|
|
Loading…
Reference in a new issue