Skip to content

joshydavid/go-gin-album

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿน Go (Golang) Gin Album API

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.

๐Ÿš€ Features

  • 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

โš™๏ธ Getting Started

Clone the repository

git clone https://github.com/joshydavid/go-gin-album.git
cd go-gin-album

Run the server

go run ./cmd

By default Gin's router.Run() will start the server on port 8080 at http://localhost:8080.

๐Ÿšง Build

Create a binary with:

go build -o bin/go-gin-album ./cmd

Then run it:

./bin/go-gin-album

๐Ÿ“ API documentation

http://localhost:8080/docs/index.html

๐Ÿ’ฌ API Endpoints

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

โœ๏ธ Example Album JSON

{
 "title": "Kind of Blue",
 "artist": "Miles Davis",
 "price": 29.99
}

โœ๏ธ cURL examples

List all albums

curl -s http://localhost:8080/api/v1/albums | jq

Get album by id

curl -s http://localhost:8080/api/v1/albums/1 | jq

Add 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/4

๐Ÿ“ Project Structure

Key files and folders:

  • cmd/ - application entrypoint (cmd/main.go)
  • api/routes.go - route registration
  • internal/config/ - database configurations
  • internal/db/ - database set up
  • internal/dto/ - map model to client response
  • internal/server/ - server set up
  • internal/repository/ - repository interfaces and concrete implementations
  • internal/handler/ - Gin handlers for each endpoint
  • internal/service/ - service layer where it contains business logic
  • internal/model/ - domain models (e.g., Album)
  • internal/constant/ - route constants

๐Ÿ’ฝ Data Model

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"`
}

๐Ÿš€ Acknowledgement

ย 

Developed by Joshua

About

๐Ÿน Go (Golang) Music Album API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages