From 9200bd16db39b920619f55b6ee2370bc25222594 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 19 Sep 2023 22:47:09 -0400 Subject: [PATCH] Minor refactor * Move namegen to seperate file * Simplify Makefile --- Makefile | 7 +++---- main.go | 36 ++++-------------------------------- namegen.go | 35 +++++++++++++++++++++++++++++++++++ web/index.html | 12 ++++++------ 4 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 namegen.go diff --git a/Makefile b/Makefile index b20f4e0..be35843 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,9 @@ -# go install github.com/omeid/go-livereload/cmd/livereload livereload: - cd web; livereload . | ts + go run github.com/omeid/go-livereload/cmd/livereload@latest web | ts -local: - go run main.go --local-assets --images-dir w.tmp/images --collages-dir w.tmp +devserver: + go run . --dev --images-dir w.tmp/images --collages-dir w.tmp update_croppie: curl -Lo web/croppie.min.js https://github.com/Foliotek/Croppie/raw/v2.6.5/croppie.min.js diff --git a/main.go b/main.go index 5925a39..7442ec0 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bytes" "context" - "crypto/rand" "embed" "encoding/json" "flag" @@ -14,7 +13,6 @@ import ( "os" "path" "regexp" - "sync/atomic" "time" "log/slog" @@ -27,7 +25,7 @@ import ( var ( imagesDir string collageDir string - localAssets bool + devMode bool collageNameGen *nameGen imagesDirFs fs.FS listenAddr string @@ -39,7 +37,7 @@ var ( func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") - flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets during development") + flag.BoolVar(&devMode, "dev", false, "Serve local assets during development") flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax") flag.Parse() @@ -57,7 +55,7 @@ func main() { addFileServer(imagesURLPath, imagesDir) addFileServer(collagesPath, collageDir) - if localAssets { + if devMode { httpFileServer := http.FileServer(http.Dir("web")) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Cache-Control", "no-cache") @@ -131,39 +129,13 @@ func main() { } } -type nameGen struct { - prefix string - counter atomic.Uint64 -} - -func NewNameGen() *nameGen { - currentTime := time.Now().Unix() - randBytes := make([]byte, 8) - _, err := rand.Read(randBytes) - if err != nil { - panic(err) - } - alpha := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ") - uniqRunID := "" - for _, b := range randBytes { - uniqRunID += string(alpha[int(b)%len(alpha)]) - } - return &nameGen{ - prefix: fmt.Sprintf("%d-%s", currentTime, uniqRunID), - counter: atomic.Uint64{}, - } -} - -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) } + defer out.Close() if err := collage.Make(req, imagesDirFs, out); err != nil { return "", fmt.Errorf("failed to make collage, err: %w", err) } diff --git a/namegen.go b/namegen.go new file mode 100644 index 0000000..c881676 --- /dev/null +++ b/namegen.go @@ -0,0 +1,35 @@ +package main + +import ( + "crypto/rand" + "fmt" + "sync/atomic" + "time" +) + +type nameGen struct { + prefix string + counter atomic.Uint64 +} + +func NewNameGen() *nameGen { + currentTime := time.Now().Unix() + randBytes := make([]byte, 8) + _, err := rand.Read(randBytes) + if err != nil { + panic(err) + } + alpha := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + uniqRunID := "" + for _, b := range randBytes { + uniqRunID += string(alpha[int(b)%len(alpha)]) + } + return &nameGen{ + prefix: fmt.Sprintf("%d-%s", currentTime, uniqRunID), + counter: atomic.Uint64{}, + } +} + +func (n *nameGen) Next() string { + return fmt.Sprintf("%s-%d", n.prefix, n.counter.Add(1)) +} diff --git a/web/index.html b/web/index.html index 3253fb0..8a17185 100644 --- a/web/index.html +++ b/web/index.html @@ -16,12 +16,12 @@