Skip to content

Instantly share code, notes, and snippets.

@gsquire
Created April 29, 2015 18:21
Show Gist options
  • Save gsquire/82c51f9bfacaee5b6d60 to your computer and use it in GitHub Desktop.
Save gsquire/82c51f9bfacaee5b6d60 to your computer and use it in GitHub Desktop.
Rust Compiler Panic
extern crate hyper;
#[macro_use]
extern crate nickel;
extern crate rustc_serialize;
use std::io::Read;
use hyper::Client;
use nickel::{Nickel, HttpRouter, QueryString};
use rustc_serialize::json;
static ENDPOINT: &'static str = "http://api.hostip.info/get_json.php?ip=";
#[derive(RustcDecodable, RustcEncodable)]
pub struct IpInfo {
country: String,
country_code: String,
city: String,
ip: String
}
fn get_ip_info(ip: &str) -> IpInfo {
let mut client = Client::new();
let q = format!("{}{}", ENDPOINT, ip);
let query: &str = q.as_ref();
let mut res = client.get(query).send().unwrap();
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
let ip_info: IpInfo = json::decode(&body.as_ref()).unwrap();
ip_info
}
fn main() {
let mut server = Nickel::new();
let mut router = Nickel::router();
router.get("/ip", middleware! { |request|
let q_string = request.query("addr", "129.65.70.10").as_ref();
let addr = q_string[0];
println!("Got: {:?}", addr);
"Hi"
} );
server.utilize(router);
server.listen("127.0.0.1:6767");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment