zero-to-production/chapter03-1/src/main.rs

26 lines
1.1 KiB
Rust
Raw Normal View History

2020-08-25 21:19:53 +00:00
use anyhow::Context;
use chapter03_1::configuration::get_configuration;
use chapter03_1::startup::run;
use sqlx::postgres::PgPool;
use std::net::TcpListener;
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
let configuration = get_configuration().expect("Failed to read configuration.");
let connection_pool = PgPool::connect(&configuration.database.connection_string())
.await
.map_err(anyhow::Error::from)
.with_context(|| "Failed to connect to Postgres.")?;
// Here we choose to bind explicitly to localhost, 127.0.0.1, for security
// reasons. This binding may cause issues in some environments. For example,
// it causes connectivity issues running in WSL2, where you cannot reach the
// server when it is bound to WSL2's localhost interface. As a workaround,
// you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware
// of the security implications when you expose the server on all interfaces.
let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await?;
2020-08-25 21:19:53 +00:00
Ok(())
2020-08-23 10:34:29 +00:00
}