|
2 | 2 | #![deny(rust_2018_idioms)]
|
3 | 3 |
|
4 | 4 | use hyper::service::{make_service_fn, service_fn};
|
5 |
| -use hyper::{Body, Error, Request, Response, Server, StatusCode}; |
| 5 | +use hyper::{Body, Error, Method, Request, Response, Server, StatusCode}; |
6 | 6 | use primitives::adapter::Adapter;
|
7 | 7 | use primitives::Config;
|
8 | 8 | use slog::{error, info, Logger};
|
9 | 9 |
|
10 | 10 | pub mod routes {
|
11 | 11 | pub mod channel;
|
| 12 | + pub mod cfg { |
| 13 | + use crate::ResponseError; |
| 14 | + use hyper::header::CONTENT_TYPE; |
| 15 | + use hyper::{Body, Response}; |
| 16 | + use primitives::Config; |
| 17 | + |
| 18 | + pub fn return_config(config: &Config) -> Result<Response<Body>, ResponseError> { |
| 19 | + let config_str = serde_json::to_string(config)?; |
| 20 | + |
| 21 | + Ok(Response::builder() |
| 22 | + .header(CONTENT_TYPE, "application/json") |
| 23 | + .body(Body::from(config_str)) |
| 24 | + .expect("Creating a response should never fail")) |
| 25 | + } |
| 26 | + } |
12 | 27 | }
|
13 | 28 |
|
14 | 29 | pub mod access;
|
@@ -43,7 +58,7 @@ impl<A: Adapter + 'static> Application<A> {
|
43 | 58 | async move {
|
44 | 59 | Ok::<_, Error>(service_fn(move |req| {
|
45 | 60 | let adapter_config = adapter_config.clone();
|
46 |
| - async move { Ok::<_, Error>(handle_routing(req, adapter_config.0).await) } |
| 61 | + async move { Ok::<_, Error>(handle_routing(req, adapter_config).await) } |
47 | 62 | }))
|
48 | 63 | }
|
49 | 64 | });
|
@@ -71,11 +86,16 @@ where
|
71 | 86 | }
|
72 | 87 | }
|
73 | 88 |
|
74 |
| -async fn handle_routing(req: Request<Body>, adapter: impl Adapter) -> Response<Body> { |
75 |
| - if req.uri().path().starts_with("/channel") { |
76 |
| - crate::routes::channel::handle_channel_routes(req, adapter).await |
77 |
| - } else { |
78 |
| - Err(ResponseError::NotFound) |
| 89 | +async fn handle_routing( |
| 90 | + req: Request<Body>, |
| 91 | + (adapter, config): (impl Adapter, Config), |
| 92 | +) -> Response<Body> { |
| 93 | + match (req.uri().path(), req.method()) { |
| 94 | + ("/cfg", &Method::GET) => crate::routes::cfg::return_config(&config), |
| 95 | + (route, _) if route.starts_with("/channel") => { |
| 96 | + crate::routes::channel::handle_channel_routes(req, adapter).await |
| 97 | + } |
| 98 | + _ => Err(ResponseError::NotFound), |
79 | 99 | }
|
80 | 100 | .unwrap_or_else(|response_err| match response_err {
|
81 | 101 | ResponseError::NotFound => not_found(),
|
|
0 commit comments