pub mod artifact_routes;
pub mod error;
pub mod health;
pub mod limits;
pub mod logging;
pub mod npm_routes;
pub mod pypi_routes;
use std::sync::Arc;
use axum::Router;
use axum::http::{HeaderValue, header};
use axum::middleware;
use axum::response::Response;
use axum::routing::get;
use crate::App;
use crate::http::error::ApiError;
pub fn router(app: Arc<App>) -> Router {
Router::new()
.route("/health/live", get(health::live))
.route("/health/ready", get(health::ready))
.route("/health/delivery", get(health::delivery))
.route("/npm/-/ping", get(npm_routes::ping))
.route("/npm/-", get(unsupported_npm_api))
.route("/npm/-/{*rest}", get(unsupported_npm_api))
.route("/npm/{package}", get(npm_routes::package))
.route(
"/npm/{package}/{version_or_tag}",
get(npm_routes::package_version),
)
.route("/pypi/simple/", get(pypi_routes::index))
.route("/pypi/simple/{project}/", get(pypi_routes::project))
.route(
"/pypi/simple/{project}",
get(pypi_routes::project_without_slash),
)
.route(
"/npm/artifacts/{reference_id}/{filename}",
get(artifact_routes::serve_npm).head(artifact_routes::serve_npm),
)
.route(
"/pypi/artifacts/{reference_id}/{filename}",
get(artifact_routes::serve_pypi).head(artifact_routes::serve_pypi),
)
.fallback(unknown_route)
.method_not_allowed_fallback(unsupported_method)
.layer(middleware::map_response(no_store))
.layer(middleware::from_fn_with_state(
Arc::clone(&app),
logging::decide,
))
.with_state(app)
}
async fn unknown_route() -> ApiError {
ApiError::NotFound
}
async fn unsupported_method() -> ApiError {
ApiError::MethodNotAllowed
}
async fn unsupported_npm_api() -> ApiError {
ApiError::NotFound
}
async fn no_store(mut response: Response) -> Response {
response
.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
response
}