-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
63 lines (55 loc) · 2.23 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod config;
use axum::{
body::{Body, Bytes},
extract::{Path, Request, State},
response::{IntoResponse, Response},
routing::any,
Json, Router,
};
use axum_macros::debug_handler;
use dotenv::dotenv;
use http::{header, StatusCode};
use reqwest::Method;
use tokio::net::TcpListener;
use crate::config::GatewayConfig;
#[tokio::main]
async fn main() {
dotenv().ok();
let config = config::load_config("gateway_config.toml");
// TODO add middleware to match service name and hit auth middleware if not bypassed for service
let app = Router::new()
.route("/:service/*destination", any(handle_inbound_req))
.with_state(config);
let host = std::env::var("GATEWAY_HOST").unwrap();
let port = std::env::var("GATEWAY_PORT").unwrap();
let listener = TcpListener::bind(format!("{}:{}", host, port))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
#[debug_handler]
async fn handle_inbound_req(
State(config): State<GatewayConfig>,
Path((service_name, dest)): Path<(String, String)>,
inbound_req: Request,
) -> Response {
match config.services.get(&service_name) {
Some(service) => {
let client = reqwest::Client::new();
let method = Method::from_bytes(inbound_req.method().as_str().as_bytes())
.expect("Unable to parse request method");
let req = client.request(
method,
format!("{}:{}/{}", service.host, service.port, dest),
);
//TODO get responses right - try to pass the reqwest response straight through
// TODO figure out best way to return a 404 if service name not found
// ? Might be better to build a middleware that extracts service name and mounts service on the router and 404s otherwise rather than do it in the handler. That way by the time we get to a handler the service is already in the route state. I think this is the way.
let res = req.send().await.unwrap();
let res_headers = res.headers();
let content_type = res_headers.get("Content-Type").unwrap();
res.bytes().await.unwrap().into_response()
}
None => StatusCode::NOT_FOUND.into_response(),
}
}