2023-08-01 19:16:46 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-13 14:38:42 -04:00
|
|
|
"bytes"
|
2023-09-11 21:16:10 -04:00
|
|
|
"context"
|
2023-08-31 20:27:43 -04:00
|
|
|
"embed"
|
2023-08-28 23:35:32 -04:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2023-08-01 19:16:46 -04:00
|
|
|
"fmt"
|
2023-08-28 23:35:32 -04:00
|
|
|
"io"
|
2023-08-31 20:27:43 -04:00
|
|
|
"io/fs"
|
2023-08-28 23:35:32 -04:00
|
|
|
"net/http"
|
2023-08-01 19:16:46 -04:00
|
|
|
"os"
|
2023-08-28 23:35:32 -04:00
|
|
|
"path"
|
2023-09-13 14:38:42 -04:00
|
|
|
"regexp"
|
2023-08-28 23:35:32 -04:00
|
|
|
"time"
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
"log/slog"
|
|
|
|
|
2023-09-11 21:16:10 -04:00
|
|
|
"go.balki.me/anyhttp"
|
|
|
|
"go.balki.me/anyhttp/idle"
|
2023-08-28 23:35:32 -04:00
|
|
|
"go.balki.me/collage-maker/collage"
|
2023-08-01 19:16:46 -04:00
|
|
|
)
|
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
var (
|
2023-09-01 15:13:54 -04:00
|
|
|
imagesDir string
|
|
|
|
collageDir string
|
2023-09-19 22:47:09 -04:00
|
|
|
devMode bool
|
2023-09-01 15:13:54 -04:00
|
|
|
collageNameGen *nameGen
|
|
|
|
imagesDirFs fs.FS
|
2023-09-11 21:16:10 -04:00
|
|
|
listenAddr string
|
2023-09-01 15:13:54 -04:00
|
|
|
|
2023-09-13 14:38:42 -04:00
|
|
|
//go:embed web
|
2023-09-01 15:13:54 -04:00
|
|
|
webFS embed.FS
|
2023-08-28 23:35:32 -04:00
|
|
|
)
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2024-08-01 22:33:29 -04:00
|
|
|
const (
|
|
|
|
kb = 1024
|
|
|
|
mb = 1024 * kb
|
|
|
|
)
|
|
|
|
|
2023-08-01 19:16:46 -04:00
|
|
|
func main() {
|
2023-08-28 23:35:32 -04:00
|
|
|
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
|
|
|
|
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
|
2023-09-19 22:47:09 -04:00
|
|
|
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
|
2023-09-11 21:16:10 -04:00
|
|
|
flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
|
2023-08-31 20:27:43 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
flag.Parse()
|
2023-09-01 15:13:54 -04:00
|
|
|
|
|
|
|
collageNameGen = NewNameGen()
|
|
|
|
imagesDirFs = os.DirFS(imagesDir)
|
2023-08-31 20:27:43 -04:00
|
|
|
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)
|
|
|
|
|
2023-09-19 22:47:09 -04:00
|
|
|
if devMode {
|
2023-08-31 20:27:43 -04:00
|
|
|
httpFileServer := http.FileServer(http.Dir("web"))
|
2023-09-05 09:04:54 -04:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Cache-Control", "no-cache")
|
|
|
|
httpFileServer.ServeHTTP(w, r)
|
|
|
|
})
|
2023-08-31 20:27:43 -04:00
|
|
|
} else {
|
2023-09-13 14:38:42 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2023-08-31 20:27:43 -04:00
|
|
|
}
|
2023-08-01 19:16:46 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
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)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(body, &collageReq); err != nil {
|
|
|
|
slog.Error("failed to unmarshal json request", "error", err)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
2023-09-01 15:13:54 -04:00
|
|
|
collageFile, err := MakeCollage(&collageReq)
|
2023-08-28 23:35:32 -04:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to make collage", "error", err)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
2023-09-11 21:16:10 -04:00
|
|
|
if _, err := w.Write([]byte(collageFile)); err != nil {
|
|
|
|
slog.Error("Failed to write collageFile", "error", err)
|
|
|
|
}
|
2023-08-03 19:33:05 -04:00
|
|
|
})
|
2024-08-01 22:33:29 -04:00
|
|
|
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)
|
|
|
|
})
|
2023-09-11 21:16:10 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-09-13 14:38:42 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
|
|
|
|
defer cancel()
|
2023-09-11 21:16:10 -04:00
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
|
|
|
slog.Error("http server Shutdown failed", "error", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
<-done
|
2023-08-03 19:33:05 -04:00
|
|
|
}
|
2023-08-28 23:35:32 -04:00
|
|
|
}
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2023-09-01 15:13:54 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-09-19 22:47:09 -04:00
|
|
|
defer out.Close()
|
2023-09-01 15:13:54 -04:00
|
|
|
if err := collage.Make(req, imagesDirFs, out); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to make collage, err: %w", err)
|
|
|
|
}
|
|
|
|
return collageFile, nil
|
|
|
|
}
|