Skip to main content

cargo_overlay_registry/
lib.rs

1//! Cargo Overlay Registry - A proxy registry for crates.io with local publishing support.
2//!
3//! This library provides the core functionality for running a cargo registry proxy that:
4//! - Proxies requests to crates.io (or another upstream registry)
5//! - Supports local publishing of crates
6//! - Can act as an HTTP/HTTPS forward proxy for cargo
7//! - Supports MITM interception for transparent proxying
8
9use std::sync::Arc;
10
11use axum::routing::{get, put};
12use axum::Router;
13
14pub mod endpoints;
15pub mod http_proxy;
16pub mod registry;
17pub mod state;
18pub mod tls;
19pub mod types;
20
21pub use endpoints::{
22    handle_api_download, handle_api_publish, handle_api_search, handle_config, handle_index_1char,
23    handle_index_2char, handle_index_3char, handle_index_4plus,
24};
25pub use http_proxy::{handle_proxy_connection, HttpProxyState};
26pub use registry::{
27    build_registry, AnyRegistry, BuiltRegistry, DynRegistry, Registry, RegistryBuildOptions,
28    RegistrySpec,
29};
30pub use state::{GenericProxyState, MitmCa, RegistryState};
31pub use tls::generate_self_signed_cert;
32
33/// Build the standard registry router with all endpoints configured.
34pub fn build_registry_router<S: RegistryState + Clone + Send + Sync + 'static>(
35    state: Arc<S>,
36) -> Router {
37    Router::new()
38        // Index config endpoint
39        .route("/config.json", get(handle_config::<S>))
40        // Index files for 1-char package names: /1/{name}
41        .route("/1/{name}", get(handle_index_1char::<S>))
42        // Index files for 2-char package names: /2/{name}
43        .route("/2/{name}", get(handle_index_2char::<S>))
44        // Index files for 3-char package names: /3/{first_char}/{name}
45        .route("/3/{first_char}/{name}", get(handle_index_3char::<S>))
46        // Index files for 4+ char package names: /{first_two}/{second_two}/{name}
47        .route(
48            "/{first_two}/{second_two}/{name}",
49            get(handle_index_4plus::<S>),
50        )
51        // API: Search crates
52        .route("/api/v1/crates", get(handle_api_search::<S>))
53        // API: Publish crate
54        .route("/api/v1/crates/new", put(handle_api_publish::<S>))
55        // API: Download crate
56        .route(
57            "/api/v1/crates/{crate_name}/{version}/download",
58            get(handle_api_download::<S>),
59        )
60        .with_state(state)
61}