Created
April 22, 2022 14:04
-
-
Save joncfoo/3408503dd0fcaa0dfcf299d3c05bd152 to your computer and use it in GitHub Desktop.
Auto-reloading server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "example" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
actix-web = "4.0" | |
anyhow = { version = "1.0", features = ["backtrace"] } | |
listenfd = "0.5" | |
log = "0.4" | |
simple_logger = "2.1" | |
tokio = { version = "1.17", features = ["signal"]} | |
thiserror = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cargo install systemfd cargo-watch | |
# cargo-watch builds and runs the application on code change | |
# and systemfd sets up a socket to handoff to the application at startup | |
systemfd --no-pid --socket http::3000 -- cargo watch -x run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use actix_web::{get, middleware, App, HttpServer}; | |
use anyhow::{Error, Result}; | |
use listenfd::ListenFd; | |
use log::debug; | |
use simple_logger::SimpleLogger; | |
#[get("/")] | |
async fn hello() -> &'static str { | |
debug!("👋 yo"); | |
"👋 yo ho ho and a bottle of rum" | |
} | |
#[actix_web::main] | |
async fn main() -> Result<()> { | |
SimpleLogger::new() | |
.with_level(log::LevelFilter::Warn) | |
.init()?; | |
let mut listenfd = ListenFd::from_env(); | |
let listener = listenfd.take_tcp_listener(0)?.unwrap(); | |
HttpServer::new(move || { | |
App::new() | |
.wrap(middleware::Logger::default()) | |
.service(hello) | |
}) | |
.listen(listener)? | |
.run() | |
.await | |
.map_err(Error::from) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment