From 071975a022d85fc27e73a429476576eb01c4c169 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sun, 8 Dec 2024 22:19:27 +0100 Subject: [PATCH] feat(config): yeet thread safety, just hope for the best --- internal/cmd/event.go | 6 ++--- internal/cmd/gamification.go | 6 ++--- internal/cmd/tap.go | 6 ++--- internal/cmd/zess.go | 14 +++++----- internal/pkg/event/api.go | 10 +++++--- internal/pkg/gamification/api.go | 3 ++- internal/pkg/song/account.go | 5 ++-- internal/pkg/song/api.go | 7 +++-- internal/pkg/tap/api.go | 3 ++- internal/pkg/zess/api.go | 6 +++-- pkg/config/config.go | 44 -------------------------------- tui/screen/cammie/cammie.go | 4 ++- tui/view/zess/zess.go | 4 ++- 13 files changed, 45 insertions(+), 73 deletions(-) diff --git a/internal/cmd/event.go b/internal/cmd/event.go index aad0111..6957ac2 100644 --- a/internal/cmd/event.go +++ b/internal/cmd/event.go @@ -13,14 +13,14 @@ import ( func Event(db *db.DB) (*event.Event, chan bool) { ev := event.New(db) done := make(chan bool) + interval := config.GetDefaultInt("backend.event.interval_s", 3600) - go eventPeriodicUpdate(ev, done) + go eventPeriodicUpdate(ev, done, interval) return ev, done } -func eventPeriodicUpdate(ev *event.Event, done chan bool) { - interval := config.GetDefaultInt("backend.event.interval_s", 3600) +func eventPeriodicUpdate(ev *event.Event, done chan bool, interval int) { zap.S().Info("Event: Starting periodic leaderboard update with an interval of ", interval, " seconds") ticker := time.NewTimer(time.Duration(interval) * time.Second) diff --git a/internal/cmd/gamification.go b/internal/cmd/gamification.go index b1d88ce..8561625 100644 --- a/internal/cmd/gamification.go +++ b/internal/cmd/gamification.go @@ -13,14 +13,14 @@ import ( func Gamification(db *db.DB) (*gamification.Gamification, chan bool) { gam := gamification.New(db) done := make(chan bool) + interval := config.GetDefaultInt("backend.gamification.interval_s", 3600) - go gamificationPeriodicUpdate(gam, done) + go gamificationPeriodicUpdate(gam, done, interval) return gam, done } -func gamificationPeriodicUpdate(gam *gamification.Gamification, done chan bool) { - interval := config.GetDefaultInt("backend.gamification.interval_s", 3600) +func gamificationPeriodicUpdate(gam *gamification.Gamification, done chan bool, interval int) { zap.S().Info("Gamification: Starting periodic leaderboard update with an interval of ", interval, " seconds") ticker := time.NewTicker(time.Duration(interval) * time.Second) diff --git a/internal/cmd/tap.go b/internal/cmd/tap.go index 5c80425..00e97f5 100644 --- a/internal/cmd/tap.go +++ b/internal/cmd/tap.go @@ -13,14 +13,14 @@ import ( func Tap(db *db.DB) (*tap.Tap, chan bool) { tap := tap.New(db) done := make(chan bool) + interval := config.GetDefaultInt("backend.tap.interval_s", 60) - go tapPeriodicUpdate(tap, done) + go tapPeriodicUpdate(tap, done, interval) return tap, done } -func tapPeriodicUpdate(tap *tap.Tap, done chan bool) { - interval := config.GetDefaultInt("backend.tap.interval_s", 60) +func tapPeriodicUpdate(tap *tap.Tap, done chan bool, interval int) { zap.S().Info("Tap: Starting periodic update with an interval of ", interval, " seconds") ticker := time.NewTicker(time.Duration(interval) * time.Second) diff --git a/internal/cmd/zess.go b/internal/cmd/zess.go index f2fd499..03e3888 100644 --- a/internal/cmd/zess.go +++ b/internal/cmd/zess.go @@ -12,17 +12,20 @@ import ( // Zess starts the zess instance func Zess(db *db.DB) (*zess.Zess, chan bool, chan bool) { zess := zess.New(db) + doneSeason := make(chan bool) + intervalSeason := config.GetDefaultInt("backend.zess.interval_season_s", 300) + doneScan := make(chan bool) + intervalScan := config.GetDefaultInt("backend.zess.interval_scan_s", 60) - go zessPeriodicSeasonUpdate(zess, doneSeason) - go zessPeriodicScanUpdate(zess, doneScan) + go zessPeriodicSeasonUpdate(zess, doneSeason, intervalSeason) + go zessPeriodicScanUpdate(zess, doneScan, intervalScan) return zess, doneSeason, doneScan } -func zessPeriodicSeasonUpdate(zess *zess.Zess, done chan bool) { - interval := config.GetDefaultInt("backend.zess.interval_season_s", 300) +func zessPeriodicSeasonUpdate(zess *zess.Zess, done chan bool, interval int) { zap.S().Info("Zess: Starting periodic season update with an interval of ", interval, " seconds") ticker := time.NewTicker(time.Duration(interval) * time.Second) @@ -49,8 +52,7 @@ func zessPeriodicSeasonUpdate(zess *zess.Zess, done chan bool) { } } -func zessPeriodicScanUpdate(zess *zess.Zess, done chan bool) { - interval := config.GetDefaultInt("backend.zess.interval_scan_s", 60) +func zessPeriodicScanUpdate(zess *zess.Zess, done chan bool, interval int) { zap.S().Info("Zess: Starting periodic scan update with an interval of ", interval, " seconds") ticker := time.NewTicker(time.Duration(interval) * time.Second) diff --git a/internal/pkg/event/api.go b/internal/pkg/event/api.go index 2302ef6..926437e 100644 --- a/internal/pkg/event/api.go +++ b/internal/pkg/event/api.go @@ -18,11 +18,14 @@ import ( var layout = "Monday 02 January, 15:04 2006" +var ( + website = config.GetDefaultString("backend.event.website", "https://zeus.gent/events") + websitePoster = config.GetDefaultString("backend.event.website_poster", "https://git.zeus.gent/ZeusWPI/visueel/raw/branch/master") +) + func (e *Event) getEvents() ([]dto.Event, error) { zap.S().Info("Events: Getting all events") - website := config.GetDefaultString("backend.event.website", "https://zeus.gent/events") - var events []dto.Event var errs []error c := colly.NewCollector() @@ -83,7 +86,6 @@ func (e *Event) getEvents() ([]dto.Event, error) { func (e *Event) getPoster(event *dto.Event) error { zap.S().Info("Events: Getting poster for ", event.Name) - website := config.GetDefaultString("backend.event.website_poster", "https://git.zeus.gent/ZeusWPI/visueel/raw/branch/master") yearParts := strings.Split(event.AcademicYear, "-") if len(yearParts) != 2 { return fmt.Errorf("Event: Academic year not properly formatted %s", event.AcademicYear) @@ -100,7 +102,7 @@ func (e *Event) getPoster(event *dto.Event) error { year := fmt.Sprintf("20%d-20%d", yearStart, yearEnd) - url := fmt.Sprintf("%s/%s/%s/scc.png", website, year, event.Name) + url := fmt.Sprintf("%s/%s/%s/scc.png", websitePoster, year, event.Name) req := fiber.Get(url) status, body, errs := req.Bytes() diff --git a/internal/pkg/gamification/api.go b/internal/pkg/gamification/api.go index 280dd1e..161a36a 100644 --- a/internal/pkg/gamification/api.go +++ b/internal/pkg/gamification/api.go @@ -10,6 +10,8 @@ import ( "go.uber.org/zap" ) +var api = config.GetDefaultString("backend.gamification.api", "https://gamification.zeus.gent") + type gamificationItem struct { ID int32 `json:"id"` Name string `json:"github_name"` @@ -20,7 +22,6 @@ type gamificationItem struct { func (g *Gamification) getLeaderboard() ([]dto.Gamification, error) { zap.S().Info("Gamification: Getting leaderboard") - api := config.GetDefaultString("backend.gamification.api", "https://gamification.zeus.gent") req := fiber.Get(api+"/top4").Set("Accept", "application/json") res := new([]gamificationItem) diff --git a/internal/pkg/song/account.go b/internal/pkg/song/account.go index 9c779b0..7687121 100644 --- a/internal/pkg/song/account.go +++ b/internal/pkg/song/account.go @@ -9,6 +9,8 @@ import ( "go.uber.org/zap" ) +var apiAccount = config.GetDefaultString("backend.song.spotify_api_account", "https://accounts.spotify.com/api/token") + type accountResponse struct { AccessToken string `json:"access_token"` TokenType string `json:"token_type"` @@ -21,8 +23,7 @@ func (s *Song) refreshToken() error { form := &fiber.Args{} form.Add("grant_type", "client_credentials") - api := config.GetDefaultString("backend.song.spotify_api_account", "https://accounts.spotify.com/api/token") - req := fiber.Post(api).Form(form).BasicAuth(s.ClientID, s.ClientSecret) + req := fiber.Post(apiAccount).Form(form).BasicAuth(s.ClientID, s.ClientSecret) res := new(accountResponse) status, _, errs := req.Struct(res) diff --git a/internal/pkg/song/api.go b/internal/pkg/song/api.go index cf3eb49..48c2f8c 100644 --- a/internal/pkg/song/api.go +++ b/internal/pkg/song/api.go @@ -11,7 +11,10 @@ import ( "go.uber.org/zap" ) -var api = config.GetDefaultString("backend.song.spotify_api", "https://api.spotify.com/v1") +var ( + api = config.GetDefaultString("backend.song.spotify_api", "https://api.spotify.com/v1") + apiLrclib = config.GetDefaultString("backend.song.lrclib_api", "https://lrclib.net/api") +) type trackArtist struct { ID string `json:"id"` @@ -121,7 +124,7 @@ func (s *Song) getLyrics(track *dto.Song) error { params.Set("album_name", track.Album) params.Set("duration", fmt.Sprintf("%d", track.DurationMS/1000)) - req := fiber.Get(fmt.Sprintf("%s/get?%s", config.GetDefaultString("backend.song.lrclib_api", "https://lrclib.net/api"), params.Encode())) + req := fiber.Get(fmt.Sprintf("%s/get?%s", apiLrclib, params.Encode())) res := new(lyricsResponse) status, _, errs := req.Struct(res) diff --git a/internal/pkg/tap/api.go b/internal/pkg/tap/api.go index bfb76e3..e0803b5 100644 --- a/internal/pkg/tap/api.go +++ b/internal/pkg/tap/api.go @@ -9,6 +9,8 @@ import ( "go.uber.org/zap" ) +var api = config.GetDefaultString("backend.tap.api", "https://tap.zeus.gent") + type orderResponseItem struct { OrderID int32 `json:"order_id"` OrderCreatedAt time.Time `json:"order_created_at"` @@ -23,7 +25,6 @@ type orderResponse struct { func (t *Tap) getOrders() ([]orderResponseItem, error) { zap.S().Info("Tap: Getting orders") - api := config.GetDefaultString("backend.tap.api", "https://tap.zeus.gent") req := fiber.Get(api + "/recent") res := new(orderResponse) diff --git a/internal/pkg/zess/api.go b/internal/pkg/zess/api.go index 9bc8afb..1e88f6b 100644 --- a/internal/pkg/zess/api.go +++ b/internal/pkg/zess/api.go @@ -10,10 +10,13 @@ import ( "go.uber.org/zap" ) +var ( + api = config.GetDefaultString("backend.zess.api", "https://zess.zeus.gent") +) + func (z *Zess) getSeasons() (*[]*dto.Season, error) { zap.S().Info("Zess: Getting seasons") - api := config.GetDefaultString("backend.zess.api", "https://zess.zeus.gent") req := fiber.Get(api + "/seasons") res := new([]*dto.Season) @@ -38,7 +41,6 @@ func (z *Zess) getSeasons() (*[]*dto.Season, error) { func (z *Zess) getScans() (*[]*dto.Scan, error) { zap.S().Info("Zess: Getting scans") - api := config.GetDefaultString("backend.zess.api", "https://zess.zeus.gent") req := fiber.Get(api + "/recent_scans") res := new([]*dto.Scan) diff --git a/pkg/config/config.go b/pkg/config/config.go index 6c3e8b0..b8ace59 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,20 +4,14 @@ package config import ( "fmt" "strings" - "sync" "github.com/joho/godotenv" "github.com/spf13/viper" ) -var mu sync.Mutex - func bindEnv(key string) { envName := strings.ToUpper(strings.ReplaceAll(key, ".", "_")) - mu.Lock() - defer mu.Unlock() - _ = viper.BindEnv(key, envName) } @@ -27,9 +21,6 @@ func Init() error { return err } - mu.Lock() - defer mu.Unlock() - viper.AutomaticEnv() env := GetDefaultString("app.env", "development") @@ -43,18 +34,11 @@ func Init() error { // GetString returns the value of the key in string func GetString(key string) string { bindEnv(key) - - mu.Lock() - defer mu.Unlock() - return viper.GetString(key) } // GetDefaultString returns the value of the key in string or a default value func GetDefaultString(key, defaultValue string) string { - mu.Lock() - defer mu.Unlock() - viper.SetDefault(key, defaultValue) return GetString(key) } @@ -62,18 +46,11 @@ func GetDefaultString(key, defaultValue string) string { // GetStringSlice returns the value of the key in string slice func GetStringSlice(key string) []string { bindEnv(key) - - mu.Lock() - defer mu.Unlock() - return viper.GetStringSlice(key) } // GetDefaultStringSlice returns the value of the key in string slice or a default value func GetDefaultStringSlice(key string, defaultValue []string) []string { - mu.Lock() - defer mu.Unlock() - viper.SetDefault(key, defaultValue) return GetStringSlice(key) } @@ -81,18 +58,11 @@ func GetDefaultStringSlice(key string, defaultValue []string) []string { // GetInt returns the value of the key in int func GetInt(key string) int { bindEnv(key) - - mu.Lock() - defer mu.Unlock() - return viper.GetInt(key) } // GetDefaultInt returns the value of the key in int or a default value func GetDefaultInt(key string, defaultVal int) int { - mu.Lock() - defer mu.Unlock() - viper.SetDefault(key, defaultVal) return GetInt(key) } @@ -100,18 +70,11 @@ func GetDefaultInt(key string, defaultVal int) int { // GetUint16 returns the value of the key in uint16 func GetUint16(key string) uint16 { bindEnv(key) - - mu.Lock() - defer mu.Unlock() - return viper.GetUint16(key) } // GetDefaultUint16 returns the value of the key in uint16 or a default value func GetDefaultUint16(key string, defaultVal uint16) uint16 { - mu.Lock() - defer mu.Unlock() - viper.SetDefault(key, defaultVal) return GetUint16(key) } @@ -119,18 +82,11 @@ func GetDefaultUint16(key string, defaultVal uint16) uint16 { // GetBool returns the value of the key in bool func GetBool(key string) bool { bindEnv(key) - - mu.Lock() - defer mu.Unlock() - return viper.GetBool(key) } // GetDefaultBool returns the value of the key in bool or a default value func GetDefaultBool(key string, defaultVal bool) bool { - mu.Lock() - defer mu.Unlock() - viper.SetDefault(key, defaultVal) return GetBool(key) } diff --git a/tui/screen/cammie/cammie.go b/tui/screen/cammie/cammie.go index 29187db..62b5a1b 100644 --- a/tui/screen/cammie/cammie.go +++ b/tui/screen/cammie/cammie.go @@ -18,6 +18,8 @@ import ( "github.com/zeusWPI/scc/tui/view/zess" ) +var bottomTimeout = config.GetDefaultInt("tui.screen.cammie.interval_s", 300) + // Cammie represents the cammie screen type Cammie struct { db *db.DB @@ -162,7 +164,7 @@ func (c *Cammie) GetSizeMsg() tea.Msg { } func updateBottomIndex(cammie Cammie) tea.Cmd { - timeout := time.Duration(config.GetDefaultInt("tui.screen.cammie.interval_s", 300) * int(time.Second)) + timeout := time.Duration(bottomTimeout * int(time.Second)) return tea.Tick(timeout, func(_ time.Time) tea.Msg { newIndex := (cammie.indexTop + 1) % len(cammie.top) diff --git a/tui/view/zess/zess.go b/tui/view/zess/zess.go index 53bc632..e9d9963 100644 --- a/tui/view/zess/zess.go +++ b/tui/view/zess/zess.go @@ -13,6 +13,8 @@ import ( "go.uber.org/zap" ) +var maxWeeks = config.GetDefaultInt("tui.view.zess.weeks", 10) + // yearWeek represents a yearWeek object by keeping the year and week number type yearWeek struct { year int @@ -120,7 +122,7 @@ func (m *Model) Update(msg tea.Msg) (view.View, tea.Cmd) { m.maxWeekScans = newScan.amount } // Make sure the array doesn't get too big - if len(m.scans) > config.GetDefaultInt("tui.view.zess.weeks", 10) { + if len(m.scans) > maxWeeks { m.scans = m.scans[:1] } }