5 Commits

Author SHA1 Message Date
e16bcc1780 Simplify httpGet function in client_test.go for cleaner code
All checks were successful
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Successful in 1m9s
🚨 Test Code Base / 🧹 Lint (push) Successful in 1m22s
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Successful in 4m6s
2026-01-21 21:48:00 -05:00
e489896af2 Update Go version to 1.25.6 in Dockerfile and lint configuration
Some checks failed
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Failing after 4m1s
🚨 Test Code Base / 🧹 Lint (push) Failing after 4m0s
2026-01-21 21:23:36 -05:00
24873fb0d7 Update Go version to 1.25.3 in Dockerfile and lint configuration
Some checks failed
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Failing after 4m30s
🚨 Test Code Base / 🧹 Lint (push) Failing after 4m42s
2026-01-21 21:16:44 -05:00
d321d96fef Refactor caching logic and add support for a new feature
Some checks failed
🚨 Test Code Base / 🧹 Lint (push) Failing after 5m24s
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Failing after 5m45s
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Failing after 6m14s
2026-01-21 21:08:26 -05:00
838051c880 update-golang (#2)
All checks were successful
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Successful in 1m12s
🚨 Test Code Base / 🧹 Lint (push) Successful in 2m9s
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Successful in 4m50s
Reviewed-on: #2
Co-authored-by: Ron Rise <ron@siteworxpro.com>
Co-committed-by: Ron Rise <ron@siteworxpro.com>
2025-08-31 02:34:23 +00:00
9 changed files with 132 additions and 107 deletions

View File

@@ -7,7 +7,7 @@ name: 🚨 Test Code Base
run-name: ${{ github.workflow }}-{{ github.ref_name }}-{{ github.run_number }}
env:
GO_VERSION: '1.24.6'
GO_VERSION: '1.25.6'
jobs:
test-go:
@@ -15,11 +15,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 🛡️ 🔒 Add Siteworx CA Certificates
run: |
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
update-ca-certificates
- name: ⚙️ 🐹 Set up Go Environment
uses: actions/setup-go@v2
with:

View File

@@ -1,4 +1,4 @@
FROM siteworxpro/golang:1.24.6 AS build
FROM siteworxpro/golang:1.25.6 AS build
WORKDIR /app

2
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/siteworxpro/top-wallpaper
go 1.24.6
go 1.25.6
require (
github.com/labstack/echo/v4 v4.13.4

View File

@@ -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)

View File

@@ -28,6 +28,8 @@ func main() {
}
e := echo.New()
defer e.Close()
e.AcquireContext().Set("redisClient", rc)
e.Logger.SetOutput(os.Stdout)

View File

@@ -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
}

View File

@@ -1,7 +1,6 @@
package reddit
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -50,11 +49,8 @@ func TestGetLatestImage(t *testing.T) {
defer server.Close()
// Custom httpGet function for testing
httpGet := func(url string) (*http.Response, error) {
if strings.Contains(url, "/r/wallpaper/.json") {
return http.Get(server.URL + "/r/wallpaper/.json")
}
return nil, fmt.Errorf("unexpected URL: %s", url)
httpGet := func() (*http.Response, error) {
return http.Get(server.URL + "/r/wallpaper/.json")
}
// Test the function

View File

@@ -50,99 +50,113 @@ func Fetch(ctx context.Context) {
return
default:
val, err := rc.Get(redis.CacheKey)
if err != nil {
l.Error("Error fetching from Redis: ", err)
break
doFetch(rc, l, false, size)
if os.Getenv("NSFW_MODE") == "true" {
doFetch(rc, l, true, size)
}
if val != "" {
l.Info("Reddit image fetched from cache...")
break
}
l.Info("Fetching latest image from Reddit...")
latestImageVal, err := GetLatestImage(func(u string) (*http.Response, error) {
request := &http.Request{
Method: http.MethodGet,
Header: http.Header{
"User-Agent": []string{"Mozilla/5.0 (compatible; RedditBot/1.0; +https://www.reddit.com/wiki/redditauth)"},
},
URL: &url.URL{
Scheme: "https",
Host: "www.reddit.com",
Path: strings.TrimPrefix(u, "https://www.reddit.com"),
},
}
httpClient := &http.Client{
Timeout: 10 * time.Second,
}
response, e := httpClient.Do(request)
if e != nil {
l.Error("Error fetching image from Reddit: ", e)
return nil, e
}
if response.StatusCode != http.StatusOK {
l.Error("Error fetching image from Reddit: ", response.Status)
return nil, &echo.HTTPError{
Code: response.StatusCode,
Message: "Error fetching image from Reddit",
}
}
return response, nil
})
if err != nil {
l.Error("Error getting latest image URL: ", err)
break
}
response, err := http.Get(latestImageVal)
if err != nil {
l.Error("Error fetching image from Reddit: ", err)
break
}
if response.StatusCode != http.StatusOK {
l.Error("Error fetching image from Reddit: ", response.Status)
break
}
imageDataBytes, err := io.ReadAll(response.Body)
if err != nil {
l.Error("Error reading image data: ", err)
_ = response.Body.Close()
break
}
_ = response.Body.Close()
imageData := string(imageDataBytes)
resized, err := resize.Shrink(imageData, uint(size), 70)
if err != nil {
l.Error("Error resizing image: ", err)
break
}
err = rc.Set(redis.CacheKey, 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)
}
}
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)
return
}
if val != "" {
l.Info("Reddit image fetched from cache...")
return
}
l.Info("Fetching latest image from Reddit...")
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,
Header: http.Header{
"User-Agent": []string{"Mozilla/5.0 (compatible; RedditBot/1.0; +https://www.reddit.com/wiki/redditauth)"},
},
URL: &url.URL{
Scheme: "https",
Host: "www.reddit.com",
Path: strings.TrimPrefix(jsonUrl, "https://www.reddit.com"),
},
}
httpClient := &http.Client{
Timeout: 10 * time.Second,
}
response, e := httpClient.Do(request)
if e != nil {
l.Error("Error fetching image from Reddit: ", e)
return nil, e
}
if response.StatusCode != http.StatusOK {
l.Error("Error fetching image from Reddit: ", response.Status)
return nil, &echo.HTTPError{
Code: response.StatusCode,
Message: "Error fetching image from Reddit",
}
}
return response, nil
})
if err != nil {
l.Error("Error getting latest image URL: ", err)
return
}
response, err := http.Get(latestImageVal)
if err != nil {
l.Error("Error fetching image from Reddit: ", err)
return
}
if response.StatusCode != http.StatusOK {
l.Error("Error fetching image from Reddit: ", response.Status)
return
}
imageDataBytes, err := io.ReadAll(response.Body)
if err != nil {
l.Error("Error reading image data: ", err)
_ = response.Body.Close()
return
}
_ = response.Body.Close()
imageData := string(imageDataBytes)
resized, err := resize.Shrink(imageData, uint(size), 70)
if err != nil {
l.Error("Error resizing image: ", err)
return
}
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.")
}

View File

@@ -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