openethereum/webapp/src/lib.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

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
#![warn(missing_docs)]
#![cfg_attr(feature="nightly", plugin(clippy))]
#[macro_use]
extern crate log;
extern crate hyper;
extern crate iron;
extern crate jsonrpc_core;
extern crate jsonrpc_http_server;
extern crate ethcore_rpc as rpc;
2016-04-07 12:10:26 +02:00
extern crate parity_webapp;
2016-04-07 10:49:00 +02:00
use std::sync::Arc;
use self::jsonrpc_core::{IoHandler, IoDelegate};
use jsonrpc_http_server::ServerHandler;
2016-04-07 12:10:26 +02:00
mod apps;
mod page;
mod router;
2016-04-07 10:49:00 +02:00
/// Http server.
pub struct WebappServer {
handler: Arc<IoHandler>,
}
impl WebappServer {
/// Construct new http server object
pub fn new() -> Self {
2016-04-07 12:10:26 +02:00
WebappServer {
2016-04-07 10:49:00 +02:00
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);
}
/// Start server asynchronously and returns result with `Listening` handle on success or an error.
pub fn start_http(&self, addr: &str, threads: usize) -> Result<Listening, WebappServerError> {
2016-04-07 10:49:00 +02:00
let addr = addr.to_owned();
let handler = self.handler.clone();
2016-04-07 12:10:26 +02:00
let cors_domain = jsonrpc_http_server::AccessControlAllowOrigin::Null;
let rpc = ServerHandler::new(handler, cors_domain);
let router = router::Router::new(rpc, apps::all_pages());
2016-04-07 10:49:00 +02:00
try!(hyper::Server::http(addr.as_ref() as &str))
.handle_threads(router, threads)
.map(|l| Listening { listening: l })
.map_err(WebappServerError::from)
}
}
/// Listening handle
pub struct Listening {
listening: hyper::server::Listening
}
impl Drop for Listening {
fn drop(&mut self) {
self.listening.close().unwrap();
}
}
2016-04-07 10:49:00 +02:00
/// Webapp Server startup error
#[derive(Debug)]
pub enum WebappServerError {
IoError(std::io::Error),
Other(hyper::error::Error),
}
impl From<hyper::error::Error> for WebappServerError {
fn from(err: hyper::error::Error) -> Self {
match err {
hyper::error::Error::Io(e) => WebappServerError::IoError(e),
e => WebappServerError::Other(e)
}
2016-04-07 10:49:00 +02:00
}
}