feat: Use env for credentials

This commit is contained in:
KaNaDaAT 2025-06-07 16:15:38 +02:00
parent d6bea0a568
commit c4b7f5a14a
7 changed files with 30 additions and 29 deletions

View file

@ -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
REACT_APP_BACKEND_URL=http://localhost:5000
SPOTIFY_CLIENT_ID=yourclientid
SPOTIFY_CLIENT_SECRET=yoursecret

View file

@ -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

View file

@ -1,4 +0,0 @@
{
"client_id": "",
"client_secret": ""
}

View file

@ -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

View file

@ -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=

View file

@ -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 {

View file

@ -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
}