How eryajf/xirang works
Project type and purpose
• This is a Go-based backend web framework and reference implementation (“xirang”) built on Gin (HTTP server) and Gorm (ORM).
• It provides a template for building RESTful APIs with ready-made layers (config, middleware, routers, controllers, business logic, services, models) and common utilities.
File structure
• Top-level files:
• main.go (entry point)
• config.yml (runtime configuration)
• Dockerfile, Makefile (build & deployment)
• Directories:
• config: loads & parses config.yml
• public: shared utilities (logging, DB init, validators, crypto, HTTP tools)
• middleware: Gin middleware (auth, CORS, rate limiting, Casbin, operation logging)
• routes: maps HTTP paths to controllers
• controller: HTTP handlers that decode requests, call logic, return responses
• logic: orchestrates business rules, validation, calls services
• service: provides fine-grained operations (e.g., CRUD, transactions)
• model: data definitions (Gorm models, request/response DTOs)
• test: unit/integration tests
README for additional insights
• Emphasizes simplicity: change config.yml, spin up Gin+Gorm
• No external microservices; supports MySQL or SQLite
• Ready-made RBAC (Casbin), logging, operation log, request rate limiting
Main components & their relationships
a. Client (Frontend or API consumer)
b. HTTP Server Layer (Gin Engine)
• Loads config (config package)
• Initializes global components (DB connection via Gorm, logger, Casbin enforcer, rate limiter)
c. Middleware Chain
• Global middleware applied to Gin: CORS, Auth, Casbin, RateLimit, OperationLog
d. Router Layer
• Binds URL paths to controller methods
e. Controller Layer
• Parses HTTP requests into DTOs (model/*/request)
• Invokes corresponding Logic
• Returns responses (model/*/response)
f. Logic Layer
• Implements business workflows
• Calls Service layer for data persistence or external calls
g. Service Layer
• Encapsulates data access, transaction control, calls to Gorm models
h. Model Layer
• Gorm model structs, ORM mapping to DB tables
i. Database
• MySQL or SQLite (configured in config.yml)
Architectural patterns & design principles
• Layered architecture (Controller → Logic → Service → Model → DB)
• Separation of concerns: routing, request handling, business logic, persistence
• Use of Gin middleware for cross-cutting concerns (auth, logging, rate limiting, RBAC)
• DTOs for request/response to decouple API schema from ORM models
• Config-driven initialization for pluggable components (DB, logger, Casbin)
• Testability: clear boundaries allow unit/integration tests in test/
Essential elements
• Clear labels for each component (e.g., “AuthMiddleware”, “system_user_controller”)
• Database symbol for MySQL/SQLite