# vietrux/habacteam-api architecture

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

- Interactive diagram: https://gitdiagram.com/vietrux/habacteam-api
- Repository: https://github.com/vietrux/habacteam-api

## Overview

##### Project type and purpose
- This is a backend‐only RESTful API built with Node.js and Express.
- It exposes HTTP endpoints (see routes/index.js and routes/api.js) and relies on Firebase for data storage/logic (config/firebase.js).
- It also requires a Facebook Page access token (HMC_ACCESS_TOKEN), so it may call the Facebook Graph API as an external service.

##### File structure
- app.js
– Bootstraps Express, applies middleware (dotenv, body-parser, CORS, etc.), mounts routes.
- bin/www
– Standard Express startup script that reads the PORT and launches the HTTP server.
- config/firebase.js
– Initializes the Firebase Admin SDK with the service account JSON.
- routes/index.js
– May serve a health‐check or homepage.
- routes/api.js
– Defines the core REST endpoints for the API.
- package.json / package-lock.json
– Lists dependencies (express, firebase-admin, dotenv, etc.) and scripts (npm run dev).
- .gitignore & README.MD
– Git exclusions and basic setup docs.

##### README for additional insights
- Setup instructions confirm it’s a dev server listening on port 5500.
- Environment variables:
- HMC_ACCESS_TOKEN: used for calls to Facebook’s API
- FIREBASE_SERVICE_ACCOUNT: JSON string to initialize Firebase Admin

##### Main components
a. External Clients (Web front‐end, Mobile apps, Chatbot UI, or any HTTP consumer)
b. Load Balancer or Reverse Proxy (optional, e.g., Nginx)
c. Node.js / Express API Server
– Entry point: bin/www → app.js
– Middleware layer: dotenv, body parsing, CORS, logging
– Routing layer: routes/index.js, routes/api.js
– Config layer: config/firebase.js (Firebase Admin)
– Service/Controller logic (inside route handlers)
d. Firebase (Cloud Firestore or Realtime Database)
e. External Facebook Graph API
f. Environment & Secrets Store (dotenv or a secrets manager)

##### Relationships and data flows
- Within API Server, flow: HTTP Server → Middleware → Router → Controller →
- Either Firebase Admin SDK → Firebase
- Or HTTP client → Facebook Graph API
- Environment variables feeding into the server startup.

##### Architectural patterns & design principles
- Layered architecture: transport layer (HTTP), middleware, routing, service logic, external integrations.
- Single responsibility: each file/directory handles a clear concern (startup, routing, config).
- Externalize configuration via dotenv.
- Use of managed services (Firebase, Facebook API) to offload state and auth.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients and Infrastructure
    EC["External Clients"]:::infra
    LB["Load Balancer / Reverse Proxy"]:::infra
    ENV["Environment & Secrets Store"]:::infra

    %% API Server Subgraph
    subgraph "API Server" 
      direction TB
      start["Startup: bin/www"]:::compute
      bootstrap["Bootstrap & Middleware: app.js"]:::compute
      middleware["Middleware Layer"]:::compute
      subgraph "Routing Layer"
        direction TB
        health["Health‐check/Homepage Route: index.js"]:::compute
        core["Core REST Endpoints: api.js"]:::compute
      end
      subgraph "Config Layer"
        direction TB
        firebase_cfg["Firebase Admin SDK Config: firebase.js"]:::config
        pkg["Environment & Dependency Management: package.json"]:::config
        gitignore["Git Exclusions: .gitignore"]:::config
        readme["Setup Docs: README.MD"]:::config
      end
    end

    %% External Services
    FB["Facebook Graph API"]:::externalAPI
    FirebaseDB["Firebase Database"]:::managed

    %% Connections
    EC -->|HTTP Requests| LB
    LB --> start
    ENV --> bootstrap
    start --> bootstrap
    bootstrap --> middleware
    middleware --> health
    middleware --> core
    core -->|Uses SDK| firebase_cfg
    core -->|Reads Dependencies| pkg
    core -->|Calls| FB
    core -->|Queries| FirebaseDB

    %% Backflow
    FB -->|HTTP Response| core
    FirebaseDB -->|Data Response| core
    core -->|HTTP Response| EC

    %% Click Events
    click start "https://github.com/vietrux/habacteam-api/tree/main/bin/www"
    click bootstrap "https://github.com/vietrux/habacteam-api/blob/main/app.js"
    click health "https://github.com/vietrux/habacteam-api/blob/main/routes/index.js"
    click core "https://github.com/vietrux/habacteam-api/blob/main/routes/api.js"
    click firebase_cfg "https://github.com/vietrux/habacteam-api/blob/main/config/firebase.js"
    click pkg "https://github.com/vietrux/habacteam-api/blob/main/package.json"
    click gitignore "https://github.com/vietrux/habacteam-api/blob/main/.gitignore"
    click readme "https://github.com/vietrux/habacteam-api/blob/main/README.MD"

    %% Styles
    classDef compute fill:#D0E6FF,stroke:#0366d6;
    classDef managed fill:#DFFFE0,stroke:#31a823;
    classDef externalAPI fill:#FFE3B3,stroke:#FF8C00;
    classDef infra fill:#E0E0E0,stroke:#4d4d4d;
    classDef config fill:#F0F0F0,stroke:#888888;
```
