How pilinux/gorest works
Main Components
• Environment & Configuration: Represent the environment variables (.env) and configuration files (in the config directory) as the groundwork for the application’s runtime settings. These include server and email settings, logging configurations, security parameters, and database configurations.
• Routing Layer: Identify the controller, handler, and router folders (e.g., controller/, handler/, example/router/) where incoming HTTP requests are mapped to the appropriate business logic.
• Business Logic / Service Layer: Outline the service directory (service/) which encapsulates business logic such as authentication, postmark email handling, and security operations.
• Database Layer:
• Database connections and migration scripts exist in the database/ folder (and its subfolders such as migrate/ and model/).
• The project supports multiple databases (MySQL, PostgreSQL, SQLite3, Redis, MongoDB) and uses GORM; represent how configuration and model definitions are used to interact with the database.
• Library & Middleware: Highlight the lib/ directory that includes generic utility functions (file handling, encryption, hashing, HTML email rendering) and middleware components (CORS, firewall, JWT processing, twoFA).
• External Integrations: Components like logging (output to logs/ and Sentry integration through lib/middleware/sentry.go) and external configuration (environment variables, email and JWT configuration) should be shown as side services.
• Example Implementation: The example/ folder provides a working instance or blueprint that illustrates how to wire up various components of the system.
Relationships and Data Flow
• Requests are directed to Controllers which call the corresponding Handlers. Both controllers and handlers are responsible for receiving request data and passing it to business services as needed.
• Middleware components (from lib/middleware/) wrap around routing and handler logic to enforce CORS, authentication, rate limiting, and request validation.
• The Service layer then processes the logic (e.g., authentication, email verification, two-factor authentication) and interacts with the Database layer via GORM.
• Configuration settings (from config/ and .env) enable and fine-tune interactions (e.g., database connections, logging, security settings).
• Utility libraries (lib/) are used across the application to support encryption, file operations, hashing, etc.
• Error handling, logging, and notifications are piped through appropriate channels (logs directory, Sentry integration).
Architectural Patterns & Design Principles
• Separation of concerns: Different folders (config, controller, database, handler, service, lib) indicate a layered design.
• MVC-like structure: While not pure MVC, the project separates request controllers, business logic (services), and data models.
• Modularity: Components are split into independent domains (middleware, encryption, file handling) that are easily testable.
• Extensibility: With directories and example code in place, the design supports rapid prototyping and customization for various project needs.