60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use std::net::SocketAddr;
|
|
|
|
use figment::{
|
|
providers::{Env, Format, Yaml},
|
|
Figment,
|
|
};
|
|
use hyper::{
|
|
client::{Client, HttpConnector},
|
|
http::uri::{Authority, Parts, Scheme},
|
|
service::{make_service_fn, service_fn},
|
|
Body, Request, Response, Server, Uri,
|
|
};
|
|
use proxy::Route;
|
|
|
|
pub mod client;
|
|
mod config;
|
|
mod proxy;
|
|
|
|
async fn handle(mut req: Request<Body>) -> Result<Response<Body>, hyper::Error>
|
|
{
|
|
println!("{:?}", req);
|
|
let req_parts = req.uri().clone().into_parts();
|
|
let mut parts = Parts::default();
|
|
parts.scheme = req_parts.scheme.or_else(|| Some(Scheme::HTTP));
|
|
parts.authority = Some(Authority::from_static("localhost:5000"));
|
|
parts.path_and_query = req_parts.path_and_query;
|
|
|
|
let new_uri = Uri::from_parts(parts).unwrap();
|
|
*req.uri_mut() = new_uri;
|
|
|
|
let client: Client<HttpConnector, Body> = Client::builder().build_http();
|
|
|
|
client.request(req).await
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main()
|
|
{
|
|
// let figment = Figment::new()
|
|
// .merge(Yaml::file("Rb.yaml").nested())
|
|
// .merge(Env::prefixed("RB_").global());
|
|
// let config: config::GwConfig = figment.extract().unwrap();
|
|
|
|
// Construct our SocketAddr to listen on...
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
|
|
// And a MakeService to handle each connection...
|
|
// let make_service = make_service_fn(|_conn| async { Ok::<_, hyper::Error>(service_fn(handle)) });
|
|
|
|
// Then bind and serve...
|
|
let route = Route::new("/".into(), String::from("localhost:8000"), "/".into());
|
|
let make_svc = make_service_fn(|_| async { Ok::<_, hyper::Error>(route) });
|
|
let server = Server::bind(&addr).serve(make_svc);
|
|
|
|
// And run forever...
|
|
if let Err(e) = server.await {
|
|
eprintln!("server error: {}", e);
|
|
}
|
|
}
|