Add dockerfile, update ping route to alive route, add documentation

This commit is contained in:
froge 2025-02-12 11:55:56 +10:00
parent 0d334bde38
commit a9e265a4f3
Signed by: froge
GPG key ID: A825E09930271BFA
6 changed files with 57 additions and 13 deletions

21
Dockerfile Normal file
View file

@ -0,0 +1,21 @@
# syntax=docker/dockerfile:1
FROM docker.io/golang:1.23
# Create working directory and grab deps
WORKDIR /usr/src/gomusic
COPY go.mod go.sum ./
RUN go mod download
# Copy the source code. Note the slash at the end, as explained in
# https://docs.docker.com/engine/reference/builder/#copy
COPY *.go ./
# Build
# Note that our dependencies require CGO
RUN CGO_ENABLED=1 GOOS=linux go build -v -o /usr/local/bin/gomusic
# Signal which ports we use
EXPOSE 8000
# Run
CMD ["gomusic"]

View file

@ -1,2 +1,28 @@
# 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 .`
## Testing
This application 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 .`
## 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.

4
go.mod
View file

@ -5,6 +5,8 @@ go 1.23.5
require (
github.com/gin-gonic/gin v1.10.0
github.com/stretchr/testify v1.10.0
gorm.io/driver/sqlite v1.5.7
gorm.io/gorm v1.25.12
)
require (
@ -38,6 +40,4 @@ require (
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.5.7 // indirect
gorm.io/gorm v1.25.12 // indirect
)

View file

@ -19,11 +19,8 @@ var spotifyClientSecret = os.Getenv("SPOTIFY_SECRET")
func setupRouter(env *Env, spotifyID string, spotifySecret string) *gin.Engine {
var r *gin.Engine = gin.Default()
// Add our spotify auth middleware
r.Use(spotifyAuth(spotifyID, spotifySecret))
r.GET("/ping", env.ping)
r.GET("/alive", env.alive)
r.GET("/artists/:artistID", env.getArtistByID)
return r
}
@ -42,5 +39,5 @@ func main() {
// Router/middleware and server setup
r := setupRouter(env, spotifyClientID, spotifyClientSecret)
r.Run(":8080")
r.Run(":8000")
}

View file

@ -7,8 +7,8 @@ import (
"log/slog"
)
func (env *Env) ping(c *gin.Context) {
c.String(http.StatusOK, "pong")
func (env *Env) alive(c *gin.Context) {
c.String(http.StatusOK, "yes!")
}
func (env *Env) getArtistByID(c *gin.Context) {

View file

@ -9,17 +9,17 @@ import (
"fmt"
)
func TestPingRoute(t *testing.T) {
db := setupTestDatabase("testping")
func TestAliveRoute(t *testing.T) {
db := setupTestDatabase("testalive")
env := &Env{db: db}
router := setupRouter(env, spotifyClientID, spotifyClientSecret)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
req, _ := http.NewRequest("GET", "/alive", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
assert.Equal(t, "yes!", w.Body.String())
}
func TestGetArtistByIDRoute(t *testing.T) {