From 1bb1691f617d7a2114150a4de382868b689d6dd8 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 19 Nov 2017 21:07:41 -0500 Subject: add a fake ipfs server that just prints out requests --- ipfs-api/examples/server.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ipfs-api/examples/server.rs (limited to 'ipfs-api/examples') diff --git a/ipfs-api/examples/server.rs b/ipfs-api/examples/server.rs new file mode 100644 index 0000000..bc02c90 --- /dev/null +++ b/ipfs-api/examples/server.rs @@ -0,0 +1,58 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +extern crate futures; +extern crate hyper; + +use futures::future::Future; +use futures::stream::Stream; +use hyper::StatusCode; +use hyper::server::{Http, Service, Request, Response}; + +struct Debug; + +impl Service for Debug { + type Request = Request; + + type Response = Response; + + type Error = hyper::Error; + + type Future = Box>; + + + fn call(&self, req: Request) -> Self::Future { + println!("{:?}", req); + + let res = req.body().concat2().map(|bod| { + println!("{}", String::from_utf8_lossy(&bod)); + + Response::new().with_status(StatusCode::Ok) + }); + + Box::new(res) + } +} + + +/// This example runs a server on the default Ipfs port. All it does is +/// print requests as it gets them. It is useful for debugging. +/// +fn main() { + let addr = "127.0.0.1:5001".parse().unwrap(); + let mut server = Http::new().bind(&addr, || Ok(Debug)).unwrap(); + + server.no_proto(); + + println!( + "Listening on http://{} with 1 thread.", + server.local_addr().unwrap() + ); + + server.run().unwrap(); +} -- cgit v1.2.3