Skip to content

Commit

Permalink
Dapps and RPC server merge (openethereum#5365)
Browse files Browse the repository at this point in the history
* Dapps server as a middleware.

* Dapps RPC - Work in Progress

* Merging Dapps and RPC server.

* Fast HTTP server configuration.

* Bump jsonrpc

* Fixing test target

* Re-implementing commented-out tests.
  • Loading branch information
tomusdrw authored and gavofyork committed Apr 3, 2017
1 parent 6a05967 commit 2df4532
Show file tree
Hide file tree
Showing 35 changed files with 862 additions and 1,241 deletions.
148 changes: 105 additions & 43 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ serde_json = "0.9"
app_dirs = "1.1.1"
fdlimit = "0.1"
ws2_32-sys = "0.2"
hyper = { default-features = false, git = "https://github.com/paritytech/hyper" }
ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" }
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
ethsync = { path = "sync" }
Expand All @@ -50,8 +49,9 @@ parity-ipfs-api = { path = "ipfs" }
parity-updater = { path = "updater" }
parity-reactor = { path = "util/reactor" }
parity-local-store = { path = "local-store" }
ethcore-dapps = { path = "dapps", optional = true }
path = { path = "util/path" }

parity-dapps = { path = "dapps", optional = true }
clippy = { version = "0.0.103", optional = true}
ethcore-secretstore = { path = "secret_store", optional = true }

Expand All @@ -60,6 +60,7 @@ rustc_version = "0.2"

[dev-dependencies]
ethcore-ipc-tests = { path = "ipc/tests" }
pretty_assertions = "0.1"

[target.'cfg(windows)'.dependencies]
winapi = "0.2"
Expand All @@ -71,18 +72,18 @@ daemonize = "0.2"
default = ["ui-precompiled"]
ui = [
"dapps",
"ethcore-dapps/ui",
"parity-dapps/ui",
"ethcore-signer/ui",
]
ui-precompiled = [
"dapps",
"ethcore-signer/ui-precompiled",
"ethcore-dapps/ui-precompiled",
"parity-dapps/ui-precompiled",
]
dapps = ["ethcore-dapps"]
dapps = ["parity-dapps"]
ipc = ["ethcore/ipc", "ethsync/ipc"]
jit = ["ethcore/jit"]
dev = ["clippy", "ethcore/dev", "ethcore-util/dev", "ethsync/dev", "ethcore-rpc/dev", "ethcore-dapps/dev", "ethcore-signer/dev"]
dev = ["clippy", "ethcore/dev", "ethcore-util/dev", "ethsync/dev", "ethcore-rpc/dev", "parity-dapps/dev", "ethcore-signer/dev"]
json-tests = ["ethcore/json-tests"]
test-heavy = ["ethcore/test-heavy"]
ethkey-cli = ["ethcore/ethkey-cli"]
Expand Down
7 changes: 2 additions & 5 deletions dapps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
description = "Parity Dapps crate"
name = "ethcore-dapps"
name = "parity-dapps"
version = "1.7.0"
license = "GPL-3.0"
authors = ["Parity Technologies <[email protected]>"]
Expand Down Expand Up @@ -28,11 +28,8 @@ zip = { version = "0.1", default-features = false }

jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
# TODO [ToDr] Temporary solution, server should be merged with RPC.
jsonrpc-server-utils = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }

ethcore-devtools = { path = "../devtools" }
ethcore-rpc = { path = "../rpc" }
ethcore-util = { path = "../util" }
fetch = { path = "../util/fetch" }
parity-hash-fetch = { path = "../hash-fetch" }
Expand All @@ -42,7 +39,7 @@ parity-ui = { path = "./ui" }
clippy = { version = "0.0.103", optional = true}

[features]
dev = ["clippy", "ethcore-rpc/dev", "ethcore-util/dev"]
dev = ["clippy", "ethcore-util/dev"]

ui = ["parity-ui/no-precompiled-js"]
ui-precompiled = ["parity-ui/use-precompiled-js"]
40 changes: 20 additions & 20 deletions dapps/src/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;
use unicase::UniCase;
use hyper::{server, net, Decoder, Encoder, Next, Control};
use hyper::header;
Expand All @@ -26,48 +25,49 @@ use apps::fetcher::Fetcher;

use handlers::extract_url;
use endpoint::{Endpoint, Endpoints, Handler, EndpointPath};
use jsonrpc_http_server;
use jsonrpc_server_utils::cors;
use jsonrpc_http_server::{self, AccessControlAllowOrigin};

#[derive(Clone)]
pub struct RestApi {
cors_domains: Option<Vec<cors::AccessControlAllowOrigin>>,
endpoints: Arc<Endpoints>,
fetcher: Arc<Fetcher>,
pub struct RestApi<F> {
// TODO [ToDr] cors_domains should be handled by the server to avoid duplicated logic.
// RequestMiddleware should be able to tell that cors headers should be included.
cors_domains: Option<Vec<AccessControlAllowOrigin>>,
apps: Vec<App>,
fetcher: F,
}

impl RestApi {
pub fn new(cors_domains: Vec<cors::AccessControlAllowOrigin>, endpoints: Arc<Endpoints>, fetcher: Arc<Fetcher>) -> Box<Endpoint> {
impl<F: Fetcher + Clone> RestApi<F> {
pub fn new(cors_domains: Vec<AccessControlAllowOrigin>, endpoints: &Endpoints, fetcher: F) -> Box<Endpoint> {
Box::new(RestApi {
cors_domains: Some(cors_domains),
endpoints: endpoints,
apps: Self::list_apps(endpoints),
fetcher: fetcher,
})
}

fn list_apps(&self) -> Vec<App> {
self.endpoints.iter().filter_map(|(ref k, ref e)| {
fn list_apps(endpoints: &Endpoints) -> Vec<App> {
endpoints.iter().filter_map(|(ref k, ref e)| {
e.info().map(|ref info| App::from_info(k, info))
}).collect()
}
}

impl Endpoint for RestApi {
impl<F: Fetcher + Clone> Endpoint for RestApi<F> {
fn to_async_handler(&self, path: EndpointPath, control: Control) -> Box<Handler> {
Box::new(RestApiRouter::new(self.clone(), path, control))
Box::new(RestApiRouter::new((*self).clone(), path, control))
}
}

struct RestApiRouter {
api: RestApi,
struct RestApiRouter<F> {
api: RestApi<F>,
cors_header: Option<header::AccessControlAllowOrigin>,
path: Option<EndpointPath>,
control: Option<Control>,
handler: Box<Handler>,
}

impl RestApiRouter {
fn new(api: RestApi, path: EndpointPath, control: Control) -> Self {
impl<F: Fetcher> RestApiRouter<F> {
fn new(api: RestApi<F>, path: EndpointPath, control: Control) -> Self {
RestApiRouter {
path: Some(path),
cors_header: None,
Expand Down Expand Up @@ -114,7 +114,7 @@ impl RestApiRouter {
}
}

impl server::Handler<net::HttpStream> for RestApiRouter {
impl<F: Fetcher> server::Handler<net::HttpStream> for RestApiRouter<F> {

fn on_request(&mut self, request: server::Request<net::HttpStream>) -> Next {
self.cors_header = jsonrpc_http_server::cors_header(&request, &self.api.cors_domains).into();
Expand Down Expand Up @@ -142,7 +142,7 @@ impl server::Handler<net::HttpStream> for RestApiRouter {
if let Some(ref hash) = hash { path.app_id = hash.clone().to_owned() }

let handler = endpoint.and_then(|v| match v {
"apps" => Some(response::as_json(&self.api.list_apps())),
"apps" => Some(response::as_json(&self.api.apps)),
"ping" => Some(response::ping()),
"content" => self.resolve_content(hash, path, control),
_ => None
Expand Down
10 changes: 6 additions & 4 deletions dapps/src/apps/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ pub trait Fetcher: Send + Sync + 'static {
fn to_async_handler(&self, path: EndpointPath, control: hyper::Control) -> Box<Handler>;
}

pub struct ContentFetcher<F: Fetch = FetchClient, R: URLHint + Send + Sync + 'static = URLHintContract> {
#[derive(Clone)]
pub struct ContentFetcher<F: Fetch + Clone = FetchClient, R: URLHint + Clone + 'static = URLHintContract> {
dapps_path: PathBuf,
resolver: R,
cache: Arc<Mutex<ContentCache>>,
Expand All @@ -57,14 +58,14 @@ pub struct ContentFetcher<F: Fetch = FetchClient, R: URLHint + Send + Sync + 'st
fetch: F,
}

impl<R: URLHint + Send + Sync + 'static, F: Fetch> Drop for ContentFetcher<F, R> {
impl<R: URLHint + Clone + 'static, F: Fetch + Clone> Drop for ContentFetcher<F, R> {
fn drop(&mut self) {
// Clear cache path
let _ = fs::remove_dir_all(&self.dapps_path);
}
}

impl<R: URLHint + Send + Sync + 'static, F: Fetch> ContentFetcher<F, R> {
impl<R: URLHint + Clone + 'static, F: Fetch + Clone> ContentFetcher<F, R> {

pub fn new(resolver: R, sync_status: Arc<SyncStatus>, embeddable_on: Option<(String, u16)>, remote: Remote, fetch: F) -> Self {
let mut dapps_path = env::temp_dir();
Expand Down Expand Up @@ -97,7 +98,7 @@ impl<R: URLHint + Send + Sync + 'static, F: Fetch> ContentFetcher<F, R> {
}
}

impl<R: URLHint + Send + Sync + 'static, F: Fetch> Fetcher for ContentFetcher<F, R> {
impl<R: URLHint + Clone + 'static, F: Fetch + Clone> Fetcher for ContentFetcher<F, R> {
fn contains(&self, content_id: &str) -> bool {
{
let mut cache = self.cache.lock();
Expand Down Expand Up @@ -233,6 +234,7 @@ mod tests {
use page::LocalPageEndpoint;
use super::{ContentFetcher, Fetcher};

#[derive(Clone)]
struct FakeResolver;
impl URLHint for FakeResolver {
fn resolve(&self, _id: Bytes) -> Option<URLHintResult> {
Expand Down
3 changes: 2 additions & 1 deletion dapps/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

//! URL Endpoint traits
use hyper::{self, server, net};
use std::collections::BTreeMap;

use hyper::{self, server, net};

#[derive(Debug, PartialEq, Default, Clone)]
pub struct EndpointPath {
pub app_id: String,
Expand Down
44 changes: 0 additions & 44 deletions dapps/src/handlers/auth.rs

This file was deleted.

2 changes: 0 additions & 2 deletions dapps/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

//! Hyper handlers implementations.
mod auth;
mod content;
mod echo;
mod fetch;
mod redirect;
mod streaming;

pub use self::auth::AuthRequiredHandler;
pub use self::content::ContentHandler;
pub use self::echo::EchoHandler;
pub use self::fetch::{ContentFetcherHandler, ContentValidator, FetchControl, ValidatorResponse};
Expand Down
Loading

0 comments on commit 2df4532

Please sign in to comment.