WIP: Trying to get futures to work...

This commit is contained in:
Luke Anderson 2019-09-10 17:16:41 +10:00
parent 965d6f1df9
commit 576712cefe
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
2 changed files with 36 additions and 14 deletions

View File

@ -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"

View File

@ -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<dyn Future<Item = Response<Body>, Error = ApiError> + Send>;
pub struct ApiMaker<T: BeaconChainTypes + 'static> {
log: slog::Logger,
beacon_chain: Arc<BeaconChain<T>>,
db_path: DBPath,
network_service: Arc<NetworkService<T>>,
network_channel: Arc<RwLock<mpsc::UnboundedSender<NetworkMessage>>>,
eth2_config: Arc<Eth2Config>,
}
impl<T: BeaconChainTypes> MakeService<AddrStream> for ApiMaker<T> {
type ReqBody = Body;
type ResBody = Body;
type Error = ApiError;
type Service = ApiService<T>;
type Future = futures::future::FutureResult<Self::Service, Self::MakeError>;
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<T: BeaconChainTypes + 'static> {
log: slog::Logger,
beacon_chain: Arc<BeaconChain<T>>,
@ -205,15 +236,16 @@ pub fn start_server<T: BeaconChainTypes>(
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<T: BeaconChainTypes>(
Ok(exit_signal)
}
impl<T: BeaconChainTypes> Future for ApiService<T> {
type Item = Result<Response<Body>, ApiError>;
type Error = ApiError;
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
unimplemented!()
}
}
fn success_response(body: Body) -> Response<Body> {
Response::builder()
.status(StatusCode::OK)