A small example REST API written in Go using the Gin web framework. This project provides API endpoints for managing a simple collection of music albums.
- Lightweight HTTP server using Go (Golang) Gin
- Endpoints for listing, retrieving, creating, and deleting albums
- Redis for caching
- Rate Limiting for API endpoints to prevent abuse
- Data stored in PostgreSQL
- API documentation via Swagger
Clone the repository
git clone https://github.com/joshydavid/go-gin-album.git
cd go-gin-albumRun the server
go run ./cmdBy default Gin's router.Run() will start the server on port 8080 at http://localhost:8080.
Create a binary with:
go build -o bin/go-gin-album ./cmdThen run it:
./bin/go-gin-albumhttp://localhost:8080/docs/index.html
The routes are registered in api/routes.go.
GET /api/v1/healthcheck
GET /api/v1/albums
POST /api/v1/albums
GET /api/v1/albums/:id
DELETE /api/v1/albums/:id{
"title": "Kind of Blue",
"artist": "Miles Davis",
"price": 29.99
}List all albums
curl -s http://localhost:8080/api/v1/albums | jqGet album by id
curl -s http://localhost:8080/api/v1/albums/1 | jqAdd an album
curl -X POST http://localhost:8080/api/v1/albums \
-H "Content-Type: application/json" \
-d '{"title":"Kind of Blue","artist":"Miles Davis","price":29.99}'Delete an album
curl -X DELETE http://localhost:8080/api/v1/albums/4Key files and folders:
cmd/- application entrypoint (cmd/main.go)api/routes.go- route registrationinternal/config/- database configurationsinternal/db/- database set upinternal/dto/- map model to client responseinternal/server/- server set upinternal/repository/- repository interfaces and concrete implementationsinternal/handler/- Gin handlers for each endpointinternal/service/- service layer where it contains business logicinternal/model/- domain models (e.g.,Album)internal/constant/- route constants
The album model is defined in internal/model/album.go:
type Album struct {
gorm.Model
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
ย
Developed by Joshua