Starting a server

Consider the following code:

fn main() {
env_logger::init();
let sys = actix::System::new("router");
server::new(|| {
// Insert `App` declaration here
}).workers(1)
.bind("127.0.0.1:8080")
.unwrap()
.start();
let _ = sys.run();
}

We create a new server with the server::new method call that expects a closure to return the App instance. Before we create the App instance, we have to finish our server and run it. The workers method sets the number of threads to run actors.

You can choose not to set this value explicitly, and by default, it will be equal to the number of CPUs on the system. In many cases, it's the best possible value for performance.

The next call of the bind method binds the server's socket to an address. If it can't be bound to an address, the method returns Err, and we unwrap the result to halt a server if we can't start a server on a desired port. At the end, we call the start method to start the Server actor. It returns an Addr struct with an address that you can use to send messages to a Server actor instance.

Actually, the Server actor won't run until we call run the method of the System instance. Add this method call, and then we'll go on to look at creating an App instance in detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.55.193