No description
Find a file
2025-02-14 12:42:01 +10:00
.gitignore Add spotify auth handling and route testing, begin DB work 2025-02-12 03:25:08 +10:00
auth.go Cleanup API responses and add better comments 2025-02-14 05:54:45 +10:00
auth_test.go Run go fmt 2025-02-12 14:31:19 +10:00
database.go Run go fmt, fix pagination bug, clean up code 2025-02-14 12:24:31 +10:00
Dockerfile Add dockerfile, update ping route to alive route, add documentation 2025-02-12 11:55:56 +10:00
go.mod Add dockerfile, update ping route to alive route, add documentation 2025-02-12 11:55:56 +10:00
go.sum Add spotify auth handling and route testing, begin DB work 2025-02-12 03:25:08 +10:00
main.go Run go fmt, fix pagination bug, clean up code 2025-02-14 12:24:31 +10:00
README.md Increase readability of readme file 2025-02-14 12:27:42 +10:00
routes.go Clean up comments and defer response body closing 2025-02-14 12:42:01 +10:00
routes_test.go Run go fmt, fix pagination bug, clean up code 2025-02-14 12:24:31 +10:00
utils.go Clean up comments and defer response body closing 2025-02-14 12:42:01 +10:00

Basic spotify API in golang

This API supports simple artist read operations for spotify data, as well as using a simple caching database and handling client authentication automatically.

Setup

After compiling the binary, or when executing via go run, just be sure to set the following environment variables to enable access to Spotify's API.

SPOTIFY_ID=<your spotify client ID>
SPOTIFY_TOKEN=<your spotify token>

Example execution using the linux command line: SPOTIFY_ID=myspotifyID SPOTIFY_TOKEN=myspotifytoken ./gomusic

Alternatively you can use go run too: SPOTIFY_ID=myspotifyID SPOTIFY_TOKEN=myspotifytoken go run .

This project has been built with docker support, and a docker image can be built using the Dockerfile provided. Please note that the above environment variables will still need to be passed into the container for execution to work correctly.

Testing

This API comes with a complete test suite, it can be run using the standard go test system. When running tests you are required to provide the same credentials as when running the server normally.

For example: SPOTIFY_ID=myspotifyID SPOTIFY_TOKEN=myspotifytoken go test .

Pagination

The API supports pagination on all endpoints that require it, and by default the first page of 10 elements is returned. If you would like to customize the pagination, simply add the page= and page_size= paramaters to your query.

For example, using curl: curl -v 'http://127.0.0.1:8000/artists?name=a&page=2&page_size=100'

This would return the second page of 100 elements from the artist name search endpoint. Currently the genre listing endpoint also supports these parameters.

Adding new genres

The API endpoint to add a new genre accepts a POST request on the /genres route, and will parse several types of application input, preferring JSON and XML first but falling back to post-form parsing if the client has not specified an appropriate content type for their request.

To give a concrete example using JSON, the following curl request adds a new genre to the database:

curl -v http://127.0.0.1:8000/genres -X POST --data '{"name": "myfancysound"}' -H "content-type: application/json"

Note that since the content type is being checked by the API for parsing, you are required to specify a correct one, otherwise you must send post-form data for the API to find your values correctly.

Technical Choices

This project is built using the Gin framework, for easy and consistent request contexts, as well as easy to expand middleware support. For database interactions it uses the gorm library, to allow for advanced SQL features and server agnostic support.

This API currently uses sqlite as the database for simplicity, but switching to postgres or mysql is easy without any loss of functionality or change in database calls.