Implement Redis caching for Reddit image fetching

This commit is contained in:
2025-07-22 12:13:38 -04:00
parent 8d0f11e681
commit 88ef0c23a0
5 changed files with 280 additions and 156 deletions

26
main.go
View File

@@ -1,10 +1,13 @@
package main
import (
"context"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
tphttp "github.com/siteworxpro/top-wallpaper/http"
"github.com/siteworxpro/top-wallpaper/reddit"
"github.com/siteworxpro/top-wallpaper/redis"
"net/http"
"os"
"strings"
@@ -12,12 +15,23 @@ import (
func main() {
e := echo.New()
e.Logger.SetLevel(log.INFO)
ctx, fn := context.WithCancel(context.Background())
defer fn()
rc, err := redis.NewRedis()
if err != nil {
log.Error("Could not initialize Redis client: ", err)
os.Exit(1)
}
e := echo.New()
e.AcquireContext().Set("redisClient", rc)
e.Logger.SetOutput(os.Stdout)
e.Logger.SetLevel(log.INFO)
e.HideBanner = true
e.Use(tphttp.GetCustomContext)
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
@@ -34,12 +48,16 @@ func main() {
e.GET(path, tphttp.Get)
e.Logger.Info("Starting server at path " + path)
// start the server
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
ctx = redis.WithContext(ctx, rc)
ctx = context.WithValue(ctx, "logger", e.Logger)
go reddit.Fetch(ctx)
e.Logger.Fatal(e.Start(":" + port))
}