Include the IP address in JSON responses

This commit is contained in:
Frank Denis 2017-07-27 17:47:01 +02:00
parent db603cd65d
commit 25ecb8d1ad
1 changed files with 9 additions and 10 deletions

View File

@ -74,8 +74,7 @@ impl WebService {
output_type output_type
} }
fn output_json(_ip_str: &str, fn output_json(map: serde_json::Map<String, serde_json::value::Value>,
map: serde_json::Map<String, serde_json::value::Value>,
cache_header: Header<CacheControl>) cache_header: Header<CacheControl>)
-> IronResult<Response> { -> IronResult<Response> {
let json = serde_json::to_string(&map).unwrap(); let json = serde_json::to_string(&map).unwrap();
@ -85,8 +84,7 @@ impl WebService {
Ok(Response::with((status::Ok, mime_json, cache_header, json))) Ok(Response::with((status::Ok, mime_json, cache_header, json)))
} }
fn output_html(ip_str: &str, fn output_html(map: serde_json::Map<String, serde_json::value::Value>,
map: serde_json::Map<String, serde_json::value::Value>,
cache_header: Header<CacheControl>) cache_header: Header<CacheControl>)
-> IronResult<Response> { -> IronResult<Response> {
let mime_html = Mime(TopLevel::Text, let mime_html = Mime(TopLevel::Text,
@ -103,7 +101,7 @@ impl WebService {
} }
body(class="container-fluid") { body(class="container-fluid") {
header { header {
h1 { : format_args!("Information for IP address: {}", ip_str) } h1 { : format_args!("Information for IP address: {}", map.get("ip").unwrap().as_str().unwrap()) }
} }
table { table {
tr { tr {
@ -148,13 +146,12 @@ impl WebService {
} }
fn output(output_type: OutputType, fn output(output_type: OutputType,
ip_str: &str,
map: serde_json::Map<String, serde_json::value::Value>, map: serde_json::Map<String, serde_json::value::Value>,
cache_header: Header<CacheControl>) cache_header: Header<CacheControl>)
-> IronResult<Response> { -> IronResult<Response> {
match output_type { match output_type {
OutputType::Json => Self::output_json(ip_str, map, cache_header), OutputType::Json => Self::output_json(map, cache_header),
_ => Self::output_html(ip_str, map, cache_header), _ => Self::output_html(map, cache_header),
} }
} }
@ -189,11 +186,13 @@ impl WebService {
let mut map = serde_json::Map::new(); let mut map = serde_json::Map::new();
map.insert("announced".to_string(), map.insert("announced".to_string(),
serde_json::value::Value::Bool(false)); serde_json::value::Value::Bool(false));
return Self::output(Self::accept_type(&req), ip_str, map, cache_header); return Self::output(Self::accept_type(&req), map, cache_header);
} }
Some(found) => found, Some(found) => found,
}; };
let mut map = serde_json::Map::new(); let mut map = serde_json::Map::new();
map.insert("ip".to_string(),
serde_json::value::Value::String(ip_str.to_string()));
map.insert("announced".to_string(), map.insert("announced".to_string(),
serde_json::value::Value::Bool(true)); serde_json::value::Value::Bool(true));
map.insert("first_ip".to_string(), map.insert("first_ip".to_string(),
@ -206,7 +205,7 @@ impl WebService {
serde_json::value::Value::String(found.country.clone())); serde_json::value::Value::String(found.country.clone()));
map.insert("as_description".to_string(), map.insert("as_description".to_string(),
serde_json::value::Value::String(found.description.clone())); serde_json::value::Value::String(found.description.clone()));
Self::output(Self::accept_type(&req), ip_str, map, cache_header) Self::output(Self::accept_type(&req), map, cache_header)
} }
pub fn start(asns_arc: Arc<RwLock<Arc<ASNs>>>, listen_addr: &str) { pub fn start(asns_arc: Arc<RwLock<Arc<ASNs>>>, listen_addr: &str) {