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

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)
}
}