aboutsummaryrefslogtreecommitdiff
path: root/src/api.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-04-07 16:26:22 +0200
committerAlex Auvolat <alex@adnab.me>2020-04-07 16:26:22 +0200
commit061e676136613dce3ffd40c515aa58f99bda30d8 (patch)
tree53d441c60605aae7aaefdafb2734d5a446302ce3 /src/api.rs
parent46d5b896e841a209ea92cac23d5f59dfc1ff885d (diff)
downloadgarage-061e676136613dce3ffd40c515aa58f99bda30d8.tar.gz
garage-061e676136613dce3ffd40c515aa58f99bda30d8.zip
Refactor; ability to update network config
Diffstat (limited to 'src/api.rs')
-rw-r--r--src/api.rs76
1 files changed, 0 insertions, 76 deletions
diff --git a/src/api.rs b/src/api.rs
deleted file mode 100644
index cf70dbdf..00000000
--- a/src/api.rs
+++ /dev/null
@@ -1,76 +0,0 @@
-use std::sync::Arc;
-
-use futures_util::TryStreamExt;
-use hyper::service::{make_service_fn, service_fn};
-use hyper::{Body, Method, Request, Response, Server, StatusCode};
-use futures::future::Future;
-
-use crate::error::Error;
-use crate::membership::System;
-
-/// This is our service handler. It receives a Request, routes on its
-/// path, and returns a Future of a Response.
-async fn handler(sys: Arc<System>, req: Request<Body>) -> Result<Response<Body>, Error> {
- match (req.method(), req.uri().path()) {
- // Serve some instructions at /
- (&Method::GET, "/") => Ok(Response::new(Body::from(
- "Try POSTing data to /echo such as: `curl localhost:3000/echo -XPOST -d 'hello world'`",
- ))),
-
- // Simply echo the body back to the client.
- (&Method::POST, "/echo") => Ok(Response::new(req.into_body())),
-
- // Convert to uppercase before sending back to client using a stream.
- (&Method::POST, "/echo/uppercase") => {
- let chunk_stream = req.into_body().map_ok(|chunk| {
- chunk
- .iter()
- .map(|byte| byte.to_ascii_uppercase())
- .collect::<Vec<u8>>()
- });
- Ok(Response::new(Body::wrap_stream(chunk_stream)))
- }
-
- // Reverse the entire body before sending back to the client.
- //
- // Since we don't know the end yet, we can't simply stream
- // the chunks as they arrive as we did with the above uppercase endpoint.
- // So here we do `.await` on the future, waiting on concatenating the full body,
- // then afterwards the content can be reversed. Only then can we return a `Response`.
- (&Method::POST, "/echo/reversed") => {
- let whole_body = hyper::body::to_bytes(req.into_body()).await?;
-
- let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();
- Ok(Response::new(Body::from(reversed_body)))
- }
-
- // Return the 404 Not Found for other routes.
- _ => {
- let mut not_found = Response::default();
- *not_found.status_mut() = StatusCode::NOT_FOUND;
- Ok(not_found)
- }
- }
-}
-
-pub async fn run_api_server(sys: Arc<System>, shutdown_signal: impl Future<Output=()>) -> Result<(), hyper::Error> {
- let addr = ([0, 0, 0, 0], sys.config.api_port).into();
-
- let service = make_service_fn(|_| {
- let sys = sys.clone();
- async move {
- let sys = sys.clone();
- Ok::<_, Error>(service_fn(move |req: Request<Body>| {
- let sys = sys.clone();
- handler(sys, req)
- }))
- }
- });
-
- let server = Server::bind(&addr).serve(service);
-
- let graceful = server.with_graceful_shutdown(shutdown_signal);
- println!("API server listening on http://{}", addr);
-
- graceful.await
-}