mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
add backend server for http proxy example
This commit is contained in:
parent
85af2bce54
commit
27de52b5d5
562
Cargo.lock
generated
562
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,6 +4,14 @@ version = "0.1.0"
|
|||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
workspace = "../"
|
workspace = "../"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "proxy"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "server"
|
||||||
|
path = "src/server.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
env_logger = "0.5"
|
env_logger = "0.5"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
|
13
http-proxy/README.md
Normal file
13
http-proxy/README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
## Http proxy example
|
||||||
|
|
||||||
|
To start proxy server:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run --bin proxy
|
||||||
|
```
|
||||||
|
|
||||||
|
To start local backend server:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run --bin server
|
||||||
|
```
|
@ -1,17 +1,15 @@
|
|||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate futures;
|
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
extern crate futures;
|
||||||
|
|
||||||
|
use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage,
|
||||||
|
HttpRequest, HttpResponse};
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
use actix_web::{
|
|
||||||
client, server, middleware,
|
|
||||||
App, AsyncResponder, Body, HttpRequest, HttpResponse, HttpMessage, Error};
|
|
||||||
|
|
||||||
|
|
||||||
/// Stream client request response and then send body to a server response
|
/// Stream client request response and then send body to a server response
|
||||||
fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
fn index(_req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||||
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
client::ClientRequest::get("http://127.0.0.1:8081/")
|
||||||
.finish().unwrap()
|
.finish().unwrap()
|
||||||
.send()
|
.send()
|
||||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
.map_err(Error::from) // <- convert SendRequestError to an Error
|
||||||
@ -25,7 +23,7 @@ fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// streaming client request to a streaming server response
|
/// streaming client request to a streaming server response
|
||||||
fn streaming(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
fn streaming(_req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||||
// send client request
|
// send client request
|
||||||
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
||||||
.finish().unwrap()
|
.finish().unwrap()
|
||||||
@ -45,12 +43,14 @@ fn main() {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("http-proxy");
|
let sys = actix::System::new("http-proxy");
|
||||||
|
|
||||||
server::new(
|
server::new(|| {
|
||||||
|| App::new()
|
App::new()
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.resource("/streaming", |r| r.f(streaming))
|
.resource("/streaming", |r| r.f(streaming))
|
||||||
.resource("/", |r| r.f(index)))
|
.resource("/", |r| r.f(index))
|
||||||
.bind("127.0.0.1:8080").unwrap()
|
}).threads(1)
|
||||||
|
.bind("127.0.0.1:8080")
|
||||||
|
.unwrap()
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8080");
|
println!("Started http server: 127.0.0.1:8080");
|
||||||
|
34
http-proxy/src/server.rs
Normal file
34
http-proxy/src/server.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
extern crate actix;
|
||||||
|
extern crate actix_web;
|
||||||
|
extern crate env_logger;
|
||||||
|
extern crate futures;
|
||||||
|
|
||||||
|
use actix_web::*;
|
||||||
|
use futures::Future;
|
||||||
|
|
||||||
|
fn index(req: HttpRequest) -> FutureResponse<HttpResponse> {
|
||||||
|
req.body()
|
||||||
|
.from_err()
|
||||||
|
.map(|bytes| HttpResponse::Ok().body(bytes))
|
||||||
|
.responder()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
::std::env::set_var("RUST_LOG", "actix_web=error");
|
||||||
|
let _ = env_logger::init();
|
||||||
|
let sys = actix::System::new("ws-example");
|
||||||
|
|
||||||
|
server::new(|| {
|
||||||
|
App::new()
|
||||||
|
// enable logger
|
||||||
|
.middleware(middleware::Logger::default())
|
||||||
|
.resource("/index.html", |r| r.f(|_| "Hello world!"))
|
||||||
|
.resource("/", |r| r.f(index))
|
||||||
|
}).threads(1)
|
||||||
|
.bind("127.0.0.1:8081")
|
||||||
|
.unwrap()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
println!("Started http server: 127.0.0.1:8081");
|
||||||
|
let _ = sys.run();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user