Dependency injection supplies a component's required resources or services from outside it. In an Actix Web service, application state carries shared data and database connections into request handlers. section 4.3.3 section 3.2.2
A typical server setup proceeds as follows:
- Read DATABASE_URL, create a sqlx PostgreSQL connection pool, and place it in AppState. section 4.3.3 section 8.1
- Wrap AppState with web::Data and register the shared value on the Actix application. section 4.3.3 section 3.2.2
- Declare web::Data<AppState> in handler parameters so Actix makes the state available during request processing. section 4.3.3 section 3.2.2
Injecting a PostgreSQL pool
Define AppState with a db: PgPool field, construct it from the database URL, and configure the Actix application with the resulting shared state. Handlers can then accept web::Data<AppState> alongside path parameters and use the pool for database operations. section 8.1 section 4.3.3
Listing 4.3 The tutor web service’s main() function
use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
use sqlx::postgres::PgPool;
use std::env;
use std::io;
use std::sync::Mutex;
#[path = "../iter2/handlers.rs"]
mod handlers;
#[path = "../iter2/models.rs"]
mod models;
#[path = "../iter2/routes.rs"]
mod routes;
#[path = "../iter2/state.rs"]
mod state;
use routes::*;
use state::AppState;
#[actix_rt::main]
async fn main() -> io::Result<()> {
dotenv().ok(); #1
let database_url = env::var("DATABASE_URL").expect(
"DATABASE_URL is not set in .env file");
let db_pool = PgPool::connect(&database_url).await.unwrap(); #2
// Construct App State
let shared_data = web::Data::new(AppState {
health_check_response: "I'm good. You've already
asked me ".to_string(),
visit_count: Mutex::new(0),
db: db_pool,
});
//Construct app and configure routes
let app = move || {
App::new()
.app_data(shared_data.clone()) #3
.configure(general_routes)
.configure(course_routes)
};
//Start HTTP server
HttpServer::new(app).bind("127.0.0.1:3000")?.run().await
}
Trace the complete main function from environment loading and pool creation through AppState construction, application configuration, and server startup. section 4.3.3




















