diff --git a/.env.example b/.env.example index bc65741..e30fdc5 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,6 @@ DB_PASS=password DB_NAME=seek-tune DB_HOST=192.168.0.1 DB_PORT=27017 -REACT_APP_BACKEND_URL=http://localhost:5000 \ No newline at end of file +REACT_APP_BACKEND_URL=http://localhost:5000 +SPOTIFY_CLIENT_ID=yourclientid +SPOTIFY_CLIENT_SECRET=yoursecret \ No newline at end of file diff --git a/README.md b/README.md index cfe6a68..3c66eb4 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,17 @@ Follow the [official getting started guide](https://developer.spotify.com/docume 1. Create a Spotify developer app. 2. Copy your **Client ID** and **Client Secret**. -Create a file named `credentials.json` in the `server/` directory with the following structure: +##### Setting up Credentials +Instead of using a credentials.json file, the application now reads these values from environment variables. + +Create a .env file in the server directory with the following content: -```json -{ - "client_id": "your-client-id", - "client_secret": "your-client-secret" -} ``` +SPOTIFY_CLIENT_ID=your-client-id +SPOTIFY_CLIENT_SECRET=your-client-secret +``` + +Make sure this .env file is loaded into your environment before running the server. The application will automatically read this file to fetch and cache access tokens. If the token is expired or missing, a new one will be requested. #### 💻 Set Up Natively diff --git a/server/credentials.json b/server/credentials.json deleted file mode 100644 index 4c7b62c..0000000 --- a/server/credentials.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "client_id": "", - "client_secret": "" -} diff --git a/server/go.mod b/server/go.mod index 34d6e2c..fd5a569 100644 --- a/server/go.mod +++ b/server/go.mod @@ -40,6 +40,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect + github.com/joho/godotenv v1.4.0 // indirect github.com/klauspost/compress v1.17.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/server/go.sum b/server/go.sum index e51b386..5aa3bf8 100644 --- a/server/go.sum +++ b/server/go.sum @@ -100,6 +100,8 @@ github.com/googollee/go-socket.io v1.7.0/go.mod h1:0vGP8/dXR9SZUMMD4+xxaGo/lohOw github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kkdai/youtube/v2 v2.10.1 h1:jdPho4R7VxWoRi9Wx4ULMq4+hlzSVOXxh4Zh83f2F9M= github.com/kkdai/youtube/v2 v2.10.1/go.mod h1:qL8JZv7Q1IoDs4nnaL51o/hmITXEIvyCIXopB0oqgVM= github.com/kkdai/youtube/v2 v2.10.4 h1:T3VAQ65EB4eHptwcQIigpFvUJlV9EcKRGJJdSVUy3aU= diff --git a/server/main.go b/server/main.go index 7836995..5fba59c 100644 --- a/server/main.go +++ b/server/main.go @@ -9,6 +9,7 @@ import ( "song-recognition/utils" "github.com/mdobak/go-xerrors" + "github.com/joho/godotenv" ) func main() { @@ -33,7 +34,8 @@ func main() { fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands") os.Exit(1) } - + _ = godotenv.Load() + switch os.Args[1] { case "find": if len(os.Args) < 3 { diff --git a/server/spotify/spotify.go b/server/spotify/spotify.go index 204a5c6..096558a 100644 --- a/server/spotify/spotify.go +++ b/server/spotify/spotify.go @@ -13,6 +13,7 @@ import ( "strings" "time" "os" + "song-recognition/utils" "github.com/tidwall/gjson" ) @@ -28,16 +29,14 @@ type Track struct { Duration int } - const ( tokenURL = "https://accounts.spotify.com/api/token" - credentialsPath = "credentials.json" cachedTokenPath = "token.json" ) type credentials struct { - ClientID string `json:"client_id"` - ClientSecret string `json:"client_secret"` + ClientID string + ClientSecret string } type tokenResponse struct { @@ -52,21 +51,17 @@ type cachedToken struct { } func loadCredentials() (*credentials, error) { - file, err := os.Open(credentialsPath) - if err != nil { - if os.IsNotExist(err) { - absPath, _ := os.Getwd() - return nil, fmt.Errorf("credentials.json not found. Please create it in the same directory:\n%s/%s", absPath, credentialsPath) - } - return nil, err - } - defer file.Close() + clientID := utils.GetEnv("SPOTIFY_CLIENT_ID", "") + clientSecret := utils.GetEnv("SPOTIFY_CLIENT_SECRET", "") - var creds credentials - if err := json.NewDecoder(file).Decode(&creds); err != nil { - return nil, fmt.Errorf("failed to parse credentials.json: %w", err) + if clientID == "" || clientSecret == "" { + return nil, fmt.Errorf("SPOTIFY_CLIENT_ID or SPOTIFY_CLIENT_SECRET environment variables not set") } - return &creds, nil + + return &credentials{ + ClientID: clientID, + ClientSecret: clientSecret, + }, nil }