blob: e4bb2172512e3e5bb2cf6f2f82b02a8e5b2947c7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use listenfd::ListenFd;
async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
async fn index(_: HttpRequest) -> impl Responder {
"Hello, this is an API. If you're looking for the web app, try port 443.".to_string()
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let mut listenfd = ListenFd::from_env();
let mut server = HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/{name}", web::get().to(greet))
});
server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
server.listen(l)?
} else {
server.bind("127.0.0.1:8000")?
};
server.run().await
}
|