Refactor API and background updater functionality
- Updated the StartBackgroundUpdater function to accept a callback for software list updates, improving flexibility. - Refactored the API handlers to utilize a proxied software list, enhancing data handling and response efficiency. - Introduced a new method for refreshing the proxied software list, ensuring accurate data representation. - Added unit tests for API handlers to validate functionality and response correctness.
This commit is contained in:
@@ -49,7 +49,6 @@ func LoadSoftwareExtended(path, server, token string, useCache bool) []models.So
|
||||
if len(parts) == 2 {
|
||||
owner, repo := parts[0], parts[1]
|
||||
|
||||
// Try to get from cache first
|
||||
if useCache {
|
||||
if cached, err := cache.GetFromCache(owner, repo); err == nil {
|
||||
softwareList = append(softwareList, *cached)
|
||||
|
||||
@@ -51,12 +51,21 @@ func TestBackgroundUpdater(t *testing.T) {
|
||||
|
||||
var mu sync.RWMutex
|
||||
softwareList := &[]models.Software{}
|
||||
StartBackgroundUpdater(configPath, mockGitea.URL, "", &mu, softwareList, 100*time.Millisecond)
|
||||
onUpdate := func(newList []models.Software) {
|
||||
mu.Lock()
|
||||
*softwareList = newList
|
||||
mu.Unlock()
|
||||
}
|
||||
StartBackgroundUpdater(configPath, mockGitea.URL, "", 100*time.Millisecond, onUpdate)
|
||||
|
||||
// Wait for ticker
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
|
||||
if len(*softwareList) == 0 {
|
||||
mu.RLock()
|
||||
length := len(*softwareList)
|
||||
mu.RUnlock()
|
||||
|
||||
if length == 0 {
|
||||
t.Error("softwareList was not updated by background updater")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,19 @@ package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"software-station/internal/models"
|
||||
)
|
||||
|
||||
func StartBackgroundUpdater(path, server, token string, mu *sync.RWMutex, softwareList *[]models.Software, interval time.Duration) {
|
||||
func StartBackgroundUpdater(path, server, token string, interval time.Duration, onUpdate func([]models.Software)) {
|
||||
ticker := time.NewTicker(interval)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
log.Println("Checking for software updates...")
|
||||
newList := LoadSoftwareFromGitea(path, server, token)
|
||||
if len(newList) > 0 {
|
||||
mu.Lock()
|
||||
*softwareList = newList
|
||||
mu.Unlock()
|
||||
onUpdate(newList)
|
||||
log.Printf("Software list updated with %d items", len(newList))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user