minor refactor

This commit is contained in:
Balakrishnan Balasubramanian 2023-09-01 15:13:54 -04:00
parent faa413c9b1
commit 1662ff226b
5 changed files with 31 additions and 17 deletions

View File

@ -51,6 +51,6 @@ func main() {
fmt.Println(err) fmt.Println(err)
out, err := os.Create("./collage.jpg") out, err := os.Create("./collage.jpg")
fmt.Println(err) fmt.Println(err)
err = collage.Make(req, os.DirFS("."), out) err = collage.Make(&req, os.DirFS("."), out)
fmt.Println(err) fmt.Println(err)
} }

View File

@ -48,7 +48,7 @@ type Request struct {
Photos []Photo `json:"photos"` 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)) rec := image.Rect(0, 0, int(req.Aspect.Width), int(req.Aspect.Height))
canvas := image.NewRGBA64(rec) canvas := image.NewRGBA64(rec)
white := color.RGBA{255, 255, 255, 255} white := color.RGBA{255, 255, 255, 255}

View File

@ -67,7 +67,7 @@ func TestMake(t *testing.T) {
t.Fatalf("failed to create ouput image file %v", err) t.Fatalf("failed to create ouput image file %v", err)
} }
err = Make(req, testDataFS, out) err = Make(&req, testDataFS, out)
if err != nil { if err != nil {
t.Fatalf("failed to make collage %v", err) t.Fatalf("failed to make collage %v", err)
} }

33
main.go
View File

@ -23,10 +23,12 @@ var (
imagesDir string imagesDir string
collageDir string collageDir string
localAssets bool localAssets bool
) collageNameGen *nameGen
imagesDirFs fs.FS
// go:embed web/* // go:embed web/*
var webFS embed.FS webFS embed.FS
)
func main() { func main() {
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") 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.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing")
flag.Parse() flag.Parse()
nameGen := NewNameGen()
imagesDirFs := os.DirFS(imagesDir) collageNameGen = NewNameGen()
imagesDirFs = os.DirFS(imagesDir)
imagesURLPath := "images" imagesURLPath := "images"
collagesPath := "collages" collagesPath := "collages"
@ -67,19 +70,18 @@ func main() {
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
slog.Error("failed to read request body", "error", err) slog.Error("failed to read request body", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return return
} }
if err := json.Unmarshal(body, &collageReq); err != nil { if err := json.Unmarshal(body, &collageReq); err != nil {
slog.Error("failed to unmarshal json request", "error", err) slog.Error("failed to unmarshal json request", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return return
} }
collageFile := fmt.Sprintf("collage-%s.jpg", nameGen.Next()) collageFile, err := MakeCollage(&collageReq)
out, err := os.Create(path.Join(collageDir, collageFile))
if err != nil { 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) slog.Error("failed to make collage", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return return
} }
w.Write([]byte(collageFile)) w.Write([]byte(collageFile))
@ -116,4 +118,17 @@ func (n *nameGen) Next() string {
return fmt.Sprintf("%s-%d", n.prefix, n.counter.Add(1)) 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 // curl -D - --json @req.json http://localhost:8767/make-collage

View File

@ -6,5 +6,4 @@ func TestNameGen(t *testing.T) {
g := NewNameGen() g := NewNameGen()
t.Logf("next id: %s", g.Next()) t.Logf("next id: %s", g.Next())
t.Logf("next id: %s", g.Next()) t.Logf("next id: %s", g.Next())
t.Fail()
} }