From 1662ff226b4af5a205d7d80a1ae809434062d80c Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Fri, 1 Sep 2023 15:13:54 -0400 Subject: [PATCH] minor refactor --- cmd/run.go | 2 +- collage/collage.go | 2 +- collage/collage_test.go | 2 +- main.go | 41 ++++++++++++++++++++++++++++------------- main_test.go | 1 - 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index a805eb7..cbe73d5 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -51,6 +51,6 @@ func main() { fmt.Println(err) out, err := os.Create("./collage.jpg") fmt.Println(err) - err = collage.Make(req, os.DirFS("."), out) + err = collage.Make(&req, os.DirFS("."), out) fmt.Println(err) } diff --git a/collage/collage.go b/collage/collage.go index 5197790..1f5420e 100644 --- a/collage/collage.go +++ b/collage/collage.go @@ -48,7 +48,7 @@ type Request struct { Photos []Photo `json:"photos"` } -func Make(req Request, source fs.FS, output io.Writer) error { +func Make(req *Request, source fs.FS, output io.Writer) error { rec := image.Rect(0, 0, int(req.Aspect.Width), int(req.Aspect.Height)) canvas := image.NewRGBA64(rec) white := color.RGBA{255, 255, 255, 255} diff --git a/collage/collage_test.go b/collage/collage_test.go index 5971111..ef4a3c0 100644 --- a/collage/collage_test.go +++ b/collage/collage_test.go @@ -67,7 +67,7 @@ func TestMake(t *testing.T) { t.Fatalf("failed to create ouput image file %v", err) } - err = Make(req, testDataFS, out) + err = Make(&req, testDataFS, out) if err != nil { t.Fatalf("failed to make collage %v", err) } diff --git a/main.go b/main.go index 6acc19e..ab40a46 100644 --- a/main.go +++ b/main.go @@ -20,13 +20,15 @@ import ( ) var ( - imagesDir string - collageDir string - localAssets bool -) + imagesDir string + collageDir string + localAssets bool + collageNameGen *nameGen + imagesDirFs fs.FS -//go:embed web/* -var webFS embed.FS + // go:embed web/* + webFS embed.FS +) func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") @@ -34,8 +36,9 @@ func main() { flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing") flag.Parse() - nameGen := NewNameGen() - imagesDirFs := os.DirFS(imagesDir) + + collageNameGen = NewNameGen() + imagesDirFs = os.DirFS(imagesDir) imagesURLPath := "images" collagesPath := "collages" @@ -67,19 +70,18 @@ func main() { body, err := io.ReadAll(r.Body) if err != nil { slog.Error("failed to read request body", "error", err) + w.WriteHeader(http.StatusInternalServerError) return } if err := json.Unmarshal(body, &collageReq); err != nil { slog.Error("failed to unmarshal json request", "error", err) + w.WriteHeader(http.StatusInternalServerError) return } - collageFile := fmt.Sprintf("collage-%s.jpg", nameGen.Next()) - out, err := os.Create(path.Join(collageDir, collageFile)) + collageFile, err := MakeCollage(&collageReq) if err != nil { - slog.Error("failed to create collage output file", "error", err) - } - if err := collage.Make(collageReq, imagesDirFs, out); err != nil { slog.Error("failed to make collage", "error", err) + w.WriteHeader(http.StatusInternalServerError) return } w.Write([]byte(collageFile)) @@ -116,4 +118,17 @@ func (n *nameGen) Next() string { return fmt.Sprintf("%s-%d", n.prefix, n.counter.Add(1)) } +func MakeCollage(req *collage.Request) (string, error) { + collageFile := fmt.Sprintf("collage-%s.jpg", collageNameGen.Next()) + out, err := os.Create(path.Join(collageDir, collageFile)) + if err != nil { + return "", fmt.Errorf("failed to create collage output file, err: %w", err) + // slog.Error("failed to create collage output file", "error", err) + } + if err := collage.Make(req, imagesDirFs, out); err != nil { + return "", fmt.Errorf("failed to make collage, err: %w", err) + } + return collageFile, nil +} + // curl -D - --json @req.json http://localhost:8767/make-collage diff --git a/main_test.go b/main_test.go index 34e9f73..40a8d5c 100644 --- a/main_test.go +++ b/main_test.go @@ -6,5 +6,4 @@ func TestNameGen(t *testing.T) { g := NewNameGen() t.Logf("next id: %s", g.Next()) t.Logf("next id: %s", g.Next()) - t.Fail() }