Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update actions/checkout action to v4 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
63 changes: 57 additions & 6 deletions src/main.rs
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(),
}
}
Loading