diff options
author | Adam Carpenter <atc@53hor.net> | 2020-05-03 22:58:13 -0400 |
---|---|---|
committer | Adam Carpenter <atc@53hor.net> | 2020-05-03 22:58:13 -0400 |
commit | b5960dffbd7151c2eedc0e897f94f7b1691c2a39 (patch) | |
tree | 8f4d5f55f219e3e375e62092ca2e88741148e006 /dichroism/src/main.rs | |
parent | 12fab23b4d6b3b2106e7d345a63c9a3ae9371082 (diff) | |
download | theglassyladies-b5960dffbd7151c2eedc0e897f94f7b1691c2a39.tar.xz theglassyladies-b5960dffbd7151c2eedc0e897f94f7b1691c2a39.zip |
started actix-web rust project for api
Diffstat (limited to 'dichroism/src/main.rs')
-rw-r--r-- | dichroism/src/main.rs | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index e7a11a9..e4bb217 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,3 +1,28 @@ -fn main() { - println!("Hello, world!"); +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 } |