adding spotify what am I listening to api
This commit is contained in:
57
backend/services/database.go
Normal file
57
backend/services/database.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"adam-french.co.uk/backend/models"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func connectToPostgreSQL() (*gorm.DB, error) {
|
||||
user := os.Getenv("POSTGRES_USER")
|
||||
password := os.Getenv("POSTGRES_PASSWORD")
|
||||
dbname := os.Getenv("POSTGRES_DB")
|
||||
host := os.Getenv("POSTGRES_HOST")
|
||||
port := os.Getenv("POSTGRES_PORT")
|
||||
|
||||
dsn := fmt.Sprintf(
|
||||
"user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
|
||||
user, password, dbname, host, port,
|
||||
)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func migrateDatabase(db *gorm.DB) error {
|
||||
err := db.AutoMigrate(&models.User{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.AutoMigrate(&models.Post{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitDatabase() (*gorm.DB, error) {
|
||||
db, err := connectToPostgreSQL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = migrateDatabase(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
@@ -1 +1,35 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/zmb3/spotify/v2"
|
||||
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
||||
"golang.org/x/oauth2/clientcredentials"
|
||||
)
|
||||
|
||||
func InitSpotify() (*spotify.Client, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// redirectURI := os.Getenv("SPOTIFY_REDIRECT_URI")
|
||||
clientID := os.Getenv("SPOTIFY_CLIENT_ID")
|
||||
clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET")
|
||||
|
||||
config := &clientcredentials.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
TokenURL: spotifyauth.TokenURL,
|
||||
}
|
||||
|
||||
token, err := config.Token(context.Background())
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't get token: %v", err)
|
||||
}
|
||||
|
||||
httpClient := spotifyauth.New().Client(ctx, token)
|
||||
client := spotify.New(httpClient)
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user