You've already forked top-wallpaper
Refactor error handling and improve code readability
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/siteworxpro/top-wallpaper/redis"
|
"github.com/siteworxpro/top-wallpaper/redis"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get(c echo.Context) error {
|
func Get(c echo.Context) error {
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -14,6 +14,10 @@ import (
|
|||||||
"github.com/siteworxpro/top-wallpaper/redis"
|
"github.com/siteworxpro/top-wallpaper/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
const ContextKeyLogger contextKey = "logger"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
ctx, fn := context.WithCancel(context.Background())
|
ctx, fn := context.WithCancel(context.Background())
|
||||||
@@ -23,7 +27,8 @@ func main() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not initialize Redis client: ", err)
|
log.Error("Could not initialize Redis client: ", err)
|
||||||
os.Exit(1)
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
@@ -56,7 +61,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx = redis.WithContext(ctx, rc)
|
ctx = redis.WithContext(ctx, rc)
|
||||||
ctx = context.WithValue(ctx, "logger", e.Logger)
|
ctx = context.WithValue(ctx, ContextKeyLogger, e.Logger)
|
||||||
|
|
||||||
go reddit.Fetch(ctx)
|
go reddit.Fetch(ctx)
|
||||||
|
|
||||||
|
|||||||
@@ -60,15 +60,16 @@ func GetLatestImage(httpGet func(url string) (*http.Response, error)) (string, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer func(Body io.ReadCloser) {
|
defer response.Body.Close()
|
||||||
_ = Body.Close()
|
|
||||||
}(response.Body)
|
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
return "", fmt.Errorf("error fetching reddit data: %s", response.Status)
|
return "", fmt.Errorf("error fetching reddit data: %s", response.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, err := io.ReadAll(response.Body)
|
jsonBytes, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
redditData := redditResponse{}
|
redditData := redditResponse{}
|
||||||
err = json.Unmarshal(jsonBytes, &redditData)
|
err = json.Unmarshal(jsonBytes, &redditData)
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package reddit
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/siteworxpro/top-wallpaper/redis"
|
|
||||||
"github.com/siteworxpro/top-wallpaper/resize"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -12,6 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/siteworxpro/top-wallpaper/redis"
|
||||||
|
"github.com/siteworxpro/top-wallpaper/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Fetch(ctx context.Context) {
|
func Fetch(ctx context.Context) {
|
||||||
@@ -50,6 +51,11 @@ func Fetch(ctx context.Context) {
|
|||||||
default:
|
default:
|
||||||
|
|
||||||
val, err := rc.Get(redis.CacheKey)
|
val, err := rc.Get(redis.CacheKey)
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error fetching from Redis: ", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
if val != "" {
|
if val != "" {
|
||||||
l.Info("Reddit image fetched from cache...")
|
l.Info("Reddit image fetched from cache...")
|
||||||
|
|
||||||
@@ -76,11 +82,11 @@ func Fetch(ctx context.Context) {
|
|||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := httpClient.Do(request)
|
response, e := httpClient.Do(request)
|
||||||
|
|
||||||
if err != nil {
|
if e != nil {
|
||||||
l.Error("Error fetching image from Reddit: ", err)
|
l.Error("Error fetching image from Reddit: ", e)
|
||||||
return nil, err
|
return nil, e
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
@@ -94,6 +100,11 @@ func Fetch(ctx context.Context) {
|
|||||||
return response, nil
|
return response, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error getting latest image URL: ", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
response, err := http.Get(latestImageVal)
|
response, err := http.Get(latestImageVal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Error("Error fetching image from Reddit: ", err)
|
l.Error("Error fetching image from Reddit: ", err)
|
||||||
@@ -119,6 +130,11 @@ func Fetch(ctx context.Context) {
|
|||||||
imageData := string(imageDataBytes)
|
imageData := string(imageDataBytes)
|
||||||
resized, err := resize.Shrink(imageData, uint(size), 70)
|
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)
|
err = rc.Set(redis.CacheKey, resized, 10*time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warn("could not cache image")
|
l.Warn("could not cache image")
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
type contextKey string
|
type contextKey string
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ package resize
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nfnt/resize"
|
|
||||||
i "image"
|
i "image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
|
||||||
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Shrink(image string, maxSize uint, quality int) (string, error) {
|
func Shrink(image string, maxSize uint, quality int) (string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user