-
-
Save jakswa/0eaf913d9b7b5c9c06cd8d99f5a50667 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// error[E0277]: the trait bound `(): actix_web::ResponseError` is not satisfied | |
// --> src/main.rs:25:32 | |
// | | |
//25 | r.method(http::Method::GET).with_async(index) | |
// | ^^^^^^^^^^ the trait `actix_web::ResponseError` is not implemented for `()` | |
// | | |
// = note: required because of the requirements on the impl of `std::convert::From<()>` for `actix_web::Error` | |
// = note: required because of the requirements on the impl of `std::convert::Into<actix_web::Error>` for `()` | |
extern crate hyper; | |
use std::io::{self, Write}; | |
use hyper::{Client, Method, Request}; | |
use hyper::rt::{self, Future, Stream}; | |
extern crate serde; | |
extern crate serde_json; | |
#[macro_use] | |
extern crate serde_derive; | |
use serde_json::{Value}; | |
extern crate actix_web; | |
use actix_web::{http, server, App, Path, Responder}; | |
fn index(info: Path<(u32, String)>) -> impl Future<Item = String, Error = ()> { | |
let client = Client::new(); | |
let url = "http://some.json.url".parse().unwrap(); | |
fetch_url(url) | |
} | |
fn main() { | |
server::new( | |
|| App::new() | |
.resource("/{id}/{name}/index.html", |r| { | |
r.method(http::Method::GET).with_async(index) | |
}) | |
).bind("127.0.0.1:8080").unwrap().run() | |
} | |
fn fetch_url(url: hyper::Uri) -> impl Future<Item = String, Error = ()> { | |
let client = Client::new(); | |
client | |
// Fetch the url... | |
.get(url) | |
// And then, if we get a response back... | |
.and_then(|res| { | |
res.into_body().concat2() | |
}) | |
// If all good, just tell the user... | |
.map(move |buffer| { | |
let parsed = serde_json::from_slice::<Value>(&buffer).unwrap(); | |
println!("Done. {:#?}", parsed[0]["TRAIN_ID"]); | |
format!("done: {:#?}", parsed[0]["TRAIN_ID"]) | |
}) | |
// If there was an error, let the user know... | |
.map_err(|err| { | |
eprintln!("Error {}", err); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment