From f323a8e0484f7ddc4290da8e4351f4a6972ec7a7 Mon Sep 17 00:00:00 2001 From: Matt Gleich Date: Sun, 28 Jul 2024 13:03:39 -0400 Subject: [PATCH] refactor: improve some naming Signed-off-by: Matt Gleich --- main.go | 16 ++++++++-------- pkg/cache/cache.go | 10 +++++----- pkg/cache/{file.go => stroage.go} | 8 ++++---- 3 files changed, 17 insertions(+), 17 deletions(-) rename pkg/cache/{file.go => stroage.go} (88%) diff --git a/main.go b/main.go index c5dba4b..ae240ad 100644 --- a/main.go +++ b/main.go @@ -48,10 +48,10 @@ func main() { ) githubHttpClient := oauth2.NewClient(context.Background(), githubTokenSource) githubClient := githubv4.NewClient(githubHttpClient) - githubCache := cache.New("github", github.FetchPinnedRepos(githubClient)) - r.Get("/github/cache", githubCache.Route()) + githubCache := cache.NewCache("github", github.FetchPinnedRepos(githubClient)) + r.Get("/github/cache", githubCache.ServeHTTP()) lumber.Success("setup github cache") - go githubCache.PeriodicUpdate(func() []github.Repository { return github.FetchPinnedRepos(githubClient) }, 5*time.Minute) + go githubCache.StartPeriodicUpdate(func() []github.Repository { return github.FetchPinnedRepos(githubClient) }, 5*time.Minute) stravaTokens := strava.LoadTokens() stravaTokens.RefreshIfNeeded() @@ -63,17 +63,17 @@ func main() { lumber.Fatal(err, "failed to create minio client") } stravaActivities := strava.FetchActivities(*minioClient, stravaTokens) - stravaCache := cache.New("strava", stravaActivities) - r.Get("/strava/cache", stravaCache.Route()) + stravaCache := cache.NewCache("strava", stravaActivities) + r.Get("/strava/cache", stravaCache.ServeHTTP()) r.Post("/strava/event", strava.EventRoute(stravaCache, *minioClient, stravaTokens)) r.Get("/strava/event", strava.ChallengeRoute) lumber.Success("setup strava cache") games := steam.FetchRecentlyPlayedGames() - steamCache := cache.New("steam", games) - r.Get("/steam/cache", steamCache.Route()) + steamCache := cache.NewCache("steam", games) + r.Get("/steam/cache", steamCache.ServeHTTP()) lumber.Success("setup steam cache") - go steamCache.PeriodicUpdate(steam.FetchRecentlyPlayedGames, 5*time.Minute) + go steamCache.StartPeriodicUpdate(steam.FetchRecentlyPlayedGames, 5*time.Minute) lumber.Info("starting server") err = http.ListenAndServe(":8000", r) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index f6ad01f..04dff2c 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -25,7 +25,7 @@ type Cache[T any] struct { filePath string } -func New[T any](name string, data T) *Cache[T] { +func NewCache[T any](name string, data T) *Cache[T] { cache := Cache[T]{ Name: name, updateCounter: promauto.NewCounter(prometheus.CounterOpts{ @@ -38,7 +38,7 @@ func New[T any](name string, data T) *Cache[T] { }), filePath: filepath.Join(cacheFolder, fmt.Sprintf("%s.json", name)), } - cache.seedFromFile() + cache.loadFromFile() cache.Update(data) return &cache } @@ -49,7 +49,7 @@ type cacheData[T any] struct { } // Handle a GET request to load data from the given cache -func (c *Cache[T]) Route() http.HandlerFunc { +func (c *Cache[T]) ServeHTTP() http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "Bearer "+secrets.SECRETS.ValidToken { w.WriteHeader(http.StatusUnauthorized) @@ -89,12 +89,12 @@ func (c *Cache[T]) Update(data T) { if updated { c.updateCounter.Inc() metrics.CacheUpdates.Inc() - c.writeToFile() + c.persistToFile() lumber.Success(c.Name, "updated") } } -func (c *Cache[T]) PeriodicUpdate(updateFunc func() T, interval time.Duration) { +func (c *Cache[T]) StartPeriodicUpdate(updateFunc func() T, interval time.Duration) { ticker := time.NewTicker(interval) defer ticker.Stop() for range ticker.C { diff --git a/pkg/cache/file.go b/pkg/cache/stroage.go similarity index 88% rename from pkg/cache/file.go rename to pkg/cache/stroage.go index a65d562..987d0ca 100644 --- a/pkg/cache/file.go +++ b/pkg/cache/stroage.go @@ -10,7 +10,7 @@ import ( const cacheFolder = "/caches/" -func (c *Cache[T]) writeToFile() { +func (c *Cache[T]) persistToFile() { var file *os.File if _, err := os.Stat(c.filePath); os.IsNotExist(err) { folder := filepath.Dir(c.filePath) @@ -47,9 +47,9 @@ func (c *Cache[T]) writeToFile() { } } -// Seed the cache from the persistent cache file -// returns if the cache was able to be seeded or not -func (c *Cache[T]) seedFromFile() { +// Load the cache from the persistent cache file +// returns if the cache was able to be loaded or not +func (c *Cache[T]) loadFromFile() { if _, err := os.Stat(c.filePath); !os.IsNotExist(err) { b, err := os.ReadFile(c.filePath) if err != nil {