2018-05-22 23:15:08 +02:00
|
|
|
|
---
|
|
|
|
|
title: Getting Started
|
|
|
|
|
menu: docs_basics
|
|
|
|
|
weight: 130
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Getting Started
|
|
|
|
|
|
|
|
|
|
Let’s write our first actix web application!
|
|
|
|
|
|
|
|
|
|
## 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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Now, add `actix-web` as dependencies of your project by ensuring your `Cargo.toml`
|
|
|
|
|
contains the following:
|
|
|
|
|
|
|
|
|
|
```ini
|
|
|
|
|
[dependencies]
|
|
|
|
|
actix-web = "{{< actix-version "actix-web" >}}"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In order to implement a web server, we first need to create a request handler.
|
|
|
|
|
|
2019-06-18 22:44:43 +02:00
|
|
|
|
A request handler is a function that accepts any type that can be extracted from a
|
|
|
|
|
request (ie, `impl FromRequest`) as its only parameter 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-18 22:44:43 +02:00
|
|
|
|
Next, create an `App` instance and register the request handler with
|
|
|
|
|
the application's `route` on a particular *HTTP method* and *path* and
|
2018-05-22 23:15:08 +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
|
2019-06-18 22:44:43 +02:00
|
|
|
|
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`.
|
|
|
|
|
Head over to ``http://localhost:8088/`` to see the results.
|
|
|
|
|
|
2019-03-02 06:42:52 +01:00
|
|
|
|
If you want, you can have an automatic reloading server during development
|
2018-05-22 23:15:08 +02:00
|
|
|
|
that recompiles on demand. To see how this can be accomplished have a look
|
|
|
|
|
at the [autoreload pattern](../autoreload/).
|