How filiphasan/golang-minify-url works
Main Components
• API Entry Point and Server:
• API layer, seen in the "cmd/api" folder (main.go, Dockerfile). This is where the application starts, bootstraps configurations (using Viper from the configs folder), and sets up Gin as the HTTP router.
• Controllers & Routes:
• Located in "internal/app/controllers" and "internal/app/routes". These handlers manage HTTP requests (e.g., health check and URL shortening endpoints).
• Service Layer:
• Under "internal/app/services". These components (such as the shortener_service.go and token_service.go) encapsulate the business logic for generating shortened URLs and managing tokens.
• Caching:
• Found in "internal/app/caches", which includes an abstraction (cache.go) and a Redis implementation (redis.go). This layer improves performance by storing transient data.
• Persistence/Database:
• Managed in "internal/database" which deals with MongoDB connections and setups. This shows the project uses MongoDB for persistent storage.
• Jobs and Scheduling:
• In "internal/app/jobs", indicating the use of cron jobs (with gocron) for tasks such as token pool management and other periodic setups.
• Middleware & Logging:
• "internal/middlewares" provide capabilities for exception handling, logging of request/response, and passing correlation IDs. The logging itself is handled in "internal/logger".
• Dependency Injection:
• Google’s Wire is used (as shown in the README and presence of Wire files) to manage and inject dependencies across the various components.
• External Dependencies:
• Additional external services such as Redis and MongoDB, configured via "configs" and environment variables (e.g., .env.example) and deployed using Docker-compose (docker-compose.yml).
Relationships and Interactions
• Incoming HTTP requests first hit the API (Gin) layer via the controllers/routes.
• Requests are passed through middleware that handles logging, exception handling, and correlation.
• Controllers then call the service layer. The services interact with both:
• The persistence layer (MongoDB) for permanent data.
• The caching layer (Redis) for performance optimizations.
• Background jobs (scheduled via gocron) periodically execute tasks (like managing URL token pools) and use the same services.
• All components are configured using the configuration files from the "configs" directory.
• Dependency injection via Wire ensures that components such as the logger, database connectors, and cache clients are provided to the parts of the app that need them.
Architectural Patterns and Design Principles
• Layered Architecture: Separation between API/controllers, business logic (services), data persistence, caching, and background jobs.
• Dependency Injection: Use of Google Wire simplifies dependency management.
• Microservice-ready Modular Design: Although this project is a single service, its separation into clearly defined modules (controllers, services, repositories, middlewares) facilitates future scaling or decomposition into microservices.
• Use of external libraries (gin for routing, uber-zap for logging, viper for config management) indicates a modern Go development approach.