Skip to content

Commit

Permalink
chore: use environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Aug 9, 2024
1 parent 244bfa5 commit e0ee906
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Go workspace file
go.work

tmp.go
tmp.go

config.yaml
File renamed without changes.
13 changes: 7 additions & 6 deletions api/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"scc/config"
"scc/screen"
"slices"

Expand All @@ -20,13 +21,13 @@ type cammieHeader struct {
IP string `header:"X-Real-IP"`
}

var cammieCessages uint64 = 0
var cammieBlockedNames = []string{"Paul-Henri Spaak"} // Blocked names
var cammieBlockedIps = []string{} // Blocked IPs
var cammieMaxMessageLength = 200 // Maximum message length
var cammieMessages uint64 = 0
var cammieBlockedNames = config.GetConfig().Cammie.BlockedNames // Blocked names
var cammieBlockedIps = config.GetConfig().Cammie.BlockedIps // Blocked IPs
var cammieMaxMessageLength = config.GetConfig().Cammie.MaxMessageLength // Maximum message length

func cammieGetMessage(app *screen.ScreenApp, c *gin.Context) {
c.JSON(200, gin.H{"messages": cammieCessages})
c.JSON(200, gin.H{"messages": cammieMessages})
}

func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) {
Expand Down Expand Up @@ -71,7 +72,7 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) {
}

// Increment messages
cammieCessages++
cammieMessages++

app.Cammie.Update(newMessage)

Expand Down
22 changes: 15 additions & 7 deletions api/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"scc/config"
"scc/screen"
"strings"
"time"
Expand All @@ -32,10 +33,17 @@ type spotifyTrackResponse struct {
Artists []spotifyArtist `json:"artists"`
}

var spotifyAccessToken = ""
var spotifyExpiresOn int64 = 0
var spotifyClientID = "d385173507a54bca93cc3327c0c2f5d9"
var spotifyClientSecret = "8e78977c1ba54b90b17f9dcd6b301c37"
var (
spotifyAccessToken = ""
spotifyExpiresOn int64 = 0
spotifyClientID = config.GetConfig().Spotify.ClientID
spotifyClientSecret = config.GetConfig().Spotify.ClientSecret
)

// var spotifyAccessToken = ""
// var spotifyExpiresOn int64 = 0
// var spotifyClientID = "d385173507a54bca93cc3327c0c2f5d9"
// var spotifyClientSecret = "8e78977c1ba54b90b17f9dcd6b301c37"

func spotifyGetMessage(app *screen.ScreenApp, ctx *gin.Context) {
message := &spotifyMessage{}
Expand All @@ -49,14 +57,14 @@ func spotifyGetMessage(app *screen.ScreenApp, ctx *gin.Context) {

if spotifyExpiresOn < time.Now().Unix() {
if err := spotifySetAccessToken(); err != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to refresh spotify token: %s", err)
log.Printf("Error: Unable to refresh spotify token: %s\n", err)
}
}

track, err := spotifyGetTrackTitle(message.TrackID)

if err != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to get track information: %s", err)
log.Printf("Error: Unable to get track information: %s\n", err)
}

app.Spotify.Update(track)
Expand Down
7 changes: 7 additions & 0 deletions config.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cammie:
blocked_names: []
blocked_ips: []
max_message_length:
spotify:
client_id:
client_secret:
44 changes: 44 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"log"
"os"
"sync"

"gopkg.in/yaml.v3"
)

type cammieConfig struct {
BlockedNames []string `yaml:"blocked_names"`
BlockedIps []string `yaml:"blocked_ips"`
MaxMessageLength int `yaml:"max_message_length"`
}

type spotifyConfig struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
}

type Config struct {
Cammie cammieConfig `yaml:"cammie"`
Spotify spotifyConfig `yaml:"spotify"`
}

var (
configInstance *Config
once sync.Once
)

func GetConfig() *Config {
once.Do(func() {
configInstance = &Config{}
data, err := os.ReadFile("config.yaml")
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
if err := yaml.Unmarshal(data, configInstance); err != nil {
log.Fatalf("Failed to unmarshal config: %v", err)
}
})
return configInstance
}
23 changes: 0 additions & 23 deletions screen/graph1.go

This file was deleted.

8 changes: 4 additions & 4 deletions screen/main.go → screen/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ScreenApp struct {

Spotify *Spotify
Cammie *Cammie
Graph1 *Graph1
Tap *Tap
Graph2 *Graph2
}

Expand All @@ -32,7 +32,7 @@ func NewScreenApp() *ScreenApp {

screen.Spotify = NewSpotify(&screen)
screen.Cammie = NewCammie(&screen)
screen.Graph1 = NewGraph1(&screen)
screen.Tap = NewTap(&screen)
screen.Graph2 = NewGraph2(&screen)

// Build the screen layout
Expand All @@ -41,7 +41,7 @@ func NewScreenApp() *ScreenApp {
AddItem(tview.NewFlex().
AddItem(screen.Cammie.view, 0, 5, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(screen.Graph1.view, 0, 1, false).
AddItem(screen.Tap.view, 0, 1, false).
AddItem(screen.Graph2.view, 0, 1, false), 0, 4, false), 0, 13, false), true).
EnableMouse(true)

Expand All @@ -54,7 +54,7 @@ func Start(screen *ScreenApp) {
// Start each screen component
go screen.Spotify.Run()
go screen.Cammie.Run()
go screen.Graph1.Run()
go screen.Tap.Run()
go screen.Graph2.Run()

// Start the screen application
Expand Down
23 changes: 23 additions & 0 deletions screen/tap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package screen

import "github.com/rivo/tview"

type Tap struct {
ScreenApp *ScreenApp
view *tview.Box
}

func NewTap(screenApp *ScreenApp) *Tap {
tap := Tap{
ScreenApp: screenApp,
view: tview.NewBox().SetBorder(true).SetTitle(" Tap "),
}

return &tap
}

func (tap *Tap) Run() {
}

func (tap *Tap) Update(text string) {
}
25 changes: 25 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,34 @@ package utils

import (
"math/rand/v2"
"os"
"strconv"
"time"
)

func GetEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}

func GetEnvAsInt(name string, defaultValue int) int {
valueStr := os.Getenv(name)
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultValue
}

func GetEnvAsBool(name string, defaultValue bool) bool {
valueStr := os.Getenv(name)
if value, err := strconv.ParseBool(valueStr); err == nil {
return value
}
return defaultValue
}

func RandRange(min, max int) int {
return rand.IntN(max-min) + min
}
Expand Down

0 comments on commit e0ee906

Please sign in to comment.