diff --git a/beacon_node/rest_api/Cargo.toml b/beacon_node/rest_api/Cargo.toml index ac762ebb7..7ea21eeba 100644 --- a/beacon_node/rest_api/Cargo.toml +++ b/beacon_node/rest_api/Cargo.toml @@ -26,7 +26,6 @@ clap = "2.32.0" http = "^0.1.17" prometheus = { version = "^0.6", features = ["process"] } hyper = "0.12.34" -futures = "0.1" exit-future = "0.1.3" tokio = "0.1.17" url = "2.0" diff --git a/beacon_node/rest_api/src/lib.rs b/beacon_node/rest_api/src/lib.rs index d7ea72cc5..2062d4e03 100644 --- a/beacon_node/rest_api/src/lib.rs +++ b/beacon_node/rest_api/src/lib.rs @@ -20,7 +20,7 @@ use client_network::NetworkMessage; use client_network::Service as NetworkService; use eth2_config::Eth2Config; use hyper::rt::Future; -use hyper::service::Service; +use hyper::service::{Service, MakeService}; use hyper::{Body, Method, Request, Response, Server, StatusCode}; use parking_lot::RwLock; use response_builder::ResponseBuilder; @@ -31,13 +31,44 @@ use std::sync::Arc; use tokio::runtime::TaskExecutor; use tokio::sync::mpsc; use url_query::UrlQuery; +use hyper::server::conn::AddrStream; pub use beacon::{BlockResponse, HeadResponse, StateResponse}; pub use config::Config as ApiConfig; use eth2_libp2p::rpc::RequestId; +use serde::export::PhantomData; type BoxFut = Box, Error = ApiError> + Send>; +pub struct ApiMaker { + log: slog::Logger, + beacon_chain: Arc>, + db_path: DBPath, + network_service: Arc>, + network_channel: Arc>>, + eth2_config: Arc, +} + +impl MakeService for ApiMaker { + type ReqBody = Body; + type ResBody = Body; + type Error = ApiError; + type Service = ApiService; + type Future = futures::future::FutureResult; + type MakeError = String; + + fn make_service(&mut self, _ctx: AddrStream) -> Self::Future { + futures::future::ok(ApiService { + log: self.log.clone(), + beacon_chain: self.beacon_chain.clone(), + db_path: self.db_path.clone(), + network_service: self.network_service.clone(), + network_channel: self.network_channel.clone(), + eth2_config: self.eth2_config.clone(), + }) + } +} + pub struct ApiService { log: slog::Logger, beacon_chain: Arc>, @@ -205,15 +236,16 @@ pub fn start_server( let server_bc = beacon_chain.clone(); let eth2_config = Arc::new(eth2_config); - let service = move || ApiService { - log: server_log.clone(), - beacon_chain: server_bc.clone(), + let service = move || ApiMaker { + log: log.clone(), + beacon_chain: beacon_chain.clone(), db_path: db_path.clone(), network_service: network_service.clone(), network_channel: Arc::new(RwLock::new(network_chan.clone())), eth2_config: eth2_config.clone(), }; + let log_clone = log.clone(); let server = Server::bind(&bind_addr) .serve(service) @@ -237,15 +269,6 @@ pub fn start_server( Ok(exit_signal) } -impl Future for ApiService { - type Item = Result, ApiError>; - type Error = ApiError; - - fn poll(&mut self) -> Result, Self::Error> { - unimplemented!() - } -} - fn success_response(body: Body) -> Response { Response::builder() .status(StatusCode::OK)