Skip to content
Merged
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
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ pub mod routes;
pub mod utils;

use axum::{
Router, middleware as axum_middleware,
Router,
extract::FromRef,
middleware as axum_middleware,
routing::{get, post},
};
use sqlx::PgPool;
Expand All @@ -28,11 +30,30 @@ use routes::proxy::proxy;
use routes::system::quota_sync::sync_to_db;
use std::sync::Arc;

#[derive(Clone)]
pub struct AppState {
pub pool: PgPool,
pub http_client: reqwest::Client,
}

impl FromRef<AppState> for PgPool {
fn from_ref(state: &AppState) -> PgPool {
state.pool.clone()
}
}

impl FromRef<AppState> for reqwest::Client {
fn from_ref(state: &AppState) -> reqwest::Client {
state.http_client.clone()
}
}

pub fn create_router(
pool: PgPool,
rate_limiter: Arc<dyn RateLimiter>,
auth_cache: Arc<dyn AuthCache>,
auth_cache_ttl_secs: u64,
http_client: reqwest::Client,
) -> Router {
let protected_routes = Router::new()
.route("/proxy/test", get(|| async { "ok" }))
Expand Down Expand Up @@ -69,5 +90,5 @@ pub fn create_router(
public_routes
.merge(protected_routes)
.merge(system_routes)
.with_state(pool)
.with_state(AppState { pool, http_client })
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ async fn main() {
}

let port = std::env::var("API_PORT").unwrap_or_else(|_| "8080".to_string());
let app = create_router(pool, rate_limiter, valkey_auth_cache, ttl_seconds);
let http_client = reqwest::Client::new();
let app = create_router(
pool,
rate_limiter,
valkey_auth_cache,
ttl_seconds,
http_client,
);

let listener = tokio::net::TcpListener::bind("0.0.0.0:".to_string() + &port)
.await
Expand Down
4 changes: 2 additions & 2 deletions src/routes/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use sqlx::PgPool;
// /proxy/{name}/{*rest_path} のリクエストを upstream_services の base_url に転送する
pub async fn proxy(
State(pool): State<PgPool>,
State(client): State<reqwest::Client>,
Extension(authed): Extension<AuthedApiKey>,
Path((name, rest_path)): Path<(String, String)>,
request: Request,
Expand Down Expand Up @@ -62,8 +63,7 @@ pub async fn proxy(
})?
.to_bytes();

// reqwest で転送
let client = reqwest::Client::new();
// reqwest で転送(共有 Client を利用してコネクションプール / keep-alive を有効化)
let mut req = client
.request(to_reqwest_method(&method), &target_url)
.body(body_bytes.to_vec());
Expand Down
1 change: 1 addition & 0 deletions tests/api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async fn setup() -> (axum::Router, PgPool, Arc<dyn RateLimiter>) {
rate_limiter.clone(),
auth_cache,
auth_cache_ttl_secs,
reqwest::Client::new(),
);
(app, pool, rate_limiter)
}
Expand Down
Loading