How calcium-ion/new-api works
Project Type & Purpose
• The README describes New API as a next-generation AI model gateway and asset management system. It integrates multiple AI models and third-party channels (OpenAI, Midjourney, Suno, among others), includes capabilities such as online payment, token management, caching, and support for various authentication methods.
• It is a full-stack application built with a Go backend and a modern JavaScript/React-based frontend (see the “web” directory).
Analyzing the File Structure
• The backend is primarily in Go (files like main.go, common/, controller/, service/, middleware/, model/, dto/, and constant/). These directories indicate an MVC-like separation:
• “controller” handles HTTP requests and routes.
• “service” contains the business logic.
• “model” manages data and database interaction.
• “middleware” provides cross-cutting concerns (rate limiting, authentication, logging, etc.).
• “common” and “constant” share utilities and configuration constants.
• The “relay” directory is extensive. It encapsulates adapters and handlers for multiple external channels. This is a key part of the architecture dealing with external API integrations for AI models and services.
• The “web” directory contains all frontend components (React components, pages, routing, and styling) and assets, demonstrating a clear separation between client and server.
• Configuration files (Dockerfile, docker-compose.yml, .env.example, Makefile) and CI/CD workflows (.github/workflows) indicate containerized and continuous deployment practices.
Main Components & Interactions
a. Frontend (Web):
• Built with modern JavaScript/React (using Vite as seen in vite.config.js and package.json).
• Manages UI for users including dashboards, settings, and chat interfaces.
b. Backend API Server:
• Exposes RESTful endpoints for various functionalities (controllers provide endpoints for billing, tasks, user management, channel operations, etc.).
• Routes HTTP requests via the “router” modules.
• Utilizes middleware for aspects like authorization, rate limiting, logging, etc.
c. Data Persistence Layer:
• Supports local SQLite (in Docker deployments) or remote MySQL/PostgreSQL databases.
• The “model” and “common/database.go” files indicate structured access for CRUD operations.
d. Relay/Integration Layer:
• The “relay” directory contains adapters and handlers for multiple external AI channels (e.g., OpenAI, Midjourney, Suno, Claude, Dify, and various others).
• These modules manage the data transformation and API calls between the internal system and third-party services.
e. Caching & Rate Limiting:
• Redis integration is managed in common/redis.go and through middleware. Caching mechanisms and rate limiting settings are essential for performance (and are documented in the README).
f. External Services:
• Authentication providers (e.g., Telegram, LinuxDO, OIDC) and payment channels (e.g., via the billing controllers).
• Integration with third-party API services for AI model interactions.