Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Using hyper-mio branch in webapps. #957

Merged
merged 8 commits into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 3 additions & 81 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use number_prefix::{binary_prefix, Standalone, Prefixed};
#[cfg(feature = "rpc")]
use rpc::Server as RpcServer;
#[cfg(feature = "webapp")]
use webapp::Listening as WebappServer;
use webapp::Server as WebappServer;

mod price_info;
mod upgrade;
Expand Down Expand Up @@ -346,12 +346,12 @@ fn setup_webapp_server(
sync: Arc<EthSync>,
secret_store: Arc<AccountService>,
miner: Arc<Miner>,
url: &str,
url: &SocketAddr,
auth: Option<(String, String)>,
) -> WebappServer {
use rpc::v1::*;

let server = webapp::WebappServer::new();
let server = webapp::ServerBuilder::new();
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(NetClient::new(&sync).to_delegate());
server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate());
Expand All @@ -360,14 +360,14 @@ fn setup_webapp_server(
server.add_delegate(EthcoreClient::new(&miner).to_delegate());
let start_result = match auth {
None => {
server.start_unsecure_http(url, ::num_cpus::get())
server.start_unsecure_http(url)
},
Some((username, password)) => {
server.start_basic_auth_http(url, ::num_cpus::get(), &username, &password)
server.start_basic_auth_http(url, &username, &password)
},
};
match start_result {
Err(webapp::WebappServerError::IoError(err)) => die_with_io_error(err),
Err(webapp::ServerError::IoError(err)) => die_with_io_error(err),
Err(e) => die!("{:?}", e),
Ok(handle) => handle,
}
Expand All @@ -383,7 +383,7 @@ fn setup_rpc_server(
_sync: Arc<EthSync>,
_secret_store: Arc<AccountService>,
_miner: Arc<Miner>,
_url: &str,
_url: &SocketAddr,
_cors_domain: Option<String>,
_apis: Vec<&str>,
) -> ! {
Expand All @@ -399,7 +399,7 @@ fn setup_webapp_server(
_sync: Arc<EthSync>,
_secret_store: Arc<AccountService>,
_miner: Arc<Miner>,
_url: &str,
_url: &SocketAddr,
_auth: Option<(String, String)>,
) -> ! {
die!("Your Parity version has been compiled without WebApps support.")
Expand Down Expand Up @@ -753,6 +753,7 @@ impl Configuration {
},
self.args.flag_webapp_port
);
let addr = SocketAddr::from_str(&url).unwrap_or_else(|_| die!("{}: Invalid Webapps listen host/port given.", url));
let auth = self.args.flag_webapp_user.as_ref().map(|username| {
let password = self.args.flag_webapp_pass.as_ref().map_or_else(|| {
use rpassword::read_password;
Expand All @@ -769,7 +770,7 @@ impl Configuration {
sync.clone(),
account_service.clone(),
miner.clone(),
&url,
&addr,
auth,
))
} else {
Expand Down
6 changes: 3 additions & 3 deletions webapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ authors = ["Ethcore <[email protected]"]
[dependencies]
log = "0.3"
jsonrpc-core = "2.0"
jsonrpc-http-server = { git = "https://github.com/tomusdrw/jsonrpc-http-server.git", branch="old-hyper" }
hyper = { version = "0.8", default-features = false }
iron = { version = "0.3" }
jsonrpc-http-server = { git = "https://github.com/debris/jsonrpc-http-server.git" }
hyper = { default-features = false, git = "https://github.com/hyperium/hyper", branch = "mio" }
url = "0.5"
ethcore-rpc = { path = "../rpc" }
ethcore-util = { path = "../util" }
parity-webapp = { git = "https://github.com/tomusdrw/parity-webapp.git" }
Expand Down
94 changes: 94 additions & 0 deletions webapp/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Simple REST API

use std::io::Write;
use std::sync::Arc;
use hyper::status::StatusCode;
use hyper::{header, server, Decoder, Encoder, Next};
use hyper::net::HttpStream;
use endpoint::{Endpoint, Endpoints};

pub struct RestApi {
endpoints: Arc<Endpoints>,
}

impl RestApi {
pub fn new(endpoints: Arc<Endpoints>) -> Box<Endpoint> {
Box::new(RestApi {
endpoints: endpoints
})
}

fn list_pages(&self) -> String {
let mut s = "[".to_owned();
for name in self.endpoints.keys() {
s.push_str(&format!("\"{}\",", name));
}
s.push_str("\"rpc\"");
s.push_str("]");
s
}
}

impl Endpoint for RestApi {
fn to_handler(&self, _prefix: &str) -> Box<server::Handler<HttpStream>> {
Box::new(RestApiHandler {
pages: self.list_pages(),
write_pos: 0,
})
}
}

struct RestApiHandler {
pages: String,
write_pos: usize,
}

impl server::Handler<HttpStream> for RestApiHandler {
fn on_request(&mut self, _request: server::Request) -> Next {
Next::write()
}

fn on_request_readable(&mut self, _decoder: &mut Decoder<HttpStream>) -> Next {
Next::write()
}

fn on_response(&mut self, res: &mut server::Response) -> Next {
res.set_status(StatusCode::Ok);
res.headers_mut().set(header::ContentType("application/json".parse().unwrap()));
Next::write()
}

fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
let bytes = self.pages.as_bytes();
if self.write_pos == bytes.len() {
return Next::end();
}

match encoder.write(&bytes[self.write_pos..]) {
Ok(bytes) => {
self.write_pos += bytes;
Next::write()
},
Err(e) => match e.kind() {
::std::io::ErrorKind::WouldBlock => Next::write(),
_ => Next::end()
},
}
}
}
20 changes: 10 additions & 10 deletions webapp/src/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@
// 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::collections::HashMap;
use page::{Page, PageHandler};
use endpoint::Endpoints;
use page::PageEndpoint;

extern crate parity_status;
extern crate parity_wallet;

pub type Pages = HashMap<String, Box<Page>>;

pub fn main_page() -> Box<Page> {
Box::new(PageHandler { app: parity_status::App::default() })
pub fn main_page() -> &'static str {
"/status/"
}

pub fn all_pages() -> Pages {
let mut pages = Pages::new();
pub fn all_endpoints() -> Endpoints {
let mut pages = Endpoints::new();
pages.insert("status".to_owned(), Box::new(PageEndpoint::new(parity_status::App::default())));
wallet_page(&mut pages);
pages
}

#[cfg(feature = "parity-wallet")]
fn wallet_page(pages: &mut Pages) {
pages.insert("wallet".to_owned(), Box::new(PageHandler { app: parity_wallet::App::default() }));
fn wallet_page(pages: &mut Endpoints) {
pages.insert("wallet".to_owned(), Box::new(PageEndpoint::new(parity_wallet::App::default())));
}

#[cfg(not(feature = "parity-wallet"))]
fn wallet_page(_pages: &mut Pages) {}
fn wallet_page(_pages: &mut Endpoints) {}
Loading