# wangwangit/substracker architecture

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

- Interactive diagram: https://gitdiagram.com/wangwangit/substracker
- Repository: https://github.com/wangwangit/substracker

## Overview

##### Project Type and Purpose
- This is a serverless full-stack subscription management and reminder system (“SubsTracker”) built entirely on Cloudflare Workers.
- It exposes both a web UI (single‐page app served from the Worker) and a REST-style API for managing subscriptions, and runs a scheduled job (Cron Trigger) to send notifications via Telegram, Webhook, email, etc.

##### File Structure Analysis
- index.js – the sole Worker entry point. Contains routing logic (UI vs API vs Cron) and service layer.
- package.json – dependencies (e.g. Router, Resend SDK).
- wrangler.toml – Cloudflare Workers config (KV binding, Cron schedule).
- README.md – detailed feature list, deployment instructions, and notification channel configs.

##### Key Architecture Insights from the README
- Uses a single Cloudflare Worker for all functionality (UI + API + scheduler).
- Persists subscription data in a Workers KV namespace (`SUBSCRIPTIONS_KV`).
- Cron trigger fires daily (or hourly) to run the “check-expiry-and-notify” job.
- Integrates with multiple external notification services (Telegram Bot API, NotifyX, WeChat, Webhook templates, Bark, Resend email).

##### Main Components & Their Interactions
a. Client (Browser)
– Requests the UI (static HTML/CSS/JS) and calls the JS app’s REST APIs under `/api/*`.
b. Cloudflare Worker
– Route Handler: Serves static assets and routes API calls.
– API Layer: CRUD operations on subscriptions, auth (admin user), config endpoints.
– Scheduler (Cron Trigger): Periodically invokes notification job.
– Notification Service Layer: Picks configured channels, formats payloads, calls external endpoints.
c. Workers KV (`SUBSCRIPTIONS_KV`)
– Key-value store for subscription records, user settings, and notification history.
d. External Services
– Telegram Bot API, NotifyX API, WeChat robot webhook, generic Webhook endpoint, Bark push API, Resend email API.

##### Architectural Patterns & Principles
- Serverless / Functions-as-a-Service: single deployable unit for routing, data, and scheduled jobs.
- Layered design within the Worker:
1. HTTP Routing → 2. Business Logic / Subscription Service → 3. Persistence (KV) & Notification Adapters.
- Event-driven Cron scheduling for asynchronous notification tasks.
- Modular adapters for each notification channel (plug-and-play).

##### Detailed Capture of Essential Elements
- Technology stacks: JavaScript/TypeScript runtime, Cloudflare Workers, Workers KV, Wrangler CLI.
- Cron schedule (defined in wrangler.toml) as part of the deployment config.
- Modular adapters for each channel so it’s clear where new channels would plug in.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Browser["Client Browser"]:::client
    Browser -->|"GET / → UI assets"| Router

    %% Cloudflare Worker Layers
    subgraph "Cloudflare Worker"
        direction TB
        Router["HTTP Router"]:::worker
        Service["Subscription Service"]:::worker
        Dispatcher["Notification Dispatcher"]:::worker
    end

    %% Interaction between Router and Service
    Router -->|"POST /api/subscriptions → CRUD calls"| Service
    Router -->|"PUT/GET /api/config → update settings"| Service

    %% Cron Trigger
    CronJob["Cron Trigger"]:::cron
    CronJob -->|"Cron: checkExpiry → notifyChannels"| Router

    %% KV Store
    KV["SUBSCRIPTIONS_KV"]:::kv
    Service -->|"read/write subscription data"| KV
    KV -->|"return data"| Service

    %% Notification Providers
    subgraph "Notification Providers"
        direction TB
        Telegram["Telegram Bot API"]:::external
        NotifyX["NotifyX API"]:::external
        WeChat["WeChat Robot Webhook"]:::external
        Webhook["Generic Webhook"]:::external
        Bark["Bark Push API"]:::external
        Resend["Resend Email API"]:::external
    end

    Dispatcher -->|"send notification"| Telegram
    Dispatcher -->|"send notification"| NotifyX
    Dispatcher -->|"send notification"| WeChat
    Dispatcher -->|"send notification"| Webhook
    Dispatcher -->|"send notification"| Bark
    Dispatcher -->|"send notification"| Resend

    %% Project Files
    subgraph "Project Files"
        direction TB
        Pkg["package.json"]:::docs
        Config["wrangler.toml"]:::docs
        Readme["README.md"]:::docs
        License["LICENSE"]:::docs
    end

    %% Click Events
    click Router "https://github.com/wangwangit/substracker/blob/master/index.js"
    click Service "https://github.com/wangwangit/substracker/blob/master/index.js"
    click Dispatcher "https://github.com/wangwangit/substracker/blob/master/index.js"
    click Pkg "https://github.com/wangwangit/substracker/blob/master/package.json"
    click Config "https://github.com/wangwangit/substracker/blob/master/wrangler.toml"
    click Readme "https://github.com/wangwangit/substracker/blob/master/README.md"
    click License "https://github.com/wangwangit/substracker/tree/master/LICENSE"

    %% Styles
    classDef client fill:#fff3cd,stroke:#f1c40f,color:#856404;
    classDef worker fill:#d6eaf8,stroke:#3498db,color:#1b4f72;
    classDef kv fill:#d5f5e3,stroke:#27ae60,color:#145a32,stroke-dasharray: 5 5;
    classDef cron fill:#fdebd0,stroke:#e67e22,color:#a04000;
    classDef external fill:#ececec,stroke:#7f8c8d,color:#2c3e50;
    classDef docs fill:#f0f0f0,stroke:#bdc3c7,color:#2c3e50,stroke-dasharray: 2 2;
```
