package main import ( "bytes" "context" "embed" "encoding/json" "flag" "fmt" "io" "io/fs" "net/http" "net/url" "os" "path" "regexp" "time" "log/slog" "go.balki.me/anyhttp" "go.balki.me/anyhttp/idle" "go.balki.me/collage-maker/collage" ) var ( collageDir string photosDir string devMode bool collageNameGen *nameGen imagesDirFs fs.FS listenAddr string photoPrismURL *url.URL photoPrismToken string //go:embed web webFS embed.FS ) func main() { var ppURL string flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") flag.StringVar(&photosDir, "photos-dir", "photos", "Cache directory for downloaded photos") 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(&ppURL, "pp-url", "", "Base url for photoprism") flag.StringVar(&photoPrismToken, "pp-token", "", "API token for photoprism") flag.Parse() photoPrismURL = func() *url.URL { photoPrismURL, err := url.Parse(ppURL) if err != nil { panic(err) } return photoPrismURL }() collageNameGen = NewNameGen() imagesDirFs = os.DirFS(photosDir) collagesPath := "collages" photosPath := "photos" addFileServer := func(path, dir string) { httpFileServer := http.FileServer(http.Dir(dir)) http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer)) } addFileServer(collagesPath, collageDir) addFileServer(photosPath, photosDir) 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]*