Skip to content

Commit

Permalink
chore(image-proxy): improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Nov 29, 2023
1 parent 14e96f6 commit 489126a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
4 changes: 3 additions & 1 deletion pkg/api/helper.go
Expand Up @@ -9,12 +9,14 @@ import (
var proxyPathRe = regexp.MustCompile(`^/([^/]+)/([^/]+)/(.+)`)

// Decode image URL from Image Proxy Path
func decodeImageProxyPath(path string) (url string, err error) {
func decodeImageProxyPath(path string) (signature, options, url string, err error) {
parts := proxyPathRe.FindStringSubmatch(path)
if len(parts) != 4 {
err = errors.New("invalid image proxy path")
return
}
signature = parts[1]
options = parts[2]
encoded := parts[3]
var decoded []byte
decoded, err = base64.StdEncoding.DecodeString(encoded)
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/image-proxy.go
Expand Up @@ -32,19 +32,21 @@ func imgProxyHandler(conf *config.Config) http.Handler {
start := time.Now()

img := strings.TrimPrefix(r.URL.Path, "/img")
_, opts, src, err := decodeImageProxyPath(img)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
logger := log.With().Str("src", src).Str("opts", opts).Logger()

if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
helper.AddXForwardHeader(&r.Header, host)
}
logger.Debug().Msg("getting image via proxy")
asset, resp, err := down.Get(r.Context(), conf.Image.ProxyURL+img, &r.Header)
if err != nil {
log.Info().Err(err).Dur("took", time.Since(start)).Msg("unable to get image via proxy")
logger.Info().Err(err).Dur("took", time.Since(start)).Msg("unable to get image via proxy")
// Redirect if image proxy failed
if decoded, err := decodeImageProxyPath(img); err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
} else {
http.Redirect(w, r, strings.Replace(decoded, "http://", "https://", 1), http.StatusTemporaryRedirect)
}
http.Redirect(w, r, strings.Replace(src, "http://", "https://", 1), http.StatusTemporaryRedirect)
return
}

Expand All @@ -57,6 +59,6 @@ func imgProxyHandler(conf *config.Config) http.Handler {
w.WriteHeader(http.StatusOK)
helper.AddCacheHeader(&header, constant.CacheMaxAge)
asset.Write(w, header)
log.Info().Str("name", asset.Name).Dur("took", time.Since(start)).Msg("got image via proxy")
logger.Info().Dur("took", time.Since(start)).Msg("got image via proxy")
})
}
34 changes: 17 additions & 17 deletions pkg/db/test/category_test.go
Expand Up @@ -3,14 +3,14 @@ package dbtest
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ncarlier/readflow/pkg/model"
)

func assertCategoryExists(t *testing.T, uid uint, title, notif string) *model.Category {
category, err := testDB.GetCategoryByUserAndTitle(uid, title)
assert.Nil(t, err)
require.Nil(t, err)
if category != nil {
return category
}
Expand All @@ -20,10 +20,10 @@ func assertCategoryExists(t *testing.T, uid uint, title, notif string) *model.Ca
}

category, err = testDB.CreateCategoryForUser(uid, createForm)
assert.Nil(t, err)
assert.NotNil(t, category)
assert.NotNil(t, category.ID)
assert.Equal(t, title, category.Title)
require.Nil(t, err)
require.NotNil(t, category)
require.NotNil(t, category.ID)
require.Equal(t, title, category.Title)
return category
}
func TestCreateAndUpdateCategory(t *testing.T) {
Expand All @@ -42,15 +42,15 @@ func TestCreateAndUpdateCategory(t *testing.T) {
Title: &title,
}
category, err := testDB.UpdateCategoryForUser(uid, update)
assert.Nil(t, err)
assert.NotNil(t, category)
assert.NotNil(t, category.ID)
assert.Equal(t, title, category.Title)
require.Nil(t, err)
require.NotNil(t, category)
require.NotNil(t, category.ID)
require.Equal(t, title, category.Title)

// Count categories of test user
nb, err := testDB.CountCategoriesByUser(uid)
assert.Nil(t, err)
assert.Positive(t, nb)
require.Nil(t, err)
require.Positive(t, nb)
}

func TestDeleteCategory(t *testing.T) {
Expand All @@ -63,13 +63,13 @@ func TestDeleteCategory(t *testing.T) {
category := assertCategoryExists(t, uid, title, "none")

categories, err := testDB.GetCategoriesByUser(uid)
assert.Nil(t, err)
assert.Positive(t, len(categories), "categories should not be empty")
require.Nil(t, err)
require.Positive(t, len(categories), "categories should not be empty")

err = testDB.DeleteCategoryByUser(uid, *category.ID)
assert.Nil(t, err)
require.Nil(t, err)

category, err = testDB.GetCategoryByUserAndTitle(uid, title)
assert.Nil(t, err)
assert.Nil(t, category)
require.Nil(t, err)
require.Nil(t, category)
}

0 comments on commit 489126a

Please sign in to comment.