Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Last active January 29, 2020 01:51
Show Gist options
  • Save huseyinyilmaz/0e4f915f455181bb458076c4f6215c81 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/0e4f915f455181bb458076c4f6215c81 to your computer and use it in GitHub Desktop.
test server with hyper and tower server.
#![deny(warnings)]
#![forbid(future_incompatible, rust_2018_idioms)]
#![deny(missing_debug_implementations, nonstandard_style)]
use std::task::{Context, Poll};
// use std::convert::Infallible;
use::std::future;
use hyper::service::Service;
use hyper::{Body, Request, Response, Server};
const ROOT: &str = "/";
#[derive(Debug)]
pub struct Svc;
impl Service<Request<Body>> for Svc {
type Response = Response<Body>;
type Error = hyper::Error;
type Future = future::Future;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
let rsp = Response::builder();
let uri = req.uri();
if uri.path() != ROOT {
let body = Body::from(Vec::new());
let rsp = rsp.status(404).body(body).unwrap();
return future::ok(rsp);
}
// let body = Body::from(Vec::from(&b"heyo!"[..]));
// let rsp = rsp.status(200).body(body).unwrap();
// future::ok(rsp)
hello(req)
}
}
pub struct MakeSvc;
impl<T> Service<T> for MakeSvc {
type Response = Svc;
type Error = std::io::Error;
type Future = future::Future<Self::Response, Self::Error>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}
fn call(&mut self, _: T) -> Self::Future {
future::ok(Svc)
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
let addr = "127.0.0.1:1337".parse().unwrap();
let server = Server::bind(&addr).serve(MakeSvc);
println!("Listening on http://{}", addr);
server.await?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment