gomusic/main.go

51 lines
1.4 KiB
Go

package main
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"log/slog"
"os"
)
// Environment type is used with route methods
// So that we can easily inject/contain context data like DB connections
type Env struct {
db *gorm.DB
}
var spotifyClientID = os.Getenv("SPOTIFY_ID")
var spotifyClientSecret = os.Getenv("SPOTIFY_TOKEN")
func setupRouter(env *Env, spotifyID string, spotifySecret string) *gin.Engine {
var r *gin.Engine = gin.Default()
// Do not trust any reverse proxy by default
// See: https://pkg.go.dev/github.com/gin-gonic/gin#Engine.SetTrustedProxies
r.SetTrustedProxies(nil)
r.Use(spotifyAuth(spotifyID, spotifySecret))
r.Use(paginator())
r.GET("/alive", env.alive)
r.GET("/artists/:artistID", env.getArtistByID)
r.GET("/artists", env.getArtistByName)
r.GET("/genres", env.getGenres)
// POST create endpoint
r.POST("/genres", env.createGenre)
return r
}
func main() {
// If the auth/ID variables are empty something is probably misconfigured
if spotifyClientID == "" {
slog.Warn("[GOMUSIC] No Spotify ID configured in 'SPOTIFY_ID' environment variable")
}
if spotifyClientSecret == "" {
slog.Warn("[GOMUSIC] No Spotify secret configured in 'SPOTIFY_TOKEN' environment variable")
}
db := setupDatabase()
env := &Env{db: db}
// Router/middleware and server setup
r := setupRouter(env, spotifyClientID, spotifyClientSecret)
r.Run(":8000")
}