package main import ( "bytes" "context" "embed" "encoding/json" "flag" "fmt" "io" "io/fs" "net/http" "os" "path" "regexp" "time" "log/slog" "go.balki.me/anyhttp" "go.balki.me/anyhttp/idle" "go.balki.me/collage-maker/collage" ) var ( imagesDir string collageDir string devMode bool collageNameGen *nameGen imagesDirFs fs.FS listenAddr string //go:embed web webFS embed.FS ) const ( kb = 1024 mb = 1024 * kb ) func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") 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() 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 devMode { 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]*