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-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)", "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)", "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-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)", "parity-webapp 0.1.0 (git+https://github.com/tomusdrw/parity-webapp.git)",
] ]
@ -693,6 +694,14 @@ name = "odds"
version = "0.2.12" version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "parity-wallet" name = "parity-wallet"
version = "0.1.0" version = "0.1.0"

View File

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

View File

@ -17,10 +17,15 @@
use std::collections::HashMap; use std::collections::HashMap;
use page::{Page, PageHandler}; use page::{Page, PageHandler};
extern crate parity_status;
extern crate parity_wallet; extern crate parity_wallet;
pub type Pages = HashMap<String, Box<Page>>; 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 { pub fn all_pages() -> Pages {
let mut pages = Pages::new(); let mut pages = Pages::new();
wallet_page(&mut pages); wallet_page(&mut pages);
@ -34,4 +39,3 @@ fn wallet_page(pages: &mut Pages) {
#[cfg(not(feature = "parity-wallet"))] #[cfg(not(feature = "parity-wallet"))]
fn wallet_page(_pages: &mut Pages) {} fn wallet_page(_pages: &mut Pages) {}

View File

@ -60,7 +60,7 @@ impl WebappServer {
let cors_domain = jsonrpc_http_server::AccessControlAllowOrigin::Null; let cors_domain = jsonrpc_http_server::AccessControlAllowOrigin::Null;
let rpc = ServerHandler::new(handler, cors_domain); 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)) try!(hyper::Server::http(addr.as_ref() as &str))
.handle_threads(router, threads) .handle_threads(router, threads)

View File

@ -17,12 +17,14 @@
//! Router implementation //! Router implementation
use hyper; use hyper;
use page::Page;
use apps::Pages; use apps::Pages;
use iron::request::Url; use iron::request::Url;
use jsonrpc_http_server::ServerHandler; use jsonrpc_http_server::ServerHandler;
pub struct Router { pub struct Router {
rpc: ServerHandler, rpc: ServerHandler,
main_page: Box<Page>,
pages: Pages, pages: Pages,
} }
@ -33,15 +35,19 @@ impl hyper::server::Handler for Router {
Some(ref url) if self.pages.contains_key(url) => { Some(ref url) if self.pages.contains_key(url) => {
self.pages.get(url).unwrap().handle(req, res); 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 { impl Router {
pub fn new(rpc: ServerHandler, pages: Pages) -> Self { pub fn new(rpc: ServerHandler, main_page: Box<Page>, pages: Pages) -> Self {
Router { Router {
rpc: rpc, rpc: rpc,
main_page: main_page,
pages: pages, 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>) { 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); let url = Router::extract_url(&req);
match url { match url {
Some(url) => { Some(ref url) if url.path.len() > 1 => {
let part = url.path[0].clone(); let part = url.path[0].clone();
let url = url.path[1..].join("/"); let url = url.path[1..].join("/");
req.uri = hyper::uri::RequestUri::AbsolutePath(url); req.uri = hyper::uri::RequestUri::AbsolutePath(url);
(Some(part), req) (Some(part), req)
}, },
None => { Some(url) => {
let url = url.path.join("/");
req.uri = hyper::uri::RequestUri::AbsolutePath(url);
(None, req)
},
_ => {
(None, req) (None, req)
} }
} }