collage-maker/main.go

189 lines
4.8 KiB
Go

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]*<!-- DEVONLY[^\n]*")
return bytes.NewReader(devOnlyRegex.ReplaceAllLiteral(indexHTMLContent, nil))
}()
httpFileServer := func() http.Handler {
webrootFs, err := fs.Sub(webFS, "web")
if err != nil {
panic(err)
}
return http.FileServer(http.FS(webrootFs))
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.ServeContent(w, r, "index.html", indexModTime, indexHTML)
} else {
httpFileServer.ServeHTTP(w, r)
}
})
}
http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) {
collageReq := collage.Request{}
body, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("failed to read request body", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := json.Unmarshal(body, &collageReq); err != nil {
slog.Error("failed to unmarshal json request", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
collageFile, err := MakeCollage(&collageReq)
if err != nil {
slog.Error("failed to make collage", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write([]byte(collageFile)); err != nil {
slog.Error("Failed to write collageFile", "error", err)
}
})
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
err := func() error {
err := r.ParseMultipartForm(500 * mb)
if err != nil {
return err
}
files, ok := r.MultipartForm.File["photos"]
if !ok {
return fmt.Errorf("photos not found in request")
}
for i, f := range files {
imgFile, err := f.Open()
if err != nil {
return err
}
defer imgFile.Close()
imagePath := path.Join(imagesDir, fmt.Sprintf("img%d.jpg", i+1))
os.Remove(imagePath)
if err != nil {
return err
}
op, err := os.Create(imagePath)
if err != nil {
return err
}
defer op.Close()
_, err = io.Copy(op, imgFile)
if err != nil {
return err
}
}
return nil
}()
if err != nil {
slog.Error("Image upload failed", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
})
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
if err != nil {
slog.Error("anyhttp Serve failed", "error", err)
}
if addrType == anyhttp.SystemdFD {
if err := idle.Wait(30 * time.Minute); err != nil {
slog.Error("Failed to wait for idler", "error", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
defer cancel()
if err := server.Shutdown(ctx); err != nil {
slog.Error("http server Shutdown failed", "error", err)
}
} else {
<-done
}
}
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)
}
return collageFile, nil
}