package main import ( "bytes" "context" "crypto/rand" "embed" "encoding/json" "flag" "fmt" "io" "io/fs" "net/http" "os" "path" "regexp" "sync/atomic" "time" "log/slog" "go.balki.me/anyhttp" "go.balki.me/anyhttp/idle" "go.balki.me/collage-maker/collage" ) var ( imagesDir string collageDir string localAssets bool collageNameGen *nameGen imagesDirFs fs.FS listenAddr string //go:embed web webFS embed.FS ) 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.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() collageNameGen = NewNameGen() imagesDirFs = os.DirFS(imagesDir) imagesURLPath := "images" collagesPath := "collages" addFileServer := func(path, dir string) { httpFileServer := http.FileServer(http.Dir(dir)) http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer)) } addFileServer(imagesURLPath, imagesDir) addFileServer(collagesPath, collageDir) if localAssets { httpFileServer := http.FileServer(http.Dir("web")) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Cache-Control", "no-cache") httpFileServer.ServeHTTP(w, r) }) } else { indexModTime := time.Now() indexHTML := func() io.ReadSeeker { indexHTMLContent, err := webFS.ReadFile("web/index.html") if err != nil { panic(err) } devOnlyRegex := regexp.MustCompile("\n[^\n]*