3

I'm trying to write a simple application to read a config file then spin up a hyper reverse proxy for each entry in that file

I'm parsing the config in a loop and want to be able to keep track of every server I have created, probably in a Vec. I can't figure out what type the Vec should be as each server is a different type. I have tried boxing the result of map_error and storing that but I'm not getting anywhere. The hyper example on GitHub only uses statically created servers.

This is a simplified version of what I am trying to do:

use futures::future::{self, Future};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};

type BoxFut = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send>;

fn debug_request(req: Request<Body>) -> BoxFut {
    let body_str = format!("{:?}", req);
    let response = Response::new(Body::from(body_str));
    Box::new(future::ok(response))
}

fn main() {
    // This is our socket address...
    let local_address = ([127, 0, 0, 1], 9001).into();

    // A `Service` is needed for every connection.
    let make_svc = make_service_fn(|socket: &AddrStream| {
        let remote_addr = socket.remote_addr();
        service_fn(move |req: Request<Body>| {
            // returns BoxFut

            return hyper_reverse_proxy::call(remote_addr.ip(), "http://192.9.200.162:80", req);
        })
    });

    let server = Box::new(
        Server::bind(&local_address)
            .serve(make_svc)
            .map_err(|e| eprintln!("server error: {}", e)),
    );

    let server2 = Box::new(
        Server::bind(&local_address)
            .serve(make_svc)
            .map_err(|e| eprintln!("server error: {}", e)),
    );

    let vec = vec![server, server2];

    println!("Running server on {:?}", local_address);

    // Run this server for... forever!
    let all_servers = future::join_all(vec);
    hyper::rt::run(all_servers);
}

vec can't hold both severs as they have different types and I'm not sure how to best do the hyper::rt::run.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user2327063
  • 126
  • 5
  • 1
    This is the same question as [How to store a hyper::server::Server as a field in a struct?](https://stackoverflow.com/q/56469502/155423), where it is demonstrated that storing a `hyper::Server` as a trait object is really hard. – Shepmaster Jul 31 '19 at 13:10
  • Did you ever figure this out or were able to run multiple `hyper` servers at once? – DogEatDog Jun 20 '22 at 13:23

0 Answers0