Skip to content

Commit

Permalink
refactor: improve some naming
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jul 28, 2024
1 parent 84af2fc commit f323a8e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cache/file.go → pkg/cache/stroage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f323a8e

Please sign in to comment.