# rfym21/qwen2api architecture

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

- Interactive diagram: https://gitdiagram.com/rfym21/qwen2api
- Repository: https://github.com/rfym21/qwen2api

## Overview

##### Project type and purpose
- Qwen2API is a full-stack application consisting of:
- A Vue.js single-page frontend (under `public/`)
- A Node.js/Express backend API (under `src/`)
- Its purpose is to expose a chat-model interface and image upload pipeline over HTTP, with optional Redis/file persistence and Docker deployment.

##### File structure
- Top-level:
- `Dockerfile`, `docker-compose.yml` (and `docker-compose-redis.yml`) for containerized deployment
- `.env.example` for runtime configuration
- `data/` for local JSON persistence template
- Frontend: under `public/`
- `public/src/` holds Vue components, routes, assets
- `public/index.html`, `vite.config.js`, `tailwind.config.js`
- Backend: under `src/`
- `src/server.js` is the Express entry point
- `src/router/` defines REST endpoints (`accounts.js`, `chat.js`, `images.js`, …)
- `src/lib/` contains service layer modules (account management, Redis client, image upload, request wrappers)
- `src/config.js` centralizes configuration values

##### README for additional insights
- Environment variables drive API prefix, port, API key, Redis URL, account list, data save mode
- Key endpoints:
- `GET /v1/models`
- `POST /v1/chat/completions`
- `POST /v1/images/generations` (legacy)
- Supports three persistence modes: none, local file, Redis

a. Main components
- Client Browser (users)
- Frontend SPA (Vue.js)
- Backend HTTP Server (Express)
- Internal Service Layer (modules under `src/lib`)
- Data Layer:
- File System (`data/` folder)
- Redis (optional, via `REDIS_URL`)
- External AI Model Service (Qwen API or Hugging Face inference)
b. Relationships & interactions
1. Browser ↔️ Frontend SPA (HTTP/WS)
2. SPA → Backend API (HTTP calls to `/v1/*`)
3. Backend Router → Service Layer modules (account.js, request.js, upload.js…)
4. Service Layer → External Model Service (chat completions, image generation)
5. Service Layer → Data Layer (persist messages/images to file or Redis)
6. Optional: Backend ↔️ Redis for caching tokens, conversation state
c. Architectural patterns & design principles
- Separation of concerns: routing, business logic (lib), configuration
- Pluggable persistence: file vs. Redis determined by `DATA_SAVE_MODE`
- Single entry point (`server.js`) routing to modular routers
- Stateless request handling with optional state in Redis
d. Key technologies & frameworks
- Vue 3 + Vite + Tailwind CSS for frontend
- Node.js + Express for backend
- Upstash Redis (TLS) or local Redis instance
- Docker & Docker Compose for deployment
- dotenv/config for environment management

##### Detail without over-splitting
- Router and service module as a component only if it has distinct responsibilities
- How environment variables (API_KEY, REDIS_URL, DATA_SAVE_MODE) toggle behavior in the backend

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Browser["Client Browser"]:::external

    %% Frontend Layer
    subgraph "Frontend Layer"
        PF_public["public/"]:::frontend
        PF_src["public/src/"]:::frontend
        Appvue["App.vue"]:::frontend
        Mainjs["main.js"]:::frontend
        Routes["routes/index.js"]:::frontend
        Views["public/src/views/"]:::frontend
        IndexHTML["index.html"]:::frontend
        ViteConfig["vite.config.js"]:::frontend
        TailwindConfig["tailwind.config.js"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        Server["server.js"]:::backend

        subgraph "API Routers"
            RouterIndex["index.js"]:::backend
            RouterAccounts["accounts.js"]:::backend
            RouterChat["chat.js"]:::backend
            RouterImages["images.js"]:::backend
            RouterModels["models.js"]:::backend
            RouterSettings["settings.js"]:::backend
            RouterUpload["upload.js"]:::backend
            RouterVerify["verify.js"]:::backend
        end

        subgraph "Service Layer"
            LibAccount["account.js"]:::backend
            LibImage["image.js"]:::backend
            LibImgCaches["imgCaches.js"]:::backend
            LibRedis["redis.js"]:::backend
            LibRequest["request.js"]:::backend
            LibSetting["setting.js"]:::backend
            LibTools["tools.js"]:::backend
            LibUpload["upload.js"]:::backend
        end

        subgraph "Configuration"
            ConfigJS["config.js"]:::backend
            EnvExample[".env.example"]:::backend
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        FileSystem["data/ (data_template.json)"]:::data
        RedisStore["Redis"]:::data
    end

    %% External Services
    AIService["Qwen/Hugging Face AI Model Service"]:::external

    %% Deployment
    subgraph "Deployment"
        Dockerfile["Dockerfile"]:::docker
        ComposeApp["docker-compose.yml"]:::docker
        ComposeRedis["docker-compose-redis.yml"]:::docker
    end

    %% Connections
    Browser -->|"loads SPA"| PF_public
    PF_public --> PF_src
    PF_public --> IndexHTML
    PF_public --> ViteConfig
    PF_public --> TailwindConfig
    PF_src --> Appvue
    PF_src --> Mainjs
    PF_src --> Routes
    PF_src --> Views

    PF_src -->|"HTTP calls"| Server

    Server --> RouterIndex
    Server --> RouterAccounts
    Server --> RouterChat
    Server --> RouterImages
    Server --> RouterModels
    Server --> RouterSettings
    Server --> RouterUpload
    Server --> RouterVerify

    RouterIndex --> LibAccount
    RouterAccounts --> LibAccount
    RouterChat --> LibRequest
    RouterChat --> LibUpload
    RouterImages --> LibImage
    RouterUpload --> LibUpload
    RouterVerify --> LibTools
    RouterSettings --> LibSetting
    RouterModels --> LibRequest

    LibRequest --> AIService
    LibUpload --> AIService
    LibImage --> FileSystem
    LibImage --> RedisStore
    LibImgCaches --> RedisStore
    LibRedis --> RedisStore

    Server --> ConfigJS
    Server --> EnvExample

    Server --> Dockerfile
    Server --> ComposeApp
    Server --> ComposeRedis

    %% Click Events
    click PF_public "https://github.com/rfym21/qwen2api/tree/main/public/"
    click PF_src "https://github.com/rfym21/qwen2api/tree/main/public/src/"
    click Appvue "https://github.com/rfym21/qwen2api/blob/main/public/src/App.vue"
    click Mainjs "https://github.com/rfym21/qwen2api/blob/main/public/src/main.js"
    click Routes "https://github.com/rfym21/qwen2api/blob/main/public/src/routes/index.js"
    click Views "https://github.com/rfym21/qwen2api/tree/main/public/src/views/"
    click IndexHTML "https://github.com/rfym21/qwen2api/blob/main/public/index.html"
    click ViteConfig "https://github.com/rfym21/qwen2api/blob/main/public/vite.config.js"
    click TailwindConfig "https://github.com/rfym21/qwen2api/blob/main/public/tailwind.config.js"

    click Server "https://github.com/rfym21/qwen2api/blob/main/src/server.js"

    click RouterIndex "https://github.com/rfym21/qwen2api/blob/main/src/router/index.js"
    click RouterAccounts "https://github.com/rfym21/qwen2api/blob/main/src/router/accounts.js"
    click RouterChat "https://github.com/rfym21/qwen2api/blob/main/src/router/chat.js"
    click RouterImages "https://github.com/rfym21/qwen2api/blob/main/src/router/images.js"
    click RouterModels "https://github.com/rfym21/qwen2api/blob/main/src/router/models.js"
    click RouterSettings "https://github.com/rfym21/qwen2api/blob/main/src/router/settings.js"
    click RouterUpload "https://github.com/rfym21/qwen2api/blob/main/src/router/upload.js"
    click RouterVerify "https://github.com/rfym21/qwen2api/blob/main/src/router/verify.js"

    click LibAccount "https://github.com/rfym21/qwen2api/blob/main/src/lib/account.js"
    click LibImage "https://github.com/rfym21/qwen2api/blob/main/src/lib/image.js"
    click LibImgCaches "https://github.com/rfym21/qwen2api/blob/main/src/lib/imgCaches.js"
    click LibRedis "https://github.com/rfym21/qwen2api/blob/main/src/lib/redis.js"
    click LibRequest "https://github.com/rfym21/qwen2api/blob/main/src/lib/request.js"
    click LibSetting "https://github.com/rfym21/qwen2api/blob/main/src/lib/setting.js"
    click LibTools "https://github.com/rfym21/qwen2api/blob/main/src/lib/tools.js"
    click LibUpload "https://github.com/rfym21/qwen2api/blob/main/src/lib/upload.js"

    click ConfigJS "https://github.com/rfym21/qwen2api/blob/main/src/config.js"
    click EnvExample "https://github.com/rfym21/qwen2api/blob/main/.env.example"

    click FileSystem "https://github.com/rfym21/qwen2api/tree/main/data/"
    click RedisStore "https://github.com/rfym21/qwen2api/blob/main/src/lib/redis.js"

    click Dockerfile "https://github.com/rfym21/qwen2api/tree/main/Dockerfile"
    click ComposeApp "https://github.com/rfym21/qwen2api/blob/main/docker-compose.yml"
    click ComposeRedis "https://github.com/rfym21/qwen2api/blob/main/docker-compose-redis.yml"

    %% Styles
    classDef frontend fill:#D0E8F2,stroke:#000,stroke-width:1px
    classDef backend fill:#D5E8D4,stroke:#000,stroke-width:1px
    classDef data fill:#FFE6CC,stroke:#000,stroke-width:1px
    classDef external fill:#F8CECC,stroke:#000,stroke-width:1px
    classDef docker fill:#E1DFE1,stroke:#000,stroke-width:1px
```
