Files
s3/weed/admin/static_handler.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

117 lines
2.9 KiB
Go

package admin
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"io"
"io/fs"
"net/http"
"path"
"strconv"
"strings"
"sync"
"time"
)
// Assets only change with the binary; the ETag revalidates across upgrades.
const staticCacheControl = "public, max-age=3600"
type staticAsset struct {
name string // base name, drives Content-Type in http.ServeContent
gz []byte
etag string
etagGz string
}
// identity returns the uncompressed bytes.
func (a *staticAsset) identity() ([]byte, error) {
zr, err := gzip.NewReader(bytes.NewReader(a.gz))
if err != nil {
return nil, err
}
defer zr.Close()
return io.ReadAll(zr)
}
var staticAssets = sync.OnceValue(func() map[string]*staticAsset {
assets := make(map[string]*staticAsset)
err := fs.WalkDir(staticGzFS, "static_gz", func(p string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
gz, err := fs.ReadFile(staticGzFS, p)
if err != nil {
return err
}
key := strings.TrimSuffix(strings.TrimPrefix(p, "static_gz/"), ".gz")
sum := sha256.Sum256(gz)
hash := hex.EncodeToString(sum[:8])
assets[key] = &staticAsset{
name: path.Base(key),
gz: gz,
etag: `"` + hash + `"`,
etagGz: `"` + hash + `-gz"`,
}
return nil
})
if err != nil {
panic("walk embedded static assets: " + err.Error())
}
return assets
})
// StaticHandler serves the embedded admin static assets, which are gzipped
// at generation time. Gzip-capable clients get the compressed bytes as-is;
// others get them decompressed on the fly.
func StaticHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(path.Clean("/"+r.URL.Path), "/")
asset, ok := staticAssets()[key]
if !ok {
http.NotFound(w, r)
return
}
h := w.Header()
h.Set("Cache-Control", staticCacheControl)
h.Add("Vary", "Accept-Encoding")
if acceptsGzip(r) {
h.Set("Content-Encoding", "gzip")
h.Set("ETag", asset.etagGz)
// ServeContent skips Content-Length when Content-Encoding is set
if r.Header.Get("Range") == "" {
h.Set("Content-Length", strconv.Itoa(len(asset.gz)))
}
http.ServeContent(w, r, asset.name, time.Time{}, bytes.NewReader(asset.gz))
return
}
data, err := asset.identity()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
h.Set("ETag", asset.etag)
http.ServeContent(w, r, asset.name, time.Time{}, bytes.NewReader(data))
})
}
func acceptsGzip(r *http.Request) bool {
for _, part := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
token, attr, hasAttr := strings.Cut(strings.TrimSpace(part), ";")
if strings.TrimSpace(token) != "gzip" {
continue
}
if !hasAttr {
return true
}
q, ok := strings.CutPrefix(strings.TrimSpace(attr), "q=")
if !ok {
return true
}
v, err := strconv.ParseFloat(q, 64)
return err == nil && v > 0
}
return false
}