2026-05-31 14:51:18 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"image"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"spoolweight/claude"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed index.html
|
|
|
|
|
var indexHTML []byte
|
|
|
|
|
|
2026-05-31 19:33:48 -07:00
|
|
|
// serve runs the mobile putaway web server. Each request gets its own fresh
|
|
|
|
|
// spooldb session; requests are serialized so we never run several headless
|
|
|
|
|
// browsers at once.
|
|
|
|
|
func serve(addr string, auth claude.Auth, dryRun bool) error {
|
2026-05-31 14:51:18 -07:00
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
|
|
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
w.Write(indexHTML)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mux.HandleFunc("POST /process", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
if err := r.ParseMultipartForm(64 << 20); err != nil {
|
|
|
|
|
writeJSON(w, result{Error: "upload too large or malformed"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
file, hdr, err := r.FormFile("photo")
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeJSON(w, result{Error: "no photo in request"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeJSON(w, result{Image: hdr.Filename, Error: "could not read image (try choosing from your library)"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
mu.Lock()
|
2026-05-31 19:33:48 -07:00
|
|
|
res := processImg(img, hdr.Filename, auth, dryRun)
|
2026-05-31 14:51:18 -07:00
|
|
|
mu.Unlock()
|
|
|
|
|
slog.Info("processed upload", "file", hdr.Filename, "spool", res.SpoolID, "dur", time.Since(start))
|
|
|
|
|
|
|
|
|
|
writeJSON(w, res)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
slog.Info("putaway server listening", "addr", addr, "dry_run", dryRun)
|
|
|
|
|
return http.ListenAndServe(addr, mux)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, v any) {
|
|
|
|
|
json.NewEncoder(w).Encode(v)
|
|
|
|
|
}
|