minor refactor
This commit is contained in:
parent
faa413c9b1
commit
1662ff226b
@ -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)
|
||||
}
|
||||
|
@ -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}
|
||||
|
@ -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)
|
||||
}
|
||||
|
41
main.go
41
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
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user