Adding StatusPage and main page support

This commit is contained in:
Tomasz Drwięga 2016-04-07 15:14:39 +02:00
parent 6279237c86
commit ed633bd0b7
5 changed files with 33 additions and 8 deletions

9
Cargo.lock generated
View File

@ -321,6 +321,7 @@ dependencies = [
"jsonrpc-core 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-http-server 4.0.0 (git+https://github.com/tomusdrw/jsonrpc-http-server.git)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-status 0.1.1 (git+https://github.com/tomusdrw/parity-status.git)",
"parity-wallet 0.1.0 (git+https://github.com/tomusdrw/parity-wallet.git)",
"parity-webapp 0.1.0 (git+https://github.com/tomusdrw/parity-webapp.git)",
]
@ -693,6 +694,14 @@ name = "odds"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "parity-status"
version = "0.1.1"
source = "git+https://github.com/tomusdrw/parity-status.git#31cdfd631af970b58c4ddb4e7d7e927af2e07ce3"
dependencies = [
"parity-webapp 0.1.0 (git+https://github.com/tomusdrw/parity-webapp.git)",
]
[[package]]
name = "parity-wallet"
version = "0.1.0"

View File

@ -17,6 +17,7 @@ ethcore-rpc = { path = "../rpc" }
ethcore-util = { path = "../util" }
parity-webapp = { git = "https://github.com/tomusdrw/parity-webapp.git" }
# List of apps
parity-status = { git = "https://github.com/tomusdrw/parity-status.git" }
parity-wallet = { git = "https://github.com/tomusdrw/parity-wallet.git", optional = true }
clippy = { version = "0.0.61", optional = true}

View File

@ -17,21 +17,25 @@
use std::collections::HashMap;
use page::{Page, PageHandler};
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 all_pages() -> Pages {
let mut pages = Pages::new();
wallet_page(&mut pages);
pages
}
#[cfg(feature="parity-wallet")]
#[cfg(feature = "parity-wallet")]
fn wallet_page(pages: &mut Pages) {
pages.insert("wallet".to_owned(), Box::new(PageHandler { app: parity_wallet::App::default() }));
}
#[cfg(not(feature="parity-wallet"))]
#[cfg(not(feature = "parity-wallet"))]
fn wallet_page(_pages: &mut Pages) {}

View File

@ -60,7 +60,7 @@ impl WebappServer {
let cors_domain = jsonrpc_http_server::AccessControlAllowOrigin::Null;
let rpc = ServerHandler::new(handler, cors_domain);
let router = router::Router::new(rpc, apps::all_pages());
let router = router::Router::new(rpc, apps::main_page(), apps::all_pages());
try!(hyper::Server::http(addr.as_ref() as &str))
.handle_threads(router, threads)

View File

@ -17,12 +17,14 @@
//! Router implementation
use hyper;
use page::Page;
use apps::Pages;
use iron::request::Url;
use jsonrpc_http_server::ServerHandler;
pub struct Router {
rpc: ServerHandler,
main_page: Box<Page>,
pages: Pages,
}
@ -33,15 +35,19 @@ impl hyper::server::Handler for Router {
Some(ref url) if self.pages.contains_key(url) => {
self.pages.get(url).unwrap().handle(req, res);
}
_ => self.rpc.handle(req, res),
_ if req.method == hyper::method::Method::Post => {
self.rpc.handle(req, res)
},
_ => self.main_page.handle(req, res),
}
}
}
impl Router {
pub fn new(rpc: ServerHandler, pages: Pages) -> Self {
pub fn new(rpc: ServerHandler, main_page: Box<Page>, pages: Pages) -> Self {
Router {
rpc: rpc,
main_page: main_page,
pages: pages,
}
}
@ -75,13 +81,18 @@ impl Router {
fn extract_request_path<'a, 'b>(mut req: hyper::server::Request<'a, 'b>) -> (Option<String>, hyper::server::Request<'a, 'b>) {
let url = Router::extract_url(&req);
match url {
Some(url) => {
Some(ref url) if url.path.len() > 1 => {
let part = url.path[0].clone();
let url = url.path[1..].join("/");
req.uri = hyper::uri::RequestUri::AbsolutePath(url);
(Some(part), req)
},
None => {
Some(url) => {
let url = url.path.join("/");
req.uri = hyper::uri::RequestUri::AbsolutePath(url);
(None, req)
},
_ => {
(None, req)
}
}