1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-03-16 18:32:43 +01:00

99 lines
2.7 KiB
Markdown
Raw Normal View History

2017-11-27 16:41:37 -08:00
# Getting Started
Lets create and run our first actix web application. Well create a new Cargo project
that depends on actix web and then run the application.
2018-03-28 22:16:01 +02:00
In the previous section we already installed the required rust version. Now let's create new cargo projects.
2017-11-27 16:41:37 -08:00
2018-03-28 22:16:01 +02:00
## Hello, world!
2017-11-27 16:41:37 -08:00
Lets 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
```
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-27 16:41:37 -08:00
contains the following:
```toml
[dependencies]
2018-02-27 23:31:43 -08:00
actix = "0.5"
actix-web = "0.4"
2017-11-27 16:41:37 -08:00
```
In order to implement a web server, first we need to create a request handler.
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-28 18:00:10 -08:00
and returns a type that can be converted into `HttpResponse`:
2017-11-27 16:41:37 -08:00
2017-12-04 13:32:05 -08:00
```rust
# extern crate actix_web;
# use actix_web::*;
fn index(req: HttpRequest) -> &'static str {
"Hello world!"
}
# fn main() {}
2017-11-27 16:41:37 -08: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 13:32:05 -08:00
```rust
# extern crate actix_web;
# use actix_web::*;
# fn index(req: HttpRequest) -> &'static str {
# "Hello world!"
# }
# fn main() {
2018-03-31 00:16:55 -07:00
App::new()
2017-12-25 08:19:33 -08:00
.resource("/", |r| r.f(index));
2017-12-04 13:32:05 -08:00
# }
2017-11-27 16:41:37 -08: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-27 16:41:37 -08:00
```rust,ignore
2017-12-25 08:19:33 -08:00
HttpServer::new(
2018-03-31 00:16:55 -07:00
|| App::new()
2017-12-25 08:19:33 -08:00
.resource("/", |r| r.f(index)))
2017-12-19 18:44:17 -08:00
.bind("127.0.0.1:8088")?
2018-01-05 16:32:36 -08:00
.run();
2017-11-27 16:41:37 -08:00
```
2018-03-28 22:16:01 +02:00
That's it. Now, compile and run the program with `cargo run`.
2017-11-27 16:41:37 -08:00
Head over to ``http://localhost:8088/`` to see the results.
Here is full source of main.rs file:
2018-01-09 10:08:06 -08:00
```rust
# use std::thread;
2018-02-14 10:37:12 +13:00
extern crate actix_web;
2018-03-31 00:16:55 -07:00
use actix_web::{App, HttpRequest, HttpResponse, HttpServer};
2017-11-27 16:41:37 -08:00
2017-11-28 13:52:53 -08:00
fn index(req: HttpRequest) -> &'static str {
"Hello world!"
2017-11-27 16:41:37 -08:00
}
fn main() {
2018-02-14 10:37:12 +13:00
# // In the doctest suite we can't run blocking code - deliberately leak a thread
# // If copying this example in show-all mode make sure you skip the thread spawn
# // call.
# thread::spawn(|| {
2017-11-27 16:41:37 -08:00
HttpServer::new(
2018-03-31 00:16:55 -07:00
|| App::new()
.resource("/", |r| r.f(index)))
2017-12-25 08:19:33 -08:00
.bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088")
2018-01-05 16:32:36 -08:00
.run();
2018-02-14 10:37:12 +13:00
# });
2017-11-27 16:41:37 -08:00
}
```
2017-11-27 19:56:14 -08:00
2018-03-28 22:16:01 +02:00
Note on the `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 a
2017-12-19 18:44:17 -08:00
properly configured actix system. For more information please check
2017-11-27 19:56:14 -08:00
[actix documentation](https://actix.github.io/actix/actix/)