Skip to content

knutties/pincode-rs

Repository files navigation

pincode-rs

A small, fast Rust HTTP service for Indian pincode lookup. Response shape mirrors api.postalpincode.in so existing clients are drop-in compatible. The full post-office dataset (~165 k offices across ~19 k pincodes) is embedded into the binary at build time and held in an in-memory HashMap for O(1) lookups.

Quick start

Run the published image

docker run --rm -p 8080:8080 ghcr.io/knutties/pincode-rs:latest
curl http://127.0.0.1:8080/pincode/560076 | jq

Run from source

cargo run --release

Run with docker compose (local build)

docker compose up --build

Endpoints

GET /pincode/{code}

code must be six ASCII digits with the first digit in 18 (Indian pincode format).

curl -s http://127.0.0.1:8080/pincode/560076 | jq
[
  {
    "Message": "Number of pincode(s) found: 4",
    "Status": "Success",
    "PostOffice": [
      {
        "Name": "Bannerghatta Road",
        "Description": null,
        "BranchType": "Sub Post Office",
        "DeliveryStatus": "Delivery",
        "Circle": "Karnataka",
        "District": "Bengaluru Urban",
        "Division": "Bengaluru South",
        "Region": "Bengaluru HQ",
        "Block": "Bengaluru Urban",
        "State": "Karnataka",
        "Country": "India",
        "Pincode": "560076"
      }
    ]
  }
]

Response codes:

Status When
200 Pincode exists in the dataset. PostOffice is a non-empty array.
404 Valid-format pincode with no entries. Status: "Error", PostOffice: null.
400 Malformed input (non-digits, wrong length, first digit outside 18).

All /pincode/{code} responses set Cache-Control: public so a reverse proxy or CDN can cache them; the TTL is left to the cache layer.

GET /health

curl -s http://127.0.0.1:8080/health
{ "status": "ok", "pincodes": 19586, "post_offices": 165627 }

/health always sets Cache-Control: no-store so liveness signals reflect live state.

Configuration

Env var Default Purpose
BIND_ADDR 0.0.0.0:8080 TCP socket the server binds to.
RUST_LOG info,tower_http=info tracing-subscriber env-filter directive.

The service does no path-prefix introspection — mount it behind any reverse-proxy prefix you like.

Tests

cargo test

22 tests total: 11 library unit tests (normalization helpers, CSV loader, response DTOs, error envelope) and 11 integration tests (full request/response over the real embedded CSV via tower::ServiceExt::oneshot).

Published images

Multi-arch images (linux/amd64 + linux/arm64) are published to GitHub Container Registry:

ghcr.io/knutties/pincode-rs:latest
ghcr.io/knutties/pincode-rs:vYYYYMMDD.HHMM   # CalVer, UTC
ghcr.io/knutties/pincode-rs:sha-<short>      # commit SHA

latest always points to the most recent build from main. Each push to main also creates a matching git tag in this repo.

Design & implementation notes

About

Pincode Search

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors