Supporting /api/ping for dapps server (#1543)

* Refactoring dapps to support API endpoints.

* Using ContentHandler for unauthorized requests

* Extracting url stuff

* Adding ping endpoint

* CORS support for ping request

* Fixing url.is_none()

* minor formatting fix

[ci:skip]
This commit is contained in:
Tomasz Drwięga
2016-07-07 03:42:49 -04:00
committed by Gav Wood
parent 8282c7dd50
commit 7af366c5b1
11 changed files with 233 additions and 52 deletions

View File

@@ -17,7 +17,7 @@
use serde::Serialize;
use serde_json;
use endpoint::Handler;
use handlers::ContentHandler;
use handlers::{ContentHandler, EchoHandler};
pub fn as_json<T : Serialize>(val: &T) -> Box<Handler> {
Box::new(ContentHandler::ok(serde_json::to_string(val).unwrap(), "application/json".to_owned()))
@@ -26,3 +26,11 @@ pub fn as_json<T : Serialize>(val: &T) -> Box<Handler> {
pub fn as_json_error<T : Serialize>(val: &T) -> Box<Handler> {
Box::new(ContentHandler::not_found(serde_json::to_string(val).unwrap(), "application/json".to_owned()))
}
pub fn ping_response(local_domain: &str) -> Box<Handler> {
Box::new(EchoHandler::cors(vec![
format!("http://{}", local_domain),
// Allow CORS calls also for localhost
format!("http://{}", local_domain.replace("127.0.0.1", "localhost")),
]))
}