2017-11-28 01:41:37 +01:00
|
|
|
|
# Getting Started
|
|
|
|
|
|
2018-04-06 01:21:29 +02:00
|
|
|
|
Let’s write our first actix web application!
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
|
## Hello, world!
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2018-04-06 01:21:29 +02:00
|
|
|
|
Start by creating a new binary-based Cargo project and changing into the new directory:
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cargo new hello-world --bin
|
|
|
|
|
cd hello-world
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
|
Now, add actix and actix web as dependencies of your project by ensuring your Cargo.toml
|
2017-11-28 01:41:37 +01:00
|
|
|
|
contains the following:
|
|
|
|
|
|
|
|
|
|
```toml
|
|
|
|
|
[dependencies]
|
2018-02-28 08:31:43 +01:00
|
|
|
|
actix = "0.5"
|
|
|
|
|
actix-web = "0.4"
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
2018-04-06 01:12:23 +02:00
|
|
|
|
In order to implement a web server, we first need to create a request handler.
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
|
A request handler is a function that accepts an `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
|
|
|
|
|
2018-04-06 01:21:29 +02:00
|
|
|
|
Filename: src/main.rs
|
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() {
|
2018-03-31 09:16:55 +02:00
|
|
|
|
App::new()
|
2017-12-25 17:19:33 +01:00
|
|
|
|
.resource("/", |r| r.f(index));
|
2017-12-04 22:32:05 +01:00
|
|
|
|
# }
|
2017-11-28 01:41:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
|
After that, the application instance can be used with `HttpServer` to listen for incoming
|
|
|
|
|
connections. The server accepts a function that should return an `HttpHandler` instance:
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
|
|
|
|
```rust,ignore
|
2017-12-25 17:19:33 +01:00
|
|
|
|
HttpServer::new(
|
2018-03-31 09:16:55 +02:00
|
|
|
|
|| App::new()
|
2017-12-25 17:19:33 +01:00
|
|
|
|
.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
|
|
|
|
```
|
|
|
|
|
|
2018-04-06 01:12:23 +02:00
|
|
|
|
That's it! Now, compile and run the program with `cargo run`.
|
2017-11-28 01:41:37 +01:00
|
|
|
|
Head over to ``http://localhost:8088/`` to see the results.
|
|
|
|
|
|
2018-04-06 01:21:29 +02:00
|
|
|
|
The full source of src/main.rs is listed below:
|
2017-11-28 01:41:37 +01:00
|
|
|
|
|
2018-01-09 19:08:06 +01:00
|
|
|
|
```rust
|
|
|
|
|
# use std::thread;
|
2018-02-13 22:37:12 +01:00
|
|
|
|
extern crate actix_web;
|
2018-04-06 17:40:11 +02:00
|
|
|
|
use actix_web::{server, App, HttpRequest, HttpResponse};
|
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-13 22:37:12 +01:00
|
|
|
|
# // In the doctest suite we can't run blocking code - deliberately leak a thread
|
2018-04-06 01:12:23 +02:00
|
|
|
|
# // If copying this example in show-all mode, make sure you skip the thread spawn
|
2018-02-13 22:37:12 +01:00
|
|
|
|
# // call.
|
|
|
|
|
# thread::spawn(|| {
|
2018-04-06 17:40:11 +02:00
|
|
|
|
server::HttpServer::new(
|
2018-03-31 09:16:55 +02:00
|
|
|
|
|| App::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-13 22:37:12 +01:00
|
|
|
|
# });
|
2017-11-28 01:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
```
|
2017-11-28 04:56:14 +01:00
|
|
|
|
|
2018-04-06 01:12:23 +02:00
|
|
|
|
> **Note**: actix web is built upon [actix](https://github.com/actix/actix),
|
|
|
|
|
> an [actor model](https://en.wikipedia.org/wiki/Actor_model) framework in Rust.
|
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
|
`actix::System` initializes actor system, `HttpServer` is an actor and must run within a
|
2018-04-06 01:12:23 +02:00
|
|
|
|
properly configured actix system.
|
|
|
|
|
|
|
|
|
|
> For more information, check out the [actix documentation](https://actix.github.io/actix/actix/)
|
|
|
|
|
> and [actix guide](https://actix.github.io/actix/guide/).
|