1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00
actix-website/content/docs/autoreload.md

53 lines
1.6 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Autoreloading
menu: docs_patterns
weight: 1000
---
# Auto-Reloading Development Server
During development it can be very handy to have cargo automatically recompile the code
on change. This can be accomplished by using [cargo-watch][cargowatch]. Because an
actix app will typically bind to a port for listening for incoming HTTP requests it makes
sense to combine this with the [listenfd][listenfd] crate and the [systemfd][systemfd]
utility to ensure the socket is kept open while the app is compiling and reloading.
2018-05-22 23:15:08 +02:00
`systemfd` will open a socket and pass it to `cargo-watch` which will watch for
changes and then invoke the compiler and run your actix app. The actix app
will then use `listenfd` to pick up the socket that `systemfd` opened.
## Binaries Necessary
For an automatic reloading experience you need to install `cargo-watch` and
2020-01-26 00:50:02 +01:00
`systemfd`. Both are written in Rust and can be installed with `cargo install`:
2018-05-22 23:15:08 +02:00
```
cargo install systemfd cargo-watch
```
## Code Changes
Additionally you need to slightly modify your actix app so that it can pick up
an external socket opened by `systemfd`. Add the listenfd dependency to your app:
2018-05-22 23:15:08 +02:00
```ini
2018-06-12 18:15:33 +02:00
[dependencies]
2018-05-22 23:15:08 +02:00
listenfd = "0.3"
```
Then modify your server code to only invoke `bind` as a fallback:
2019-06-17 06:44:38 +02:00
{{< include-example example="autoreload" file="main.rs" section="autoreload" >}}
2018-05-22 23:15:08 +02:00
## Running the Server
To now run the development server invoke this command:
```
systemfd --no-pid -s http::3000 -- cargo watch -x run
2018-05-22 23:15:08 +02:00
```
[cargowatch]: https://github.com/passcod/cargo-watch
[listenfd]: https://crates.io/crates/listenfd
[systemfd]: https://github.com/mitsuhiko/systemfd