How sk-shahnawaz/go-url-shortener works
Main Components
• API Server: Represented by the main entry point (src/main.go) where the Echo web server listens on a configurable port (via CUSTOM_PORT_NUMBER). This is the central component that handles the incoming HTTP requests.
• Handlers Layer: The components in the src/Handlers directory that define the endpoints (/api/generate and /api/resolve). They process requests, interface with underlying logic, and return responses.
• Models & DTOs: Under src/Models, these files define the data structures used throughout the application. They represent the core data used by handlers and passed to the database.
• Database Module: Located in src/Database. This component handles:
• The connection to PostgreSQL (with configurations from the .env file).
• Database migrations (in src/Database/Migrations) using scripts generated/run via Go.
• A dedicated Dockerfile to containerize the database component.
• Documentation & Utilities: The src/docs folder contains Swagger API definitions (swagger.json and swagger.yaml) that serve documentation at /swagger/index.html. Additionally, src/Utilities supports miscellaneous functions.
• Deployment & CI/CD: Dockerfile and docker-compose.yml are used to containerize and orchestrate the application. Also, GitHub workflows, including build and labeler configs, support the continuous integration process.
Component Relationships and Interactions
• External Client/Browser:
• Interacts with the API server through HTTP requests. This includes:
• Health Check Endpoint – verifying that the server is running.
• API calls to /api/generate and /api/resolve.
• Accessing the Swagger UI for API documentation.
• API Server:
• Routes requests to the appropriate Handlers.
• Invokes business logic that works with the Models/DTOs.
• Delegates persistence tasks to the Database Module.
• Database Module (PostgreSQL):
• Receives queries or migration commands from the API server.
• Stores and retrieves data as per the configuration (either in-memory or using PostgreSQL).
• CI/CD Pipeline:
• GitHub Actions (configured in .github/workflows) automate build/test/deployment processes.
• Docker Compose orchestrates the entire application stack (API server and Database) during deployment/testing.
Architectural Patterns & Design Principles
• Modularity: Each group of files (Handlers, Models, Database, Utilities) is clearly separated to adhere to single responsibility principles.
• Environment-Driven Configuration: Use of a .env file and different settings for choosing between in-memory or PostgreSQL databases.
• Containerization: Docker and Docker Compose are used to encapsulate application components, making deployment more reproducible.
• Documentation: Built-in Swagger documentation (auto-generated via comments) helps both developers and external users understand the API.
• GitHub Actions: Continuous integration practices ensure that the application remains robust and maintainable.
Execution
• Configuration aspects: use of .env for environment variables, and the Docker ecosystem for running containers.
• Interactions between components and any conditional behavior (such as switching between an in-memory database vs. PostgreSQL).