refactor: split http layer
This commit is contained in:
156
src/app.rs
156
src/app.rs
@@ -1,156 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
config::AppConfig, error::AppError, pokemon::PokeApiClient, rate_limit::RateLimiter,
|
|
||||||
service::PokedexService, telemetry::Telemetry, translation::TranslationClient,
|
|
||||||
};
|
|
||||||
use axum::{
|
|
||||||
Json, Router,
|
|
||||||
extract::{MatchedPath, Path, Request, State},
|
|
||||||
http::{StatusCode, header},
|
|
||||||
middleware,
|
|
||||||
response::{IntoResponse, Response},
|
|
||||||
routing::get,
|
|
||||||
};
|
|
||||||
use serde::Serialize;
|
|
||||||
use std::{sync::Arc, time::Instant};
|
|
||||||
use tower::ServiceBuilder;
|
|
||||||
use tower_http::{
|
|
||||||
ServiceBuilderExt,
|
|
||||||
request_id::MakeRequestUuid,
|
|
||||||
trace::{DefaultOnResponse, TraceLayer},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct AppState {
|
|
||||||
service: Arc<PokedexService>,
|
|
||||||
telemetry: Arc<Telemetry>,
|
|
||||||
rate_limiter: Arc<RateLimiter>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppState {
|
|
||||||
#[must_use]
|
|
||||||
pub fn new(service: PokedexService, telemetry: Telemetry, rate_limiter: RateLimiter) -> Self {
|
|
||||||
Self {
|
|
||||||
service: Arc::new(service),
|
|
||||||
telemetry: Arc::new(telemetry),
|
|
||||||
rate_limiter: Arc::new(rate_limiter),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_config(config: &AppConfig) -> Result<Self, AppError> {
|
|
||||||
let telemetry = Telemetry::new(&config.service_name);
|
|
||||||
let pokemon = PokeApiClient::new(&config.pokeapi_base_url, config.request_timeout)?;
|
|
||||||
let translations =
|
|
||||||
TranslationClient::new(&config.translations_base_url, config.request_timeout)?;
|
|
||||||
let service = PokedexService::new(pokemon, translations, telemetry.metrics());
|
|
||||||
Ok(Self::new(
|
|
||||||
service,
|
|
||||||
telemetry,
|
|
||||||
RateLimiter::new(config.rate_limit_per_second, config.rate_limit_burst)
|
|
||||||
.map_err(|_| AppError::Internal)?,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_app(state: AppState) -> Router {
|
|
||||||
let tracing_layers = ServiceBuilder::new()
|
|
||||||
.set_x_request_id(MakeRequestUuid)
|
|
||||||
.layer(
|
|
||||||
TraceLayer::new_for_http()
|
|
||||||
.make_span_with(|request: &Request| {
|
|
||||||
let request_id = request
|
|
||||||
.headers()
|
|
||||||
.get("x-request-id")
|
|
||||||
.and_then(|value| value.to_str().ok())
|
|
||||||
.unwrap_or("unknown");
|
|
||||||
tracing::info_span!(
|
|
||||||
"http.request",
|
|
||||||
method = %request.method(),
|
|
||||||
uri = %request.uri(),
|
|
||||||
request_id = %request_id,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.on_response(DefaultOnResponse::new()),
|
|
||||||
)
|
|
||||||
.propagate_x_request_id();
|
|
||||||
|
|
||||||
Router::new()
|
|
||||||
.route("/health", get(health))
|
|
||||||
.route("/metrics", get(metrics))
|
|
||||||
.route("/pokemon/{name}", get(get_pokemon))
|
|
||||||
.route("/pokemon/translated/{name}", get(get_translated_pokemon))
|
|
||||||
.with_state(state.clone())
|
|
||||||
.layer(middleware::from_fn_with_state(
|
|
||||||
state.clone(),
|
|
||||||
enforce_rate_limit,
|
|
||||||
))
|
|
||||||
.layer(middleware::from_fn_with_state(state, record_http_metrics))
|
|
||||||
.layer(tracing_layers)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn health() -> Json<HealthResponse> {
|
|
||||||
Json(HealthResponse { status: "ok" })
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn metrics(State(state): State<AppState>) -> Result<impl IntoResponse, AppError> {
|
|
||||||
let body = state.telemetry.render_prometheus()?;
|
|
||||||
Ok(([(header::CONTENT_TYPE, "text/plain; version=0.0.4")], body))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn enforce_rate_limit(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
req: Request,
|
|
||||||
next: middleware::Next,
|
|
||||||
) -> Response {
|
|
||||||
if state.rate_limiter.try_acquire() {
|
|
||||||
next.run(req).await
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
StatusCode::TOO_MANY_REQUESTS,
|
|
||||||
Json(serde_json::json!({ "error": "rate limit exceeded" })),
|
|
||||||
)
|
|
||||||
.into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn record_http_metrics(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
req: Request,
|
|
||||||
next: middleware::Next,
|
|
||||||
) -> Response {
|
|
||||||
let start = Instant::now();
|
|
||||||
let method = req.method().clone();
|
|
||||||
let path = req.extensions().get::<MatchedPath>().map_or_else(
|
|
||||||
|| req.uri().path().to_owned(),
|
|
||||||
|path| path.as_str().to_owned(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let response = next.run(req).await;
|
|
||||||
let status = response.status();
|
|
||||||
state.telemetry.metrics().record_http_request(
|
|
||||||
method.as_str(),
|
|
||||||
&path,
|
|
||||||
status.as_u16(),
|
|
||||||
start.elapsed(),
|
|
||||||
);
|
|
||||||
|
|
||||||
response
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_pokemon(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
Path(name): Path<String>,
|
|
||||||
) -> Result<Json<crate::domain::PokemonInfo>, AppError> {
|
|
||||||
state.service.get_basic(&name).await.map(Json)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_translated_pokemon(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
Path(name): Path<String>,
|
|
||||||
) -> Result<Json<crate::domain::PokemonInfo>, AppError> {
|
|
||||||
state.service.get_translated(&name).await.map(Json)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
struct HealthResponse {
|
|
||||||
status: &'static str,
|
|
||||||
}
|
|
||||||
@@ -2,16 +2,16 @@
|
|||||||
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||||
#![allow(clippy::missing_errors_doc, clippy::multiple_crate_versions)]
|
#![allow(clippy::missing_errors_doc, clippy::multiple_crate_versions)]
|
||||||
|
|
||||||
use pokedex_api::{app, config::AppConfig, telemetry};
|
use pokedex_api::{config::AppConfig, http, telemetry};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
telemetry::init_tracing();
|
telemetry::init_tracing();
|
||||||
let config = AppConfig::from_env()?;
|
let config = AppConfig::from_env()?;
|
||||||
let listener = tokio::net::TcpListener::bind(config.bind_addr).await?;
|
let listener = tokio::net::TcpListener::bind(config.bind_addr).await?;
|
||||||
let state = app::AppState::from_config(&config)?;
|
let state = http::AppState::from_config(&config)?;
|
||||||
|
|
||||||
axum::serve(listener, app::create_app(state))
|
axum::serve(listener, http::create_app(state))
|
||||||
.with_graceful_shutdown(shutdown_signal())
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
37
src/http/handlers.rs
Normal file
37
src/http/handlers.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use super::AppState;
|
||||||
|
use crate::{domain::PokemonInfo, error::AppError};
|
||||||
|
use axum::{
|
||||||
|
Json,
|
||||||
|
extract::{Path, State},
|
||||||
|
http::header,
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
pub(super) async fn health() -> Json<HealthResponse> {
|
||||||
|
Json(HealthResponse { status: "ok" })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn metrics(State(state): State<AppState>) -> Result<impl IntoResponse, AppError> {
|
||||||
|
let body = state.telemetry.render_prometheus()?;
|
||||||
|
Ok(([(header::CONTENT_TYPE, "text/plain; version=0.0.4")], body))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn get_pokemon(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path(name): Path<String>,
|
||||||
|
) -> Result<Json<PokemonInfo>, AppError> {
|
||||||
|
state.service.get_basic(&name).await.map(Json)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn get_translated_pokemon(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path(name): Path<String>,
|
||||||
|
) -> Result<Json<PokemonInfo>, AppError> {
|
||||||
|
state.service.get_translated(&name).await.map(Json)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct HealthResponse {
|
||||||
|
status: &'static str,
|
||||||
|
}
|
||||||
49
src/http/middleware.rs
Normal file
49
src/http/middleware.rs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
use super::AppState;
|
||||||
|
use axum::{
|
||||||
|
Json,
|
||||||
|
extract::{MatchedPath, Request, State},
|
||||||
|
http::StatusCode,
|
||||||
|
middleware,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub(super) async fn enforce_rate_limit(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
req: Request,
|
||||||
|
next: middleware::Next,
|
||||||
|
) -> Response {
|
||||||
|
if state.rate_limiter.try_acquire() {
|
||||||
|
next.run(req).await
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
StatusCode::TOO_MANY_REQUESTS,
|
||||||
|
Json(serde_json::json!({ "error": "rate limit exceeded" })),
|
||||||
|
)
|
||||||
|
.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn record_http_metrics(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
req: Request,
|
||||||
|
next: middleware::Next,
|
||||||
|
) -> Response {
|
||||||
|
let start = Instant::now();
|
||||||
|
let method = req.method().clone();
|
||||||
|
let path = req.extensions().get::<MatchedPath>().map_or_else(
|
||||||
|
|| req.uri().path().to_owned(),
|
||||||
|
|path| path.as_str().to_owned(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = next.run(req).await;
|
||||||
|
let status = response.status();
|
||||||
|
state.telemetry.metrics().record_http_request(
|
||||||
|
method.as_str(),
|
||||||
|
&path,
|
||||||
|
status.as_u16(),
|
||||||
|
start.elapsed(),
|
||||||
|
);
|
||||||
|
|
||||||
|
response
|
||||||
|
}
|
||||||
89
src/http/mod.rs
Normal file
89
src/http/mod.rs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
mod handlers;
|
||||||
|
mod middleware;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
config::AppConfig, error::AppError, pokemon::PokeApiClient, rate_limit::RateLimiter,
|
||||||
|
service::PokedexService, telemetry::Telemetry, translation::TranslationClient,
|
||||||
|
};
|
||||||
|
use axum::{Router, extract::Request, middleware as axum_middleware, routing::get};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tower::ServiceBuilder;
|
||||||
|
use tower_http::{
|
||||||
|
ServiceBuilderExt,
|
||||||
|
request_id::MakeRequestUuid,
|
||||||
|
trace::{DefaultOnResponse, TraceLayer},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AppState {
|
||||||
|
pub(super) service: Arc<PokedexService>,
|
||||||
|
pub(super) telemetry: Arc<Telemetry>,
|
||||||
|
pub(super) rate_limiter: Arc<RateLimiter>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppState {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(service: PokedexService, telemetry: Telemetry, rate_limiter: RateLimiter) -> Self {
|
||||||
|
Self {
|
||||||
|
service: Arc::new(service),
|
||||||
|
telemetry: Arc::new(telemetry),
|
||||||
|
rate_limiter: Arc::new(rate_limiter),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_config(config: &AppConfig) -> Result<Self, AppError> {
|
||||||
|
let telemetry = Telemetry::new(&config.service_name);
|
||||||
|
let pokemon = PokeApiClient::new(&config.pokeapi_base_url, config.request_timeout)?;
|
||||||
|
let translations =
|
||||||
|
TranslationClient::new(&config.translations_base_url, config.request_timeout)?;
|
||||||
|
let service = PokedexService::new(pokemon, translations, telemetry.metrics());
|
||||||
|
Ok(Self::new(
|
||||||
|
service,
|
||||||
|
telemetry,
|
||||||
|
RateLimiter::new(config.rate_limit_per_second, config.rate_limit_burst)
|
||||||
|
.map_err(|_| AppError::Internal)?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_app(state: AppState) -> Router {
|
||||||
|
let tracing_layers = ServiceBuilder::new()
|
||||||
|
.set_x_request_id(MakeRequestUuid)
|
||||||
|
.layer(
|
||||||
|
TraceLayer::new_for_http()
|
||||||
|
.make_span_with(|request: &Request| {
|
||||||
|
let request_id = request
|
||||||
|
.headers()
|
||||||
|
.get("x-request-id")
|
||||||
|
.and_then(|value| value.to_str().ok())
|
||||||
|
.unwrap_or("unknown");
|
||||||
|
tracing::info_span!(
|
||||||
|
"http.request",
|
||||||
|
method = %request.method(),
|
||||||
|
uri = %request.uri(),
|
||||||
|
request_id = %request_id,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.on_response(DefaultOnResponse::new()),
|
||||||
|
)
|
||||||
|
.propagate_x_request_id();
|
||||||
|
|
||||||
|
Router::new()
|
||||||
|
.route("/health", get(handlers::health))
|
||||||
|
.route("/metrics", get(handlers::metrics))
|
||||||
|
.route("/pokemon/{name}", get(handlers::get_pokemon))
|
||||||
|
.route(
|
||||||
|
"/pokemon/translated/{name}",
|
||||||
|
get(handlers::get_translated_pokemon),
|
||||||
|
)
|
||||||
|
.with_state(state.clone())
|
||||||
|
.layer(axum_middleware::from_fn_with_state(
|
||||||
|
state.clone(),
|
||||||
|
middleware::enforce_rate_limit,
|
||||||
|
))
|
||||||
|
.layer(axum_middleware::from_fn_with_state(
|
||||||
|
state,
|
||||||
|
middleware::record_http_metrics,
|
||||||
|
))
|
||||||
|
.layer(tracing_layers)
|
||||||
|
}
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||||
#![allow(clippy::missing_errors_doc, clippy::multiple_crate_versions)]
|
#![allow(clippy::missing_errors_doc, clippy::multiple_crate_versions)]
|
||||||
|
|
||||||
pub mod app;
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod domain;
|
pub mod domain;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod http;
|
||||||
pub mod pokemon;
|
pub mod pokemon;
|
||||||
pub mod rate_limit;
|
pub mod rate_limit;
|
||||||
pub mod service;
|
pub mod service;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use axum::{
|
|||||||
http::{Request, StatusCode, header::HeaderName},
|
http::{Request, StatusCode, header::HeaderName},
|
||||||
};
|
};
|
||||||
use pokedex_api::{
|
use pokedex_api::{
|
||||||
app::{AppState, create_app},
|
http::{AppState, create_app},
|
||||||
pokemon::PokeApiClient,
|
pokemon::PokeApiClient,
|
||||||
rate_limit::RateLimiter,
|
rate_limit::RateLimiter,
|
||||||
service::PokedexService,
|
service::PokedexService,
|
||||||
|
|||||||
Reference in New Issue
Block a user