Skip to main content

Running the Application

Executing and configuring the server run loop.

Actix 4.9+ 3 min read

# Running the Application

Once you've configured your routes and your HttpServer, you must run it. In Actix Web, this is done by calling .run() and .awaiting the result within the #[actix_web::main] context.

##The actix_web::main macro

Rust's main function cannot be async by default. To execute an async runtime, you need an executor. Actix Web provides a custom executor macro, #[actix_web::main], which sets up a Tokio-based runtime optimized for Actix Web's architecture.

##Graceful Shutdown

When running a production server, it's crucial to handle shutdowns gracefully. Actix Web's HttpServer automatically listens for standard OS signals (like SIGINT and SIGTERM). When it receives one, it will stop accepting new connections and give existing connections time to finish processing before exiting completely.

You can configure the shutdown timeout using .shutdown_timeout(seconds):

code_blocksrust
HttpServer::new(|| {
    App::new().service(hello)
})
.bind("127.0.0.1:8080")?
.shutdown_timeout(30) // Wait up to 30 seconds for active requests to finish
.run()
.await

##Hot Reloading in Development

While developing, it can be tedious to manually stop and start the server every time you make a change. A common pattern in the Rust ecosystem is to use cargo-watch.

Install it:

terminalbash
cargo install cargo-watch

Then run your application with:

terminalbash
cargo watch -x run

This will automatically recompile and restart your Actix Web server whenever you save a Rust file.

Was this page helpful?

Reference Implementation

The following code snippet demonstrates the authoritative pattern for this documentation.

src/main.rs
1use actix_web::{get, App, HttpServer, Responder};
2
3#[get("/")]
4async fn index() -> impl Responder {
5 "Running smoothly!"
6}
7
8#[actix_web::main]
9async fn main() -> std::io::Result<()> {
10 println!("Starting server on 127.0.0.1:8080");
11 HttpServer::new(|| App::new().service(index))
12 .bind("127.0.0.1:8080")?
13 .shutdown_timeout(10)
14 .run()
15 .await
16}
Was this page helpful?