# subh05sus/codenearby architecture

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

- Interactive diagram: https://gitdiagram.com/subh05sus/codenearby
- Repository: https://github.com/subh05sus/codenearby

## Overview

##### Project type and purpose (API-focused view)
- CodeNearby is a full-stack Next.js application. For “API part only,” we focus on the backend/serverless‐functions layer under the `app/api` and `app/api/v1` directories.
- These directories define all REST endpoints and versioned APIs. They connect client requests to data stores, caches, authentication, and external services (GitHub, AI, Firebase, Cloudinary, Razorpay, Redis).

##### File structure for the API layer
- Top-level API directories:
- `app/api/*` – core endpoints (authentication, user/profile, posts, gatherings, chat, search, share, upload, etc.).
- `app/api/v1/*` – versioned endpoints for programmatic clients (API-key auth, billing, pricing, repos, user-tokens, user-tier, etc.).
- Support libraries under `lib/` provide connections to:
- MongoDB (`lib/mongodb.ts`)
- Redis (`lib/redis.ts`)
- Firebase (`lib/firebase.ts`)
- Cloudinary (`lib/razorpay.ts`)
- GitHub/Ai (`lib/github-search.ts`, `lib/ai.ts`)
- Authentication layer under `app/api/auth/[...nextauth]` using NextAuth.js.

##### README insights on APIs
- Features mention AI-Connect (`/api/ai-chat`), GitHub-integration endpoints, Redis caching, billing webhooks (`/api/v1/billing`).
- Auth flows: NextAuth.js (GitHub provider) at `/api/auth` and API key auth under `/api/v1/auth/api-key`.

##### Elements
- Clear labels: “API Routes”, “Auth (NextAuth)”, “DB (MongoDB)”, “Cache (Redis)”, etc.
- Blue for internal components (Next.js, Auth layer)
- Green for data stores (MongoDB, Redis)
- Orange for external services (GitHub, AI, Firebase, Cloudinary, Razorpay)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client"]:::external

    %% Next.js Server
    subgraph "Next.js Server (Vercel)" 
        direction TB
        CoreAPI["app/api"]:::internal
        V1API["app/api/v1"]:::internal
    end

    %% Authentication Services
    NextAuth["Auth (NextAuth.js)"]:::internal
    APIKeyAuth["API-Key Auth (v1)"]:::internal

    %% Data Stores
    MongoDB["MongoDB"]:::db
    Redis["Redis (Upstash)"]:::db

    %% External Services
    Firebase["Firebase (Realtime & Notifications)"]:::external
    AIService["AI Service (Google Gemini / Llama)"]:::external
    GitHubAPI["GitHub API"]:::external
    GitHubSearch["GitHub Search"]:::external
    Cloudinary["Cloudinary"]:::external
    RazorpayLib["Razorpay"]:::external

    %% Endpoints & Libraries
    AIChat["app/api/ai-chat"]:::internal
    DevelopersEP["app/api/developers"]:::internal
    UserByGH1["app/api/user-by-github/[id]"]:::internal
    UserByGH2["app/api/user-by-githubId/[id]"]:::internal

    BillingEP["app/api/v1/billing"]:::internal
    RazorpayLibCode["lib/razorpay.ts"]:::db
    PricingEP["app/api/v1/pricing/route.ts"]:::internal
    PricingUtils["lib/pricing-utils.ts"]:::db
    UserTierEP["app/api/v1/users/tier/route.ts"]:::internal
    APIKeyPermEP["app/api/v1/users/api-key-permission/route.ts"]:::internal
    BillingWebhook["app/api/v1/billing/webhook/route.ts"]:::internal
    ProfileAnalyze["app/api/v1/profile/analyze/route.ts"]:::internal

    %% Client to APIs
    Client -->|"HTTP Request"| CoreAPI
    Client -->|"HTTP Request"| V1API

    %% Auth flows
    CoreAPI -->|"NextAuth.js"| NextAuth
    V1API -->|"API-Key Auth"| APIKeyAuth

    %% CoreAPI integrations
    CoreAPI -->|"uses"| MongoDB
    CoreAPI -->|"uses"| Redis
    CoreAPI -->|"uses"| Firebase
    CoreAPI -->|"uses"| AIService
    CoreAPI -->|"calls"| GitHubAPI
    CoreAPI -->|"calls"| GitHubSearch
    CoreAPI -->|"uses"| Cloudinary

    %% V1API integrations
    V1API -->|"uses"| MongoDB
    V1API -->|"uses"| Redis
    V1API -->|"uses"| Firebase
    V1API -->|"calls"| GitHubAPI
    V1API -->|"calls"| GitHubSearch
    V1API -->|"uses"| Cloudinary
    V1API -->|"payment flows"| RazorpayLib

    %% Specific endpoint flows
    CoreAPI --> AIChat
    CoreAPI --> DevelopersEP
    CoreAPI --> UserByGH1
    CoreAPI --> UserByGH2

    V1API --> BillingEP
    V1API --> PricingEP
    V1API --> UserTierEP
    V1API --> APIKeyPermEP
    V1API --> BillingWebhook
    V1API --> ProfileAnalyze

    %% Connections to services
    AIChat --> AIService
    DevelopersEP --> GitHubAPI
    UserByGH1 --> GitHubAPI
    UserByGH2 --> GitHubSearch
    BillingEP --> RazorpayLib
    BillingWebhook --> MongoDB

    %% Click Events
    click CoreAPI "https://github.com/subh05sus/codenearby/tree/main/app/api"
    click V1API "https://github.com/subh05sus/codenearby/tree/main/app/api/v1"
    click NextAuth "https://github.com/subh05sus/codenearby/blob/main/app/api/auth/[...nextauth]/route.ts"
    click APIKeyAuth "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/auth/api-key/route.ts"
    click RazorpayLibCode "https://github.com/subh05sus/codenearby/blob/main/lib/razorpay.ts"
    click BillingEP "https://github.com/subh05sus/codenearby/tree/main/app/api/v1/billing"
    click MongoDB "https://github.com/subh05sus/codenearby/blob/main/lib/mongodb.ts"
    click Redis "https://github.com/subh05sus/codenearby/blob/main/lib/redis.ts"
    click Firebase "https://github.com/subh05sus/codenearby/blob/main/lib/firebase.ts"
    click AIService "https://github.com/subh05sus/codenearby/blob/main/lib/ai.ts"
    click AIChat "https://github.com/subh05sus/codenearby/blob/main/app/api/ai-chat/route.ts"
    click GitHubAPI "https://github.com/subh05sus/codenearby/blob/main/lib/github.ts"
    click GitHubSearch "https://github.com/subh05sus/codenearby/blob/main/lib/github-search.ts"
    click DevelopersEP "https://github.com/subh05sus/codenearby/blob/main/app/api/developers/route.ts"
    click UserByGH1 "https://github.com/subh05sus/codenearby/blob/main/app/api/user-by-github/[id]/route.ts"
    click UserByGH2 "https://github.com/subh05sus/codenearby/blob/main/app/api/user-by-githubId/[id]/route.ts"
    click PricingEP "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/pricing/route.ts"
    click PricingUtils "https://github.com/subh05sus/codenearby/blob/main/lib/pricing-utils.ts"
    click UserTierEP "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/users/tier/route.ts"
    click APIKeyPermEP "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/users/api-key-permission/route.ts"
    click BillingWebhook "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/billing/webhook/route.ts"
    click ProfileAnalyze "https://github.com/subh05sus/codenearby/blob/main/app/api/v1/profile/analyze/route.ts"

    %% Styles
    classDef internal fill:#B3D4FC,stroke:#0366d6,color:#000;
    classDef db fill:#C6F6D5,stroke:#2F855A,color:#000;
    classDef external fill:#FED7D7,stroke:#C53030,color:#000;
```
