gomusic/main.go

43 lines
1.2 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
}
// Grab some required spotify credentials from the environment
var spotifyClientID = os.Getenv("SPOTIFY_ID")
var spotifyClientSecret = os.Getenv("SPOTIFY_SECRET")
func setupRouter(env *Env, spotifyID string, spotifySecret string) *gin.Engine {
var r *gin.Engine = gin.Default()
r.Use(spotifyAuth(spotifyID, spotifySecret))
r.GET("/alive", env.alive)
r.GET("/artists/:artistID", env.getArtistByID)
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_SECRET' environment variable")
}
var db = setupDatabase()
env := &Env{db: db}
// Router/middleware and server setup
r := setupRouter(env, spotifyClientID, spotifyClientSecret)
r.Run(":8000")
}