2017-11-28 01:41:37 +01:00
|
|
|
|
# Getting Started
|
|
|
|
|
|
|
|
|
|
Let’s create and run our first actix web application. We’ll create a new Cargo project
|
|
|
|
|
that depends on actix web and then run the application.
|
|
|
|
|
|
|
|
|
|
In previous section we already installed required rust version. Now let's create new cargo projects.
|
|
|
|
|
|
|
|
|
|
## Hello, world!
|
|
|
|
|
|
|
|
|
|
Let’s write our first actix web application! Start by creating a new binary-based
|
|
|
|
|
Cargo project and changing into the new directory:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cargo new hello-world --bin
|
|
|
|
|
cd hello-world
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Now, add actix and actix web as dependencies of your project by ensuring your Cargo.toml
|
|
|
|
|
contains the following:
|
|
|
|
|
|
|
|
|
|
```toml
|
|
|
|
|
[dependencies]
|
2018-01-12 21:54:57 +01:00
|
|
|
|
actix = "0.4"
|
|
|
|
|
actix-web = "0.3"
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In order to implement a web server, first we need to create a request handler.
|
|
|
|
|
|
|
|
|
|
A request handler is a function that accepts a `HttpRequest` instance as its only parameter
|
2017-11-29 03:00:10 +01:00
|
|
|
|
and returns a type that can be converted into `HttpResponse`:
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
|
```rust
|
|
|
|
|
# extern crate actix_web;
|
|
|
|
|
# use actix_web::*;
|
|
|
|
|
fn index(req: HttpRequest) -> &'static str {
|
|
|
|
|
"Hello world!"
|
|
|
|
|
}
|
|
|
|
|
# fn main() {}
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Next, create an `Application` instance and register the
|
|
|
|
|
request handler with the application's `resource` on a particular *HTTP method* and *path*::
|
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
|
```rust
|
|
|
|
|
# extern crate actix_web;
|
|
|
|
|
# use actix_web::*;
|
|
|
|
|
# fn index(req: HttpRequest) -> &'static str {
|
|
|
|
|
# "Hello world!"
|
|
|
|
|
# }
|
|
|
|
|
# fn main() {
|
2017-12-25 17:19:33 +01:00
|
|
|
|
Application::new()
|
|
|
|
|
.resource("/", |r| r.f(index));
|
2017-12-04 22:32:05 +01:00
|
|
|
|
# }
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
After that, application instance can be used with `HttpServer` to listen for incoming
|
2017-12-12 16:40:36 +01:00
|
|
|
|
connections. Server accepts function that should return `HttpHandler` instance:
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
|
|
|
|
```rust,ignore
|
2017-12-25 17:19:33 +01:00
|
|
|
|
HttpServer::new(
|
|
|
|
|
|| Application::new()
|
|
|
|
|
.resource("/", |r| r.f(index)))
|
2017-12-20 03:44:17 +01:00
|
|
|
|
.bind("127.0.0.1:8088")?
|
2018-01-06 01:32:36 +01:00
|
|
|
|
.run();
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
That's it. Now, compile and run the program with cargo run.
|
|
|
|
|
Head over to ``http://localhost:8088/`` to see the results.
|
|
|
|
|
|
|
|
|
|
Here is full source of main.rs file:
|
|
|
|
|
|
2018-01-09 19:08:06 +01:00
|
|
|
|
```rust
|
|
|
|
|
# use std::thread;
|
2018-01-06 01:32:36 +01:00
|
|
|
|
# extern crate actix_web;
|
2017-11-29 04:49:17 +01:00
|
|
|
|
use actix_web::*;
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
|
fn index(req: HttpRequest) -> &'static str {
|
|
|
|
|
"Hello world!"
|
2017-11-28 01:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2018-02-12 11:52:03 +01:00
|
|
|
|
# let child = thread::spawn(|| {
|
2017-11-28 01:41:37 +01:00
|
|
|
|
HttpServer::new(
|
2017-12-12 16:40:36 +01:00
|
|
|
|
|| Application::new()
|
2017-12-06 17:03:08 +01:00
|
|
|
|
.resource("/", |r| r.f(index)))
|
2017-12-25 17:19:33 +01:00
|
|
|
|
.bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088")
|
2018-01-06 01:32:36 +01:00
|
|
|
|
.run();
|
2018-02-12 11:52:03 +01:00
|
|
|
|
});
|
|
|
|
|
# child.join().expect("failed to join server thread");
|
2017-11-28 01:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
```
|
2017-11-28 04:56:14 +01:00
|
|
|
|
|
|
|
|
|
Note on `actix` crate. Actix web framework is built on top of actix actor library.
|
|
|
|
|
`actix::System` initializes actor system, `HttpServer` is an actor and must run within
|
2017-12-20 03:44:17 +01:00
|
|
|
|
properly configured actix system. For more information please check
|
2017-11-28 04:56:14 +01:00
|
|
|
|
[actix documentation](https://actix.github.io/actix/actix/)
|