# dinesheth/olx-api architecture

> Architecture diagram of the GitHub repository dinesheth/olx-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dinesheth/olx-api
- Repository: https://github.com/dinesheth/olx-api

## Overview

OLX API is a small Go HTTP service for health, welcome, and classified-listing reads. At startup it loads environment configuration, opens PostgreSQL, builds handlers and a JSON logger, then registers routes behind request-ID middleware. The listings handler queries and serializes recent rows; the other routes return static JSON. A separate migration command applies or rolls back versioned schema changes. README capabilities are represented, including migrations and structured logging; migration schema details are documented, but the SQL and individual migration command behavior are not sampled.

Main entry points: [`cmd/api/main.go`](https://github.com/dinesheth/olx-api/blob/main/cmd/api/main.go) (API server), [`internal/handlers/listings.go`](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go) (Listings endpoint), [`cmd/migrate/main.go`](https://github.com/dinesheth/olx-api/blob/main/cmd/migrate/main.go) (Migration command), [`internal/middlewares/requestId.go`](https://github.com/dinesheth/olx-api/blob/main/internal/middlewares/requestId.go) (Request IDs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_startup["Service startup"]
  node_config["Environment config<br/>[config.go]"]
  node_api["API server<br/>[main.go]"]
  node_logger["Structured logger<br/>[main.go]"]
end

subgraph group_http["HTTP API"]
  node_requestid["Request IDs<br/>[requestId.go]"]
  node_home["Home endpoint<br/>[home.go]"]
  node_health["Health endpoint<br/>[healthz.go]"]
  node_listinghandler["Listings endpoint<br/>[listings.go]"]
end

subgraph group_data["Listings data"]
  node_database[("PostgreSQL")]
  node_listingquery["Listing query<br/>[listings.go]"]
  node_listingresponse["JSON listings<br/>[listings.go]"]
  node_dbconn["DB connection pool<br/>[db.go]"]
end

subgraph group_schema["Schema management"]
  node_migratecli["Migration command<br/>[main.go]"]
  node_migrations["Schema migrations"]
  node_migrationdown["Rollback migration"]
end

node_client(("HTTP client"))

node_api -->|"loads config"| node_config
node_api -->|"opens pool"| node_dbconn
node_dbconn -->|"connects"| node_database
node_api -->|"creates logger"| node_logger
node_api -->|"registers route"| node_home
node_api -->|"registers route"| node_health
node_api -->|"registers route"| node_listinghandler
node_api -->|"wraps routes"| node_requestid
node_client -.->|"sends request"| node_api
node_requestid -->|"dispatches request"| node_listinghandler
node_listinghandler -->|"runs query"| node_listingquery
node_listingquery -->|"reads listings"| node_database
node_listinghandler -->|"encodes rows"| node_listingresponse
node_listinghandler -->|"logs outcomes"| node_logger
node_migratecli -->|"loads config"| node_config
node_migratecli -.->|"applies schema"| node_migrations
node_migratecli -.->|"rolls back"| node_migrationdown

click node_config "https://github.com/dinesheth/olx-api/blob/main/internal/config/config.go"
click node_api "https://github.com/dinesheth/olx-api/blob/main/cmd/api/main.go"
click node_requestid "https://github.com/dinesheth/olx-api/blob/main/internal/middlewares/requestId.go"
click node_home "https://github.com/dinesheth/olx-api/blob/main/internal/handlers/home.go"
click node_health "https://github.com/dinesheth/olx-api/blob/main/internal/handlers/healthz.go"
click node_listinghandler "https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go"
click node_listingquery "https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go"
click node_listingresponse "https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go"
click node_logger "https://github.com/dinesheth/olx-api/blob/main/cmd/api/main.go"
click node_dbconn "https://github.com/dinesheth/olx-api/blob/main/internal/db/db.go"
click node_migratecli "https://github.com/dinesheth/olx-api/blob/main/cmd/migrate/main.go"
click node_migrations "https://github.com/dinesheth/olx-api/blob/main/migrations/000001_create_listing_table.up.sql"
click node_migrationdown "https://github.com/dinesheth/olx-api/blob/main/migrations/000001_create_listing_table.down.sql"

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_config,node_api,node_logger,node_client toneBlue
class node_requestid,node_home,node_health,node_listinghandler toneAmber
class node_database,node_listingquery,node_listingresponse,node_dbconn toneMint
class node_migratecli,node_migrations,node_migrationdown toneRose
```

## Components

### Service startup

- [Environment config](https://github.com/dinesheth/olx-api/blob/main/internal/config/config.go): `internal/config/config.go`
- [API server](https://github.com/dinesheth/olx-api/blob/main/cmd/api/main.go): `cmd/api/main.go`
- [Structured logger](https://github.com/dinesheth/olx-api/blob/main/cmd/api/main.go): `cmd/api/main.go`

### HTTP API

- [Request IDs](https://github.com/dinesheth/olx-api/blob/main/internal/middlewares/requestId.go): `internal/middlewares/requestId.go`
- [Home endpoint](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/home.go): `internal/handlers/home.go`
- [Health endpoint](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/healthz.go): `internal/handlers/healthz.go`
- [Listings endpoint](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go): `internal/handlers/listings.go`

### Listings data

- **PostgreSQL**
- [Listing query](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go): `internal/handlers/listings.go`
- [JSON listings](https://github.com/dinesheth/olx-api/blob/main/internal/handlers/listings.go): `internal/handlers/listings.go`
- [DB connection pool](https://github.com/dinesheth/olx-api/blob/main/internal/db/db.go): `internal/db/db.go`

### Schema management

- [Migration command](https://github.com/dinesheth/olx-api/blob/main/cmd/migrate/main.go): `cmd/migrate/main.go`
- [Schema migrations](https://github.com/dinesheth/olx-api/blob/main/migrations/000001_create_listing_table.up.sql): `migrations/000001_create_listing_table.up.sql`
- [Rollback migration](https://github.com/dinesheth/olx-api/blob/main/migrations/000001_create_listing_table.down.sql): `migrations/000001_create_listing_table.down.sql`

### Other components

- **HTTP client**

## Connections

- API server → Environment config: loads config (evidence: `cmd/api/main.go`)
- API server → DB connection pool: opens pool (evidence: `cmd/api/main.go`)
- DB connection pool → PostgreSQL: connects (evidence: `internal/db/db.go`)
- API server → Structured logger: creates logger (evidence: `cmd/api/main.go`)
- API server → Home endpoint: registers route (evidence: `cmd/api/main.go`)
- API server → Health endpoint: registers route (evidence: `cmd/api/main.go`)
- API server → Listings endpoint: registers route (evidence: `cmd/api/main.go`)
- API server → Request IDs: wraps routes (evidence: `cmd/api/main.go`)
- HTTP client → API server: sends request (evidence: `README.md`)
- Request IDs → Listings endpoint: dispatches request (evidence: `internal/middlewares/requestId.go`)
- Listings endpoint → Listing query: runs query (evidence: `internal/handlers/listings.go`)
- Listing query → PostgreSQL: reads listings (evidence: `internal/handlers/listings.go`)
- Listings endpoint → JSON listings: encodes rows (evidence: `internal/handlers/listings.go`)
- Listings endpoint → Structured logger: logs outcomes (evidence: `internal/handlers/listings.go`)
- Migration command → Environment config: loads config (evidence: `cmd/migrate/main.go`)
- Migration command → Schema migrations: applies schema (evidence: `README.md`)
- Migration command → Rollback migration: rolls back (evidence: `README.md`)
