-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from stephank/hyper-0.13
Hyper 0.13
- Loading branch information
Showing
14 changed files
with
493 additions
and
677 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,41 @@ | ||
extern crate futures; | ||
extern crate http; | ||
extern crate hyper; | ||
extern crate hyper_staticfile; | ||
|
||
// This example serves the docs from `target/doc/`. | ||
// | ||
// Run `cargo doc && cargo run --example doc_server`, then | ||
// point your browser to http://localhost:3000/ | ||
|
||
use futures::{future, Async::*, Future, Poll}; | ||
use futures_util::future; | ||
use http::response::Builder as ResponseBuilder; | ||
use http::{header, Request, Response, StatusCode}; | ||
use hyper::Body; | ||
use hyper_staticfile::{Static, StaticFuture}; | ||
use std::io::Error; | ||
use http::{header, StatusCode}; | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Request, Response}; | ||
use hyper_staticfile::Static; | ||
use std::io::Error as IoError; | ||
use std::path::Path; | ||
|
||
/// Future returned from `MainService`. | ||
enum MainFuture { | ||
Root, | ||
Static(StaticFuture<Body>), | ||
} | ||
|
||
impl Future for MainFuture { | ||
type Item = Response<Body>; | ||
type Error = Error; | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
match *self { | ||
MainFuture::Root => { | ||
let res = ResponseBuilder::new() | ||
.status(StatusCode::MOVED_PERMANENTLY) | ||
.header(header::LOCATION, "/hyper_staticfile/") | ||
.body(Body::empty()) | ||
.expect("unable to build response"); | ||
Ok(Ready(res)) | ||
} | ||
MainFuture::Static(ref mut future) => future.poll(), | ||
} | ||
async fn handle_request<B>(req: Request<B>, static_: Static) -> Result<Response<Body>, IoError> { | ||
if req.uri().path() == "/" { | ||
let res = ResponseBuilder::new() | ||
.status(StatusCode::MOVED_PERMANENTLY) | ||
.header(header::LOCATION, "/hyper_staticfile/") | ||
.body(Body::empty()) | ||
.expect("unable to build response"); | ||
Ok(res) | ||
} else { | ||
static_.clone().serve(req).await | ||
} | ||
} | ||
|
||
/// Hyper `Service` implementation that serves all requests. | ||
struct MainService { | ||
static_: Static, | ||
} | ||
#[tokio::main] | ||
async fn main() { | ||
let static_ = Static::new(Path::new("target/doc/")); | ||
|
||
impl MainService { | ||
fn new() -> MainService { | ||
MainService { | ||
static_: Static::new(Path::new("target/doc/")), | ||
} | ||
} | ||
} | ||
|
||
impl hyper::service::Service for MainService { | ||
type ReqBody = Body; | ||
type ResBody = Body; | ||
type Error = Error; | ||
type Future = MainFuture; | ||
|
||
fn call(&mut self, req: Request<Body>) -> MainFuture { | ||
if req.uri().path() == "/" { | ||
MainFuture::Root | ||
} else { | ||
MainFuture::Static(self.static_.serve(req)) | ||
} | ||
} | ||
} | ||
let make_service = make_service_fn(|_| { | ||
let static_ = static_.clone(); | ||
future::ok::<_, hyper::Error>(service_fn(move |req| handle_request(req, static_.clone()))) | ||
}); | ||
|
||
/// Application entry point. | ||
fn main() { | ||
let addr = ([127, 0, 0, 1], 3000).into(); | ||
let server = hyper::Server::bind(&addr) | ||
.serve(|| future::ok::<_, Error>(MainService::new())) | ||
.map_err(|e| eprintln!("server error: {}", e)); | ||
let server = hyper::Server::bind(&addr).serve(make_service); | ||
eprintln!("Doc server running on http://{}/", addr); | ||
hyper::rt::run(server); | ||
server.await.expect("Server failed"); | ||
} |
This file contains 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
Oops, something went wrong.