I'm learning the Actix framework. The documentation has the sample:
use actix_rt::System;
use actix_web::{web, App, HttpResponse, HttpServer};
use std::sync::mpsc;
use std::thread;
#[actix_rt::main]
async fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let sys = System::new("http-server");
let srv = HttpServer::new(|| App::new().route("/", web::get().to(|| HttpResponse::Ok())))
.bind("127.0.0.1:8088")?
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.run();
let _ = tx.send(srv);
sys.run()
});
let srv = rx.recv().unwrap();
// pause accepting new connections
srv.pause().await;
// resume accepting new connections
srv.resume().await;
// stop server
srv.stop(true).await;
}
I have no error after compiling this code:
But I can't open the page in my browser:
What have I missed and why does the page not open in my browser?
This section is an example of how to control the server running in the thread created previously. You can pause, resume, and stop the server gracefully. These lines do those three actions. At the end, the server is stopped.
let srv = rx.recv().unwrap();
// pause accepting new connections
srv.pause().await;
// resume accepting new connections
srv.resume().await;
// stop server
srv.stop(true).await;
That makes this example a server that shuts itself off at the end of the snippet. One small change to get this snippet to run indefinitely is to make this change:
let srv = rx.recv().unwrap();
// wait for any incoming connections
srv.await;
which is not something I'd recommend. There are other examples, particularly at the actix/examples repository, that would likely be more appropriate to get you started on how to structure an actix server.