2018-05-22 23:15:08 +02:00
|
|
|
|
---
|
|
|
|
|
title: Getting Started
|
|
|
|
|
menu: docs_basics
|
|
|
|
|
weight: 130
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Getting Started
|
|
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
|
Let’s write our first `actix-web` application!
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
|
|
## Hello, world!
|
|
|
|
|
|
|
|
|
|
Start by creating a new binary-based Cargo project and changing into the new directory:
|
|
|
|
|
|
|
|
|
|
```bash
|
2018-05-26 09:31:13 +02:00
|
|
|
|
cargo new hello-world
|
2018-05-22 23:15:08 +02:00
|
|
|
|
cd hello-world
|
|
|
|
|
```
|
|
|
|
|
|
2019-08-05 18:28:26 +02:00
|
|
|
|
Now, add `actix-web` as a dependency of your project by ensuring your `Cargo.toml`
|
2018-05-22 23:15:08 +02:00
|
|
|
|
contains the following:
|
|
|
|
|
|
2020-01-03 05:10:24 +01:00
|
|
|
|
```ini
|
|
|
|
|
[dependencies]
|
|
|
|
|
actix-web = "{{< actix-version "actix-web" >}}"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
If you want to use the `#[actix_rt::main]` macro, you have to add `actix-rt` to your dependency.
|
|
|
|
|
Now your `Cargo.toml` should look like following:
|
|
|
|
|
|
2018-05-22 23:15:08 +02:00
|
|
|
|
```ini
|
|
|
|
|
[dependencies]
|
|
|
|
|
actix-web = "{{< actix-version "actix-web" >}}"
|
2019-12-29 14:57:11 +01:00
|
|
|
|
actix-rt = "{{< actix-version "actix-rt" >}}"
|
2018-05-22 23:15:08 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In order to implement a web server, we first need to create a request handler.
|
|
|
|
|
|
2019-12-29 14:57:11 +01:00
|
|
|
|
A request handler is an async function that accepts zero or more parameters that can be
|
2019-06-20 00:00:31 +02:00
|
|
|
|
extracted from a request (ie, `impl FromRequest`) and returns a type that can be
|
|
|
|
|
converted into an `HttpResponse` (ie, `impl Responder`):
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
2018-05-23 23:25:51 +02:00
|
|
|
|
{{< include-example example="getting-started" section="setup" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
|
Next, create an `App` instance and register the request handler with the application's
|
2019-12-29 14:57:11 +01:00
|
|
|
|
`route` on a _path_ and with a particular _HTTP method_. After that, the application
|
2019-06-20 00:00:31 +02:00
|
|
|
|
instance can be used with `HttpServer` to listen for incoming connections. The server
|
|
|
|
|
accepts a function that should return an application factory.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
2018-05-23 23:25:51 +02:00
|
|
|
|
{{< include-example example="getting-started" section="main" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
|
|
That's it! Now, compile and run the program with `cargo run`.
|
2019-12-29 14:57:11 +01:00
|
|
|
|
Head over to `http://localhost:8088/` to see the results.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
2020-01-02 07:43:41 +01:00
|
|
|
|
**Note**: You could notice `#[actix_rt::main]` attribute macro. This
|
|
|
|
|
macro executes marked async function in actix runtime. Any async function
|
|
|
|
|
could be marked and executed by this macro.
|
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
### Using Attribute Macros to Define Routes
|
2019-08-05 18:32:00 +02:00
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
Alternatively, you can define routes using macro attributes which
|
2019-08-06 09:52:22 +02:00
|
|
|
|
allow you to specify the routes above your functions like so:
|
2019-08-05 18:32:00 +02:00
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
{{< include-example example="getting-started" section="macro-attributes">}}
|
2019-08-05 18:32:00 +02:00
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
You can then register the route using `service()`:
|
2019-08-05 18:32:00 +02:00
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
App::new()
|
|
|
|
|
.service(index3)
|
|
|
|
|
```
|
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
For consistency reasons, this documentation only uses the explicit syntax shown at the
|
2019-08-05 18:32:00 +02:00
|
|
|
|
beginning of this page. However, if you prefer this syntax you should feel free to
|
|
|
|
|
use it any time you declare a route as it's only syntactic sugar.
|
|
|
|
|
|
2019-08-06 01:30:50 +02:00
|
|
|
|
To learn more, see [actix-web-codegen].
|
|
|
|
|
|
2019-08-05 17:29:01 +02:00
|
|
|
|
### Auto-reloading
|
|
|
|
|
|
|
|
|
|
If you want, you can have an automatically reloading server during development
|
|
|
|
|
that recompiles on demand. This isn't necessary, but it makes rapid prototyping
|
|
|
|
|
more convenient as you can see changes instantly upon saving.
|
|
|
|
|
To see how this can be accomplished, have a look at the [autoreload pattern][autoload].
|
2019-06-25 05:36:32 +02:00
|
|
|
|
|
2020-01-02 07:43:41 +01:00
|
|
|
|
[actix-web-codegen]: https://docs.rs/actix-web-codegen/
|
2019-06-25 05:36:32 +02:00
|
|
|
|
[autoload]: ../autoreload/
|