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