-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
50 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[package] | ||
name = "stream-body" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
description = "An HttpBody implementation with efficient streaming support for the Rust HTTP library hyper" | ||
homepage = "https://github.com/rousan/stream-body" | ||
repository = "https://github.com/rousan/stream-body" | ||
keywords = ["rust", "hyper", "asynchronous", "stream", "writer", "channel"] | ||
keywords = ["rust", "hyper", "asynchronous", "stream", "writer"] | ||
categories = ["asynchronous"] | ||
authors = ["Rousan Ali <[email protected]>"] | ||
readme = "README.md" | ||
|
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Request, Response, Server}; | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
use stream_body::StreamBody; | ||
use tokio::fs::File; | ||
|
||
async fn handle(_: Request<Body>) -> Result<Response<StreamBody>, Infallible> { | ||
let f = File::open("large-file.pdf").await.unwrap(); | ||
let file_size = f.metadata().await.unwrap().len(); | ||
|
||
Ok(Response::builder() | ||
.header("Content-Type", "application/pdf") | ||
.header("Content-Length", file_size.to_string()) | ||
.body(StreamBody::from_reader(f)) | ||
.unwrap()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
|
||
let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) }); | ||
|
||
let server = Server::bind(&addr).serve(make_svc); | ||
|
||
if let Err(e) = server.await { | ||
eprintln!("server error: {}", e); | ||
} | ||
} |
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