From 985c11896fa3effe98c2c3f8601a9ee2d3ac22b0 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Wed, 11 Mar 2020 14:18:25 +0100 Subject: [PATCH] refactoring: implement review feedback --- pkg/flagset/flagset.go | 6 ++--- pkg/service/v0/service.go | 17 +++++++------- pkg/thumbnails/imgsource/imgsource.go | 32 +++++---------------------- pkg/thumbnails/imgsource/webdav.go | 13 +++++++++-- 4 files changed, 27 insertions(+), 41 deletions(-) diff --git a/pkg/flagset/flagset.go b/pkg/flagset/flagset.go index 75af98d5ba..8512b2d77c 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -42,7 +42,7 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ Name: "debug-addr", - Value: "0.0.0.0:9114", + Value: "0.0.0.0:9189", Usage: "Address to debug endpoint", EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"}, Destination: &cfg.Debug.Addr, @@ -89,7 +89,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "debug-addr", - Value: "0.0.0.0:9194", + Value: "0.0.0.0:9189", Usage: "Address to bind debug server", EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"}, Destination: &cfg.Debug.Addr, @@ -115,7 +115,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "http-addr", - Value: "0.0.0.0:9190", + Value: "0.0.0.0:9185", Usage: "Address to bind http server", EnvVars: []string{"THUMBNAILS_HTTP_ADDR"}, Destination: &cfg.HTTP.Addr, diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 8740f5acd3..28b3052e84 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -1,6 +1,7 @@ package svc import ( + "context" "net/http" "strconv" @@ -24,7 +25,7 @@ func NewService(opts ...Option) Service { m := chi.NewMux() m.Use(options.Middleware...) - svc := Thumbnails{ + svc := Thumbnail{ config: options.Config, mux: m, manager: thumbnails.SimpleManager{ @@ -40,8 +41,8 @@ func NewService(opts ...Option) Service { return svc } -// Thumbnails defines implements the business logic for Service. -type Thumbnails struct { +// Thumbnail implements the business logic for Service. +type Thumbnail struct { config *config.Config mux *chi.Mux manager thumbnails.Manager @@ -49,12 +50,12 @@ type Thumbnails struct { } // ServeHTTP implements the Service interface. -func (g Thumbnails) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (g Thumbnail) ServeHTTP(w http.ResponseWriter, r *http.Request) { g.mux.ServeHTTP(w, r) } // Thumbnails provides the endpoint to retrieve a thumbnail for an image -func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) { +func (g Thumbnail) Thumbnails(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() width, _ := strconv.Atoi(query.Get("width")) height, _ := strconv.Atoi(query.Get("height")) @@ -84,11 +85,9 @@ func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) { } auth := r.Header.Get("Authorization") - - sCtx := imgsource.NewContext() - sCtx.Set(imgsource.WebDavAuth, auth) + sCtx := context.WithValue(r.Context(), imgsource.WebDavAuth, auth) // TODO: clean up error handling - img, err := g.source.Get(ctx.ImagePath, sCtx) + img, err := g.source.Get(sCtx, ctx.ImagePath) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) diff --git a/pkg/thumbnails/imgsource/imgsource.go b/pkg/thumbnails/imgsource/imgsource.go index 6697c9bdc0..030a966d0e 100644 --- a/pkg/thumbnails/imgsource/imgsource.go +++ b/pkg/thumbnails/imgsource/imgsource.go @@ -1,33 +1,11 @@ package imgsource -import "image" +import ( + "context" + "image" +) // Source defines the interface for image sources type Source interface { - Get(path string, ctx SourceContext) (image.Image, error) -} - -// NewContext creates a new SourceContext instance -func NewContext() SourceContext { - return SourceContext{ - m: make(map[string]interface{}), - } -} - -// SourceContext is used to pass source specific parameters -type SourceContext struct { - m map[string]interface{} -} - -// GetString tries to cast the value to a string -func (s SourceContext) GetString(key string) string { - if s, ok := s.m[key].(string); ok { - return s - } - return "" -} - -// Set sets a value -func (s SourceContext) Set(key string, val interface{}) { - s.m[key] = val + Get(ctx context.Context, path string) (image.Image, error) } diff --git a/pkg/thumbnails/imgsource/webdav.go b/pkg/thumbnails/imgsource/webdav.go index a6d8fdb50f..ecb3832e12 100644 --- a/pkg/thumbnails/imgsource/webdav.go +++ b/pkg/thumbnails/imgsource/webdav.go @@ -1,6 +1,7 @@ package imgsource import ( + "context" "fmt" "image" "net/http" @@ -28,7 +29,7 @@ const ( ) // Get downloads the file from a webdav service -func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) { +func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) { u, _ := url.Parse(s.baseURL) u.Path = path.Join(u.Path, file) req, err := http.NewRequest(http.MethodGet, u.String(), nil) @@ -36,7 +37,10 @@ func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) { return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error()) } - auth := ctx.GetString(WebDavAuth) + auth, ok := authorization(ctx) + if !ok { + return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file) + } req.Header.Add("Authorization", auth) client := &http.Client{} @@ -55,3 +59,8 @@ func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) { } return img, nil } + +func authorization(ctx context.Context) (string, bool) { + auth, ok := ctx.Value(WebDavAuth).(string) + return auth, ok +}