Go to file
Bastian Gruber 6d2e1cc943 Fix dependencies 2019-04-24 17:59:30 +02:00
examples Change back to run 2019-04-23 21:49:19 +02:00
http-service-hyper Fix dependencies 2019-04-24 17:59:30 +02:00
http-service-mock fmt 2019-04-23 19:13:16 +02:00
src cargo fmt 2019-04-23 15:31:02 +02:00
.gitignore Add first example, update README 2019-04-18 20:17:38 +02:00
.travis.yml add ci 2019-04-18 17:33:44 +02:00
Cargo.toml Add dev-dependency for http-service-hyper 2019-04-23 19:07:02 +02:00
LICENSE Initial commit 2018-11-29 10:04:54 -08:00
LICENSE-APACHE Initial commit 2018-12-12 18:39:14 -05:00
LICENSE-MIT Initial commit 2018-12-12 18:39:14 -05:00
README.md Update README.md 2019-04-21 05:41:14 +02:00

README.md

http-service

Types and traits to help you implement your own HTTP server

Built with by The Rust Async Ecosystem WG

About

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.

You can use the workspace member http-service-hyper to run your HTTP Server via http_service_hyper::serve(HTTP_SERVICE, ADDRESS);

This crate uses the latest Futures preview, and therefore needs to be run on Rust Nightly.

Examples

Cargo.toml

[dependencies]
http-service = "0.1.5"
futures-preview = "0.3.0-alpha.14"

[dependencies.http-service-hyper]
version = "0.1.1"

main.rs

#![feature(futures_api, async_await, await_macro, existential_type)]

use futures::{
    future::{self, FutureObj},
};
use http_service::{HttpService, Response};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

struct Server {
    message: Vec<u8>,
}

impl Server {
    fn create(message: Vec<u8>) -> Server {
        Server {
            message,
        }
    }

    pub fn serve(s: Server) {
        let a = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
        http_service_hyper::serve(s, a);
    }
}

impl HttpService for Server {
    type Connection = ();
    type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
    type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
    
    fn connect(&self) -> Self::ConnectionFuture {
        future::ok(())
    }

    fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
        let message = self.message.clone();
        FutureObj::new(Box::new(
            async move {
                Ok(Response::new(http_service::Body::from(message)))
            }
        ))
    }
}

fn main() {
    let s = Server::create(String::from("Hello, World").into_bytes());
    Server::serve(s);
}

License

MIT OR Apache-2.0