-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,63 @@ | ||
use std::net::SocketAddr; | ||
mod config; | ||
|
||
use config::load_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; | ||
|
||
mod config; | ||
use crate::config::GatewayConfig; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let config = load_config("gateway_config.toml"); | ||
let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); | ||
println!("config:{:?}, addr:{:?}", config, addr); | ||
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(), | ||
} | ||
} |