# krey-yon/linkex architecture

> Architecture diagram of the GitHub repository krey-yon/linkex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krey-yon/linkex
- Repository: https://github.com/krey-yon/linkex

## Overview

#### Purpose and runtime

`tross` is a Rust HTTP API that retrieves public LinkedIn profiles through an authenticated server-side Voyager client, normalizes LinkedIn payloads into a stable JSON contract, and protects the upstream with caching, throttling, and circuit breaking. The executable starts in `src/main.rs`; application construction and shared dependencies are assembled in `src/app.rs` and `src/state.rs`.

#### HTTP API and request boundary

Route registration and HTTP-layer behavior live under `src/api/mod.rs`, with profile routes in `src/api/profile.rs`, account/billing routes in `src/api/account.rs`, and health/session system routes in `src/api/system.rs`. Middleware in `src/api/middleware.rs` provides request-level concerns such as API-key authentication, client rate limiting, request IDs, response headers, and billable-request handling.

The external boundary is JSON-over-HTTP: callers submit a LinkedIn profile URL and receive the common response envelope represented by `src/domain/response.rs`.

#### Profile retrieval orchestration

`src/service/profile.rs` is the central use-case layer. It validates and canonicalizes the requested profile URL, determines whether cache refresh is required, coalesces concurrent identical requests, invokes the LinkedIn repository on misses, parses the result, and returns profile metadata suitable for the API layer. `src/service/mod.rs` defines the service composition boundary between transport and upstream/data concerns.

#### Cache and persistence

Profile caching is implemented in `src/service/cache.rs`. It combines bounded in-memory TTL storage with optional disk persistence, loading valid persisted entries at startup and avoiding upstream work for hits. Cache decisions feed billing costs and response metadata, while refresh requests deliberately bypass normal cache reuse. Persistent cache/session paths are deployment-sensitive because containers only permit writes beneath `/data`.

#### LinkedIn authenticated upstream client

The LinkedIn subsystem is isolated in `src/linkedin/`. `src/linkedin/client.rs` performs Voyager HTTP interactions; `src/linkedin/endpoints.rs` defines upstream request targets; and `src/linkedin/repository.rs` exposes retrieval to the service layer. Authentication and durable cookie/session lifecycle are handled by `src/linkedin/auth.rs` and `src/linkedin/session.rs`, including cookie-header credentials and optional password login.

`src/linkedin/throttle.rs` enforces upstream spacing, bounded concurrency, retries, and circuit-breaker behavior. This isolates LinkedIn-specific failures and prevents the public API from repeatedly stressing an unavailable or rate-limiting upstream.

#### Parsing and stable domain model

Raw Voyager/Dash payloads cross into a dedicated parser pipeline in `src/parser/mod.rs`. Format-specific readers—`src/parser/normalized.rs`, `src/parser/dash.rs`, and `src/parser/legacy.rs`—extract source representations; `src/parser/assembler.rs` composes the normalized result. The stable internal/public profile schema is centered on `src/domain/profile.rs`, separating volatile upstream structure from the API contract.

#### Billing, configuration, and operations

`src/billing.rs` governs credit charging/refunds and supports Redis-backed balances; account endpoints expose balance and pricing. Runtime configuration, including LinkedIn credentials, cache/session paths, Redis, raw-endpoint exposure, and operational limits, is centralized in `src/config.rs`.

Redis is the material external infrastructure for production billing, configured through `docker-compose.yml`. Container deployment is defined by `Dockerfile`; it runs one API replica per LinkedIn credential with persistent `/data` storage. `src/telemetry.rs` provides structured operational logging, while `src/error.rs` maps internal/upstream failures to stable API error codes.

Main entry points: [`src/api/mod.rs`](https://github.com/krey-yon/linkex/blob/master/src/api/mod.rs) (Routes), [`src/app.rs`](https://github.com/krey-yon/linkex/blob/master/src/app.rs) (App assembly), [`src/service/profile.rs`](https://github.com/krey-yon/linkex/blob/master/src/service/profile.rs) (Profile orchestration), [`src/api/profile.rs`](https://github.com/krey-yon/linkex/blob/master/src/api/profile.rs) (Profile endpoints)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Executable<br/>Rust entrypoint<br/>[main.rs]"]
  node_app["App assembly<br/>composition root<br/>[app.rs]"]
  node_state["Shared state<br/>dependencies<br/>[state.rs]"]
  node_errors["Error mapping<br/>stable API errors<br/>[error.rs]"]
end

subgraph group_api["Public API"]
  node_api_routes["Routes<br/>HTTP routing<br/>[mod.rs]"]
  node_middleware["Request middleware<br/>auth and billing<br/>[middleware.rs]"]
  node_profile_api["Profile endpoints<br/>HTTP handlers<br/>[profile.rs]"]
  node_account_api["Account endpoints<br/>HTTP handlers<br/>[account.rs]"]
  node_system_api["System endpoints<br/>HTTP handlers<br/>[system.rs]"]
  node_response_domain["Response envelope<br/>API contract<br/>[response.rs]"]
end

subgraph group_service["Profile Service"]
  node_profile_service["Profile orchestration<br/>use case<br/>[profile.rs]"]
  node_cache[("Profile cache<br/>memory and disk cache<br/>[cache.rs]")]
  node_billing["Billing<br/>credits<br/>[billing.rs]"]
  node_parser["Voyager parser pipeline<br/>normalization pipeline<br/>[mod.rs]"]
  node_profile_domain["Stable profile schema<br/>domain contract<br/>[profile.rs]"]
end

subgraph group_linkedin["LinkedIn Upstream"]
  node_repository["LinkedIn repository<br/>upstream boundary<br/>[repository.rs]"]
  node_voyager_client["Voyager client<br/>authenticated HTTP client<br/>[client.rs]"]
  node_auth_session["Auth and sessions<br/>credential lifecycle<br/>[auth.rs]"]
  node_throttle["Upstream protection<br/>throttle and circuit breaker<br/>[throttle.rs]"]
end

subgraph group_data["Data &amp; Operations"]
  node_config["Runtime config<br/>configuration<br/>[config.rs]"]
  node_redis[("Redis<br/>external balances store<br/>[docker-compose.yml]")]
  node_telemetry["Telemetry<br/>structured logging<br/>[telemetry.rs]"]
end

node_main -->|"starts"| node_app
node_app -->|"builds"| node_state
node_app -->|"serves"| node_api_routes
node_config -->|"configures"| node_app
node_api_routes -->|"applies"| node_middleware
node_api_routes -->|"registers"| node_profile_api
node_api_routes -->|"registers"| node_account_api
node_api_routes -->|"registers"| node_system_api
node_profile_api -->|"retrieves profile"| node_profile_service
node_profile_api -->|"returns"| node_response_domain
node_middleware -->|"handles billable requests"| node_billing
node_account_api -->|"reads balance and pricing"| node_billing
node_profile_service -->|"checks and refreshes"| node_cache
node_profile_service -->|"fetches on miss"| node_repository
node_profile_service -->|"parses payload"| node_parser
node_parser -->|"normalizes into"| node_profile_domain
node_repository -->|"protects requests with"| node_throttle
node_throttle -->|"permits calls to"| node_voyager_client
node_voyager_client -->|"uses session"| node_auth_session
node_billing -->|"stores balances in"| node_redis
node_app -->|"initializes"| node_telemetry
node_api_routes -->|"maps failures through"| node_errors

click node_main "https://github.com/krey-yon/linkex/blob/master/src/main.rs"
click node_app "https://github.com/krey-yon/linkex/blob/master/src/app.rs"
click node_state "https://github.com/krey-yon/linkex/blob/master/src/state.rs"
click node_config "https://github.com/krey-yon/linkex/blob/master/src/config.rs"
click node_api_routes "https://github.com/krey-yon/linkex/blob/master/src/api/mod.rs"
click node_middleware "https://github.com/krey-yon/linkex/blob/master/src/api/middleware.rs"
click node_profile_api "https://github.com/krey-yon/linkex/blob/master/src/api/profile.rs"
click node_account_api "https://github.com/krey-yon/linkex/blob/master/src/api/account.rs"
click node_system_api "https://github.com/krey-yon/linkex/blob/master/src/api/system.rs"
click node_profile_service "https://github.com/krey-yon/linkex/blob/master/src/service/profile.rs"
click node_cache "https://github.com/krey-yon/linkex/blob/master/src/service/cache.rs"
click node_billing "https://github.com/krey-yon/linkex/blob/master/src/billing.rs"
click node_repository "https://github.com/krey-yon/linkex/blob/master/src/linkedin/repository.rs"
click node_voyager_client "https://github.com/krey-yon/linkex/blob/master/src/linkedin/client.rs"
click node_auth_session "https://github.com/krey-yon/linkex/blob/master/src/linkedin/auth.rs"
click node_throttle "https://github.com/krey-yon/linkex/blob/master/src/linkedin/throttle.rs"
click node_parser "https://github.com/krey-yon/linkex/blob/master/src/parser/mod.rs"
click node_profile_domain "https://github.com/krey-yon/linkex/blob/master/src/domain/profile.rs"
click node_response_domain "https://github.com/krey-yon/linkex/blob/master/src/domain/response.rs"
click node_redis "https://github.com/krey-yon/linkex/blob/master/docker-compose.yml"
click node_telemetry "https://github.com/krey-yon/linkex/blob/master/src/telemetry.rs"
click node_errors "https://github.com/krey-yon/linkex/blob/master/src/error.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app,node_state,node_errors toneBlue
class node_api_routes,node_middleware,node_profile_api,node_account_api,node_system_api,node_response_domain toneAmber
class node_profile_service,node_cache,node_billing,node_parser,node_profile_domain toneMint
class node_repository,node_voyager_client,node_auth_session,node_throttle toneRose
class node_config,node_redis,node_telemetry toneIndigo
```

## Components

### Runtime

- [Executable](https://github.com/krey-yon/linkex/blob/master/src/main.rs): Rust entrypoint · `src/main.rs`
- [App assembly](https://github.com/krey-yon/linkex/blob/master/src/app.rs): composition root · `src/app.rs`
- [Shared state](https://github.com/krey-yon/linkex/blob/master/src/state.rs): dependencies · `src/state.rs`
- [Error mapping](https://github.com/krey-yon/linkex/blob/master/src/error.rs): stable API errors · `src/error.rs`

### Public API

- [Routes](https://github.com/krey-yon/linkex/blob/master/src/api/mod.rs): HTTP routing · `src/api/mod.rs`
- [Request middleware](https://github.com/krey-yon/linkex/blob/master/src/api/middleware.rs): auth and billing · API keys, client limits, request IDs, and billable handling. · `src/api/middleware.rs`
- [Profile endpoints](https://github.com/krey-yon/linkex/blob/master/src/api/profile.rs): HTTP handlers · `src/api/profile.rs`
- [Account endpoints](https://github.com/krey-yon/linkex/blob/master/src/api/account.rs): HTTP handlers · `src/api/account.rs`
- [System endpoints](https://github.com/krey-yon/linkex/blob/master/src/api/system.rs): HTTP handlers · `src/api/system.rs`
- [Response envelope](https://github.com/krey-yon/linkex/blob/master/src/domain/response.rs): API contract · `src/domain/response.rs`

### Profile Service

- [Profile orchestration](https://github.com/krey-yon/linkex/blob/master/src/service/profile.rs): use case · Validates URLs, coalesces misses, and coordinates retrieval. · `src/service/profile.rs`
- [Profile cache](https://github.com/krey-yon/linkex/blob/master/src/service/cache.rs): memory and disk cache · TTL cache with optional persistent entries under /data. · `src/service/cache.rs`
- [Billing](https://github.com/krey-yon/linkex/blob/master/src/billing.rs): credits · Charges or refunds based on cache and request outcomes. · `src/billing.rs`
- [Voyager parser pipeline](https://github.com/krey-yon/linkex/blob/master/src/parser/mod.rs): normalization pipeline · Reads normalized, Dash, and legacy payloads before assembly. · `src/parser/mod.rs`
- [Stable profile schema](https://github.com/krey-yon/linkex/blob/master/src/domain/profile.rs): domain contract · Separates volatile LinkedIn data from the public profile model. · `src/domain/profile.rs`

### LinkedIn Upstream

- [LinkedIn repository](https://github.com/krey-yon/linkex/blob/master/src/linkedin/repository.rs): upstream boundary · `src/linkedin/repository.rs`
- [Voyager client](https://github.com/krey-yon/linkex/blob/master/src/linkedin/client.rs): authenticated HTTP client · `src/linkedin/client.rs`
- [Auth and sessions](https://github.com/krey-yon/linkex/blob/master/src/linkedin/auth.rs): credential lifecycle · Cookie credentials or optional password login with durable sessions. · `src/linkedin/auth.rs`
- [Upstream protection](https://github.com/krey-yon/linkex/blob/master/src/linkedin/throttle.rs): throttle and circuit breaker · Spacing, bounded concurrency, retries, and circuit breaking. · `src/linkedin/throttle.rs`

### Data & Operations

- [Runtime config](https://github.com/krey-yon/linkex/blob/master/src/config.rs): configuration · `src/config.rs`
- [Redis](https://github.com/krey-yon/linkex/blob/master/docker-compose.yml): external balances store · Production backing store for billing balances. · `docker-compose.yml`
- [Telemetry](https://github.com/krey-yon/linkex/blob/master/src/telemetry.rs): structured logging · `src/telemetry.rs`

## Connections

- Executable → App assembly: starts
- App assembly → Shared state: builds
- App assembly → Routes: serves
- Runtime config → App assembly: configures
- Routes → Request middleware: applies
- Routes → Profile endpoints: registers
- Routes → Account endpoints: registers
- Routes → System endpoints: registers
- Profile endpoints → Profile orchestration: retrieves profile
- Profile endpoints → Response envelope: returns
- Request middleware → Billing: handles billable requests
- Account endpoints → Billing: reads balance and pricing
- Profile orchestration → Profile cache: checks and refreshes
- Profile orchestration → LinkedIn repository: fetches on miss
- Profile orchestration → Voyager parser pipeline: parses payload
- Voyager parser pipeline → Stable profile schema: normalizes into
- LinkedIn repository → Upstream protection: protects requests with
- Upstream protection → Voyager client: permits calls to
- Voyager client → Auth and sessions: uses session
- Billing → Redis: stores balances in
- App assembly → Telemetry: initializes
- Routes → Error mapping: maps failures through
