You've already forked top-wallpaper
Refactor caching logic and add support for a new feature
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
||||
module github.com/siteworxpro/top-wallpaper
|
||||
|
||||
go 1.25.0
|
||||
go 1.25.6
|
||||
|
||||
require (
|
||||
github.com/labstack/echo/v4 v4.13.4
|
||||
|
||||
@@ -2,6 +2,7 @@ package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/siteworxpro/top-wallpaper/redis"
|
||||
@@ -14,7 +15,10 @@ func Get(c echo.Context) error {
|
||||
return c.String(http.StatusInternalServerError, "Internal Server Error: "+err.Error())
|
||||
}
|
||||
|
||||
val, err := rc.Get(redis.CacheKey)
|
||||
defer rc.Close()
|
||||
|
||||
nsfw := (c.QueryParam("nsfw") == "true" || c.QueryParam("nsfw") == "1") && os.Getenv("NSFW_MODE") == "true"
|
||||
val, err := rc.Get(rc.GetCacheKey(nsfw))
|
||||
|
||||
if err != nil || val == "" {
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
|
||||
2
main.go
2
main.go
@@ -28,6 +28,8 @@ func main() {
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
defer e.Close()
|
||||
|
||||
e.AcquireContext().Set("redisClient", rc)
|
||||
e.Logger.SetOutput(os.Stdout)
|
||||
|
||||
|
||||
@@ -54,9 +54,14 @@ type redditResponse struct {
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func GetLatestImage(httpGet func(url string) (*http.Response, error)) (string, error) {
|
||||
const (
|
||||
wallpaperURL = "https://www.reddit.com/r/wallpaper/.json"
|
||||
nsfwWallpaperURL = "https://www.reddit.com/r/NSFW_Wallpapers/.json"
|
||||
)
|
||||
|
||||
response, err := httpGet("https://www.reddit.com/r/wallpaper/.json")
|
||||
func GetLatestImage(httpGet func() (*http.Response, error)) (string, error) {
|
||||
|
||||
response, err := httpGet()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -50,21 +50,39 @@ func Fetch(ctx context.Context) {
|
||||
return
|
||||
default:
|
||||
|
||||
val, err := rc.Get(redis.CacheKey)
|
||||
doFetch(rc, l, false, size)
|
||||
|
||||
if os.Getenv("NSFW_MODE") == "true" {
|
||||
doFetch(rc, l, true, size)
|
||||
}
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
func doFetch(rc *redis.Redis, l echo.Logger, nsfw bool, size int) {
|
||||
val, err := rc.Get(rc.GetCacheKey(nsfw))
|
||||
if err != nil {
|
||||
l.Error("Error fetching from Redis: ", err)
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
if val != "" {
|
||||
l.Info("Reddit image fetched from cache...")
|
||||
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
l.Info("Fetching latest image from Reddit...")
|
||||
|
||||
latestImageVal, err := GetLatestImage(func(u string) (*http.Response, error) {
|
||||
latestImageVal, err := GetLatestImage(func() (*http.Response, error) {
|
||||
|
||||
jsonUrl := wallpaperURL
|
||||
nsfwMode := os.Getenv("NSFW_MODE")
|
||||
if nsfwMode == "true" {
|
||||
jsonUrl = nsfwWallpaperURL
|
||||
}
|
||||
|
||||
request := &http.Request{
|
||||
Method: http.MethodGet,
|
||||
@@ -74,7 +92,7 @@ func Fetch(ctx context.Context) {
|
||||
URL: &url.URL{
|
||||
Scheme: "https",
|
||||
Host: "www.reddit.com",
|
||||
Path: strings.TrimPrefix(u, "https://www.reddit.com"),
|
||||
Path: strings.TrimPrefix(jsonUrl, "https://www.reddit.com"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -102,27 +120,27 @@ func Fetch(ctx context.Context) {
|
||||
|
||||
if err != nil {
|
||||
l.Error("Error getting latest image URL: ", err)
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
response, err := http.Get(latestImageVal)
|
||||
if err != nil {
|
||||
l.Error("Error fetching image from Reddit: ", err)
|
||||
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
l.Error("Error fetching image from Reddit: ", response.Status)
|
||||
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
imageDataBytes, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
l.Error("Error reading image data: ", err)
|
||||
_ = response.Body.Close()
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
_ = response.Body.Close()
|
||||
@@ -132,17 +150,13 @@ func Fetch(ctx context.Context) {
|
||||
|
||||
if err != nil {
|
||||
l.Error("Error resizing image: ", err)
|
||||
break
|
||||
return
|
||||
}
|
||||
|
||||
err = rc.Set(redis.CacheKey, resized, 10*time.Minute)
|
||||
err = rc.Set(rc.GetCacheKey(nsfw), resized, 10*time.Minute)
|
||||
if err != nil {
|
||||
l.Warn("could not cache image")
|
||||
}
|
||||
|
||||
l.Info("Reddit image fetched and resized successfully, cached for 10 minutes.")
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ type contextKey string
|
||||
const redisKey contextKey = "redisClient"
|
||||
|
||||
const CacheKey = "top-wallpaper:latestImage"
|
||||
const NsfwCacheKey = "top-wallpaper-nsfw:latestImage"
|
||||
|
||||
type Cache interface {
|
||||
Get(key string) (string, error)
|
||||
@@ -92,6 +93,14 @@ func (r *Redis) Set(key string, value string, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Redis) GetCacheKey(nsfw bool) string {
|
||||
if nsfw {
|
||||
return NsfwCacheKey
|
||||
}
|
||||
|
||||
return CacheKey
|
||||
}
|
||||
|
||||
func (r *Redis) Close() error {
|
||||
if r.client == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user