I'm trying to make a question and answer server for a huge data structure. The user would send JSON questions to the server and the server would use the huge data structure to answer.
I'm trying to do this by implementing the hyper::server::Service
trait for my Oracle
struct.
I've got something like this:
use self::hyper::server::{Http, Service, Request, Response};
// ...other imports
struct Oracle { /* Tons of stuff */}
impl<'a> Service for &'a Oracle {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, req: Request) -> Self::Future {
match (req.method(), req.path()) {
// could be lots of question types
(&hyper::Method::Post, "/query") => {
Box::new(req.body().concat2().map(|b| {
let query: Query = deserialize_req(&b.as_ref());
let ans = get_answer(&self, &query);
Response::new()
.with_header(ContentLength(ans.len() as u64))
.with_body(ans)
}))
},
_ => {
let response = Response::new()
.with_status(hyper::StatusCode::NotFound);
Box::new(futures::future::ok(response))
},
}
}
}
This causes lifetime problems (cannot infer an appropriate lifetime due to conflicting requirements
) when I try to put &self
in a future.
My inclination is that this is totally the wrong way to approach this problem, but I'm having a hard time figuring out the best way to do this.