Skip to content
This repository was archived by the owner on Jun 21, 2020. It is now read-only.

Commit 1f30e91

Browse files
Bastian Gruberyoshuawuyts
Bastian Gruber
authored andcommitted
Write docs (#19)
* Add first example, update README * Update README.md * Formatting * Update README.md * Add example to lib.rs * Add main() to examples so it compiles * cargo fmt * Add dev-dependency for http-service-hyper * fmt * fmt * Use `run` instead of `serve` * fmt * Change back to run * Fix dependencies * Fmt, update example * Remove not needed line in docs * Ignore doc code
1 parent 90efbfb commit 1f30e91

File tree

6 files changed

+208
-14
lines changed

6 files changed

+208
-14
lines changed

.gitignore

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
# Generated by Cargo
32
# will have compiled files and executables
43
/target/
@@ -9,8 +8,3 @@ Cargo.lock
98

109
# These are backup files generated by rustfmt
1110
**/*.rs.bk
12-
=======
13-
/target
14-
**/*.rs.bk
15-
Cargo.lock
16-
>>>>>>> Initial commit

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ members = ["http-service-hyper", "http-service-mock"]
1414
bytes = "0.4.12"
1515
http = "0.1.17"
1616

17+
[dev-dependencies]
18+
http-service-hyper = { path = "http-service-hyper", version = "0.1.1" }
19+
1720
[dependencies.futures-preview]
1821
version = "0.3.0-alpha.14"

README.md

+110-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,116 @@
1-
# http-service
1+
<h1 align="center">http-service</h1>
2+
<div align="center">
3+
<strong>
4+
Types and traits to help you implement your own HTTP server
5+
</strong>
6+
</div>
27

3-
[![build status][1]][2]
8+
<br />
49

5-
Types and traits giving an interface between low-level http server implementations
6-
and services that use them. The interface is based on the `std::futures` API.
10+
<div align="center">
11+
<!-- Crates version -->
12+
<a href="https://crates.io/crates/http-service">
13+
<img src="https://img.shields.io/crates/v/http-service.svg?style=flat-square"
14+
alt="Crates.io version" />
15+
</a>
16+
<!-- Build Status -->
17+
<a href="https://travis-ci.org/rust-net-web/http-service">
18+
<img src="https://img.shields.io/travis/rust-net-web/http-service.svg?style=flat-square"
19+
alt="Build Status" />
20+
</a>
21+
<!-- Downloads -->
22+
<a href="https://crates.io/crates/http-service">
23+
<img src="https://img.shields.io/crates/d/http-service.svg?style=flat-square"
24+
alt="Download" />
25+
</a>
26+
<!-- docs.rs docs -->
27+
<a href="https://docs.rs/http-service/0.1.5/http_service">
28+
<img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square"
29+
alt="docs.rs docs" />
30+
</a>
31+
</div>
32+
33+
<div align="center">
34+
<h3>
35+
<a href="https://docs.rs/http-service/0.1.5/http_service/">
36+
API Docs
37+
</a>
38+
<span> | </span>
39+
<a href="https://discordapp.com/channels/442252698964721669/474974025454452766">
40+
Chat
41+
</a>
42+
</h3>
43+
</div>
44+
45+
<div align="center">
46+
<sub>Built with ⛵ by <a href="https://github.com/rustasync">The Rust Async Ecosystem WG</a>
47+
</div>
48+
49+
## About
50+
The crate `http-service` provides the necessary types and traits to implement your own HTTP Server. It uses `hyper` for the lower level TCP abstraction.
51+
52+
You can use the workspace member [`http-service-hyper`](https://crates.io/crates/http-service-hyper) to run your HTTP Server via `http_service_hyper::serve(HTTP_SERVICE, ADDRESS);`
53+
54+
This crate uses the latest [Futures](https://github.com/rust-lang-nursery/futures-rs) preview, and therefore needs to be run on Rust Nightly.
55+
56+
## Examples
57+
58+
**Cargo.toml**
59+
```
60+
[dependencies]
61+
http-service = "0.1.5"
62+
futures-preview = "0.3.0-alpha.14"
63+
64+
[dependencies.http-service-hyper]
65+
version = "0.1.1"
66+
```
67+
68+
**main.rs**
69+
```rust
70+
#![feature(futures_api, async_await, await_macro, existential_type)]
71+
72+
use futures::future::{self, FutureObj};
73+
use http_service::{HttpService, Response};
74+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
75+
76+
struct Server {
77+
message: Vec<u8>,
78+
}
79+
80+
impl Server {
81+
fn create(message: Vec<u8>) -> Server {
82+
Server { message }
83+
}
84+
85+
pub fn run(s: Server) {
86+
let a = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
87+
http_service_hyper::run(s, a);
88+
}
89+
}
90+
91+
impl HttpService for Server {
92+
type Connection = ();
93+
type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
94+
type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
95+
96+
fn connect(&self) -> Self::ConnectionFuture {
97+
future::ok(())
98+
}
99+
100+
fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
101+
let message = self.message.clone();
102+
FutureObj::new(Box::new(async move {
103+
Ok(Response::new(http_service::Body::from(message)))
104+
}))
105+
}
106+
}
107+
108+
fn main() {
109+
let s = Server::create(String::from("Hello, World").into_bytes());
110+
Server::run(s);
111+
}
112+
```
7113

8114
## License
9115

10116
[MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE)
11-
12-
[1]: https://img.shields.io/travis/rust-net-web/http-service.svg?style=flat-square
13-
[2]: https://travis-ci.org/rust-net-web/http-service

examples/simple_response.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![feature(futures_api, async_await, await_macro, existential_type)]
2+
3+
use futures::future::{self, FutureObj};
4+
use http_service::{HttpService, Response};
5+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
6+
7+
struct Server {
8+
message: Vec<u8>,
9+
}
10+
11+
impl Server {
12+
fn create(message: Vec<u8>) -> Server {
13+
Server { message }
14+
}
15+
16+
pub fn run(s: Server) {
17+
let a = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
18+
http_service_hyper::run(s, a);
19+
}
20+
}
21+
22+
impl HttpService for Server {
23+
type Connection = ();
24+
type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
25+
type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
26+
27+
fn connect(&self) -> Self::ConnectionFuture {
28+
future::ok(())
29+
}
30+
31+
fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
32+
let message = self.message.clone();
33+
FutureObj::new(Box::new(async move {
34+
Ok(Response::new(http_service::Body::from(message)))
35+
}))
36+
}
37+
}
38+
39+
fn main() {
40+
let s = Server::create(String::from("Hello, World").into_bytes());
41+
Server::run(s);
42+
}

http-service-hyper/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ version = "0.1.1"
1313

1414
[dependencies]
1515
http = "0.1"
16-
http-service = "0.1.5"
16+
http-service = { version = "0.1.5", path = ".." }
1717
hyper = "0.12.27"
1818

1919
[dependencies.futures-preview]

src/lib.rs

+52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
//! Types and traits giving an interface between low-level http server implementations
22
//! and services that use them. The interface is based on the `std::futures` API.
3+
//!
4+
//! ## Example
5+
//! ```no_run, rust, ignore
6+
//! #![feature(futures_api, async_await, await_macro, existential_type)]
7+
//!
8+
//! use futures::{
9+
//! future::{self, FutureObj},
10+
//! };
11+
//! use http_service::{HttpService, Response};
12+
//! use std::net::{IpAddr, Ipv4Addr, SocketAddr};
13+
//!
14+
//! struct Server {
15+
//! message: Vec<u8>,
16+
//! }
17+
//!
18+
//! impl Server {
19+
//! fn create(message: Vec<u8>) -> Server {
20+
//! Server {
21+
//! message,
22+
//! }
23+
//! }
24+
//!
25+
//! pub fn run(s: Server) {
26+
//! let a = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
27+
//! http_service_hyper::run(s, a);
28+
//! }
29+
//! }
30+
//!
31+
//! impl HttpService for Server {
32+
//! type Connection = ();
33+
//! type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
34+
//! type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
35+
//!
36+
//! fn connect(&self) -> Self::ConnectionFuture {
37+
//! future::ok(())
38+
//! }
39+
//!
40+
//! fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
41+
//! let message = self.message.clone();
42+
//! FutureObj::new(Box::new(
43+
//! async move {
44+
//! Ok(Response::new(http_service::Body::from(message)))
45+
//! }
46+
//! ))
47+
//! }
48+
//! }
49+
//!
50+
//! fn main() {
51+
//! let s = Server::create(String::from("Hello, World").into_bytes());
52+
//! Server::run(s);
53+
//! }
54+
//! ```
355
456
#![forbid(future_incompatible, rust_2018_idioms)]
557
#![deny(missing_debug_implementations, nonstandard_style)]

0 commit comments

Comments
 (0)