|
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> |
2 | 7 |
|
3 | | -[![build status][1]][2] |
| 8 | +<br /> |
4 | 9 |
|
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 | +``` |
7 | 113 |
|
8 | 114 | ## License |
9 | 115 |
|
10 | 116 | [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 |
|
0 commit comments