2016-04-07 10:49:00 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Ethcore Webapplications for Parity
|
2016-04-14 20:38:48 +02:00
|
|
|
//! ```
|
|
|
|
//! extern crate jsonrpc_core;
|
2016-05-26 18:21:15 +02:00
|
|
|
//! extern crate ethcore_dapps;
|
2016-04-14 20:38:48 +02:00
|
|
|
//!
|
|
|
|
//! use std::sync::Arc;
|
|
|
|
//! use jsonrpc_core::IoHandler;
|
2016-05-26 18:21:15 +02:00
|
|
|
//! use ethcore_dapps::*;
|
2016-04-14 20:38:48 +02:00
|
|
|
//!
|
|
|
|
//! struct SayHello;
|
|
|
|
//! impl MethodCommand for SayHello {
|
|
|
|
//! fn execute(&self, _params: Params) -> Result<Value, Error> {
|
|
|
|
//! Ok(Value::String("hello".to_string()))
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let io = IoHandler::new();
|
|
|
|
//! io.add_method("say_hello", SayHello);
|
|
|
|
//! let _server = Server::start_unsecure_http(
|
2016-04-15 07:16:58 +02:00
|
|
|
//! &"127.0.0.1:3030".parse().unwrap(),
|
|
|
|
//! Arc::new(io)
|
|
|
|
//! );
|
2016-04-14 20:38:48 +02:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2016-04-07 10:49:00 +02:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![cfg_attr(feature="nightly", plugin(clippy))]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2016-04-12 22:44:53 +02:00
|
|
|
extern crate url;
|
2016-04-07 10:49:00 +02:00
|
|
|
extern crate hyper;
|
2016-05-17 16:55:08 +02:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
2016-04-07 10:49:00 +02:00
|
|
|
extern crate jsonrpc_core;
|
|
|
|
extern crate jsonrpc_http_server;
|
2016-05-26 18:21:15 +02:00
|
|
|
extern crate parity_dapps;
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
mod endpoint;
|
2016-04-07 12:10:26 +02:00
|
|
|
mod apps;
|
|
|
|
mod page;
|
|
|
|
mod router;
|
2016-04-14 20:38:48 +02:00
|
|
|
mod rpc;
|
|
|
|
mod api;
|
2016-05-12 14:45:09 +02:00
|
|
|
mod proxypac;
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2016-04-23 12:29:12 +02:00
|
|
|
use std::sync::{Arc, Mutex};
|
2016-04-14 20:38:48 +02:00
|
|
|
use std::net::SocketAddr;
|
2016-05-13 18:40:20 +02:00
|
|
|
use std::collections::HashMap;
|
2016-04-14 20:38:48 +02:00
|
|
|
use jsonrpc_core::{IoHandler, IoDelegate};
|
2016-04-08 16:11:58 +02:00
|
|
|
use router::auth::{Authorization, NoAuth, HttpBasicAuth};
|
|
|
|
|
2016-05-16 16:08:52 +02:00
|
|
|
static DAPPS_DOMAIN : &'static str = ".parity";
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
/// Webapps HTTP+RPC server build.
|
|
|
|
pub struct ServerBuilder {
|
2016-04-07 10:49:00 +02:00
|
|
|
handler: Arc<IoHandler>,
|
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl ServerBuilder {
|
2016-05-26 18:21:15 +02:00
|
|
|
/// Construct new dapps server
|
2016-04-07 10:49:00 +02:00
|
|
|
pub fn new() -> Self {
|
2016-04-14 20:38:48 +02:00
|
|
|
ServerBuilder {
|
|
|
|
handler: Arc::new(IoHandler::new())
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add io delegate.
|
2016-04-07 12:10:26 +02:00
|
|
|
pub fn add_delegate<D>(&self, delegate: IoDelegate<D>) where D: Send + Sync + 'static {
|
2016-04-07 10:49:00 +02:00
|
|
|
self.handler.add_delegate(delegate);
|
|
|
|
}
|
|
|
|
|
2016-04-08 16:11:58 +02:00
|
|
|
/// Asynchronously start server with no authentication,
|
2016-04-14 20:38:48 +02:00
|
|
|
/// returns result with `Server` handle on success or an error.
|
|
|
|
pub fn start_unsecure_http(&self, addr: &SocketAddr) -> Result<Server, ServerError> {
|
|
|
|
Server::start_http(addr, NoAuth, self.handler.clone())
|
2016-04-08 16:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously start server with `HTTP Basic Authentication`,
|
2016-04-14 20:38:48 +02:00
|
|
|
/// return result with `Server` handle on success or an error.
|
|
|
|
pub fn start_basic_auth_http(&self, addr: &SocketAddr, username: &str, password: &str) -> Result<Server, ServerError> {
|
|
|
|
Server::start_http(addr, HttpBasicAuth::single_user(username, password), self.handler.clone())
|
2016-04-08 16:11:58 +02:00
|
|
|
}
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|
2016-04-08 16:11:58 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
/// Webapps HTTP server.
|
|
|
|
pub struct Server {
|
|
|
|
server: Option<hyper::server::Listening>,
|
2016-04-23 12:29:12 +02:00
|
|
|
panic_handler: Arc<Mutex<Option<Box<Fn() -> () + Send>>>>,
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl Server {
|
|
|
|
fn start_http<A: Authorization + 'static>(addr: &SocketAddr, authorization: A, handler: Arc<IoHandler>) -> Result<Server, ServerError> {
|
2016-04-23 12:29:12 +02:00
|
|
|
let panic_handler = Arc::new(Mutex::new(None));
|
2016-04-14 20:38:48 +02:00
|
|
|
let authorization = Arc::new(authorization);
|
2016-05-13 18:40:20 +02:00
|
|
|
let endpoints = Arc::new(apps::all_endpoints());
|
|
|
|
let special = Arc::new({
|
|
|
|
let mut special = HashMap::new();
|
|
|
|
special.insert(router::SpecialEndpoint::Rpc, rpc::rpc(handler, panic_handler.clone()));
|
|
|
|
special.insert(router::SpecialEndpoint::Api, api::RestApi::new(endpoints.clone()));
|
|
|
|
special.insert(router::SpecialEndpoint::Utils, apps::utils());
|
|
|
|
special
|
|
|
|
});
|
2016-04-14 20:38:48 +02:00
|
|
|
|
|
|
|
try!(hyper::Server::http(addr))
|
|
|
|
.handle(move |_| router::Router::new(
|
|
|
|
apps::main_page(),
|
|
|
|
endpoints.clone(),
|
2016-05-13 18:40:20 +02:00
|
|
|
special.clone(),
|
2016-04-14 20:38:48 +02:00
|
|
|
authorization.clone(),
|
|
|
|
))
|
2016-04-23 12:29:12 +02:00
|
|
|
.map(|l| Server {
|
|
|
|
server: Some(l),
|
|
|
|
panic_handler: panic_handler,
|
|
|
|
})
|
2016-04-14 20:38:48 +02:00
|
|
|
.map_err(ServerError::from)
|
|
|
|
}
|
2016-04-23 12:29:12 +02:00
|
|
|
|
|
|
|
/// Set callback for panics.
|
|
|
|
pub fn set_panic_handler<F>(&self, handler: F) where F : Fn() -> () + Send + 'static {
|
|
|
|
*self.panic_handler.lock().unwrap() = Some(Box::new(handler));
|
|
|
|
}
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl Drop for Server {
|
2016-04-07 13:09:58 +02:00
|
|
|
fn drop(&mut self) {
|
2016-04-14 20:38:48 +02:00
|
|
|
self.server.take().unwrap().close()
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-04-07 13:09:58 +02:00
|
|
|
/// Webapp Server startup error
|
|
|
|
#[derive(Debug)]
|
2016-04-14 20:38:48 +02:00
|
|
|
pub enum ServerError {
|
2016-04-07 13:15:59 +02:00
|
|
|
/// Wrapped `std::io::Error`
|
2016-04-07 13:09:58 +02:00
|
|
|
IoError(std::io::Error),
|
2016-04-07 13:15:59 +02:00
|
|
|
/// Other `hyper` error
|
2016-04-07 13:09:58 +02:00
|
|
|
Other(hyper::error::Error),
|
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl From<hyper::error::Error> for ServerError {
|
2016-04-07 13:09:58 +02:00
|
|
|
fn from(err: hyper::error::Error) -> Self {
|
|
|
|
match err {
|
2016-04-14 20:38:48 +02:00
|
|
|
hyper::error::Error::Io(e) => ServerError::IoError(e),
|
|
|
|
e => ServerError::Other(e),
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
|
|
|
}
|