This project is a template for building web services using the Gin framework in Go. It provides a structured and scalable foundation for developing robust APIs.
- Modular Structure: Organized codebase for easy maintenance and scalability.
- RESTful API: Built-in support for creating RESTful endpoints.
- Database Integration: Pre-configured database layer for efficient data management.
- Caching: Integrated caching mechanism for improved performance.
- Error Handling: Standardized error handling and reporting.
- Middleware Support: Easy integration of custom middleware.
- Logging: Built-in logging for better debugging and monitoring.
- Graceful Shutdown: Ensures proper closure of resources and connections.
- Docker Support: Containerization ready for easy deployment.
- Tracing and Metrics: Integrated for better observability.
The application is configured using a YAML file located at config/config.yaml
. Here's how to set it up:
-
Copy the
config.yaml.example
toconfig.yaml
. -
Edit the
config.yaml
file to set your specific configuration:server: port: 8080 timeout: 10s
database: host: localhost port: 5432 user: youruser password: yourpassword dbname: yourdbname
cache: address: localhost:6379 password: "" db: 0
log: level: info format: json
Adjust the values according to your environment and requirements.
- Album Management: CRUD operations for managing albums.
- Error Handling: Centralized error handling with custom error types.
- Caching: Request caching to improve response times.
- Database Integration: Configured database layer with connection pooling.
- Middleware: Custom middleware for various purposes like error handling.
- Logging: Structured logging for better traceability.
- Graceful Shutdown: Proper shutdown procedure to ensure all resources are released.
- Docker Support: Dockerized application for easy deployment and scaling.
- Tracing and Metrics: Integrated tracing and metrics for monitoring and performance analysis.
- Clone the repository
- Configure the
config.yaml
file - Run
go mod tidy
to install dependencies - Run
go run main.go
to start the server
To start the server, follow these steps:
-
Ensure you have Go installed on your system.
-
Open a terminal and navigate to the project root directory.
-
Run the following command:
go run main.go
-
You should see output similar to this:
2023/06/10 15:30:45 Starting server on :8080
-
The server is now running and listening on port 8080 (or the port specified in your
config.yaml
).
You can now send requests to http://localhost:8080
to interact with the API.
To stop the server, press Ctrl+C
in the terminal. The application will perform a graceful shutdown, ensuring all resources are properly released.
For more detailed information on each component, please refer to the respective files in the project structure.
├── app // Our application and all dependent code
│ ├── albums // Our Albums domain, including all APIs, services, and models
│ │ ├── controller.go // API controller for the Album domain
│ │ ├── service.go // service layer for all business logic
│ │ ├── repository.go // repository layer for all data access to an album
│ │ ├── models.go // Models for presenting an Album
│ │ └── init.go // the bootstrapping of the entire api, including routes, and versioning
│ ├── apiErrors
│ │ └── error.go // API Error creation and model definition
│ ├── cache
│ │ └── cache.go // request caching layer, model and initialization
│ ├── db // database layer module
│ │ ├── db.go // database connection and initialization
│ │ ├── error.go // error mapping from db specific to application error
│ │ └── models.go // shared models from the database E.G. pagination models
│ ├── middleware // middleware used for the application
│ │ └── errorHandler.go // error handling code to return standardized error models
├── config
│ └── config.yaml // yaml file for all configuration
├── seed // seed data for the application locally
│ ├── albums.go // Albums seed data
│ └── seed.go // main seed script for all models
└── main.go
└── go.sum // Go module checksum file
- Add swagger
- add versioning
- Add tests
- logger
- graceful shutdown
- create a docker image
- add tracing and metrics
- CI/CD