Minor refactor

* Move namegen to seperate file
* Simplify Makefile
This commit is contained in:
Balakrishnan Balasubramanian 2023-09-19 22:47:09 -04:00
parent acc9e87f2c
commit 9200bd16db
4 changed files with 48 additions and 42 deletions

View File

@ -1,10 +1,9 @@
# go install github.com/omeid/go-livereload/cmd/livereload
livereload: livereload:
cd web; livereload . | ts go run github.com/omeid/go-livereload/cmd/livereload@latest web | ts
local: devserver:
go run main.go --local-assets --images-dir w.tmp/images --collages-dir w.tmp go run . --dev --images-dir w.tmp/images --collages-dir w.tmp
update_croppie: update_croppie:
curl -Lo web/croppie.min.js https://github.com/Foliotek/Croppie/raw/v2.6.5/croppie.min.js curl -Lo web/croppie.min.js https://github.com/Foliotek/Croppie/raw/v2.6.5/croppie.min.js

36
main.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/rand"
"embed" "embed"
"encoding/json" "encoding/json"
"flag" "flag"
@ -14,7 +13,6 @@ import (
"os" "os"
"path" "path"
"regexp" "regexp"
"sync/atomic"
"time" "time"
"log/slog" "log/slog"
@ -27,7 +25,7 @@ import (
var ( var (
imagesDir string imagesDir string
collageDir string collageDir string
localAssets bool devMode bool
collageNameGen *nameGen collageNameGen *nameGen
imagesDirFs fs.FS imagesDirFs fs.FS
listenAddr string listenAddr string
@ -39,7 +37,7 @@ var (
func main() { func main() {
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages 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.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() flag.Parse()
@ -57,7 +55,7 @@ func main() {
addFileServer(imagesURLPath, imagesDir) addFileServer(imagesURLPath, imagesDir)
addFileServer(collagesPath, collageDir) addFileServer(collagesPath, collageDir)
if localAssets { if devMode {
httpFileServer := http.FileServer(http.Dir("web")) httpFileServer := http.FileServer(http.Dir("web"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-cache") 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) { func MakeCollage(req *collage.Request) (string, error) {
collageFile := fmt.Sprintf("collage-%s.jpg", collageNameGen.Next()) collageFile := fmt.Sprintf("collage-%s.jpg", collageNameGen.Next())
out, err := os.Create(path.Join(collageDir, collageFile)) out, err := os.Create(path.Join(collageDir, collageFile))
if err != nil { if err != nil {
return "", fmt.Errorf("failed to create collage output file, err: %w", err) return "", fmt.Errorf("failed to create collage output file, err: %w", err)
} }
defer out.Close()
if err := collage.Make(req, imagesDirFs, out); err != nil { if err := collage.Make(req, imagesDirFs, out); err != nil {
return "", fmt.Errorf("failed to make collage, err: %w", err) return "", fmt.Errorf("failed to make collage, err: %w", err)
} }

35
namegen.go Normal file
View File

@ -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))
}