MCPcopy Create free account
hub / github.com/actix/examples / main

Function main

shutdown-server/src/main.rs:17–47  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15
16#[actix_web::main]
17async fn main() -> std::io::Result<()> {
18 env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
19
20 // create the stop handle container
21 let stop_handle = web::Data::new(StopHandle::default());
22
23 log::info!("starting HTTP server at http://localhost:8080");
24
25 // start server as normal but don't .await after .run() yet
26 let srv = HttpServer::new({
27 let stop_handle = stop_handle.clone();
28
29 move || {
30 // give the server a Sender in .data
31 App::new()
32 .app_data(stop_handle.clone())
33 .service(hello)
34 .service(stop)
35 .wrap(middleware::Logger::default())
36 }
37 })
38 .bind(("127.0.0.1", 8080))?
39 .workers(2)
40 .run();
41
42 // register the server handle with the stop handle
43 stop_handle.register(srv.handle());
44
45 // run server until stopped (either by ctrl-c or stop endpoint)
46 srv.await
47}
48
49#[derive(Default)]
50struct StopHandle {

Callers

nothing calls this directly

Calls 3

runMethod · 0.80
registerMethod · 0.80
handleMethod · 0.45

Tested by

no test coverage detected