Artifact 7b45ddcb6b9b2ccca04034f3303116fadc66a4eebaa876e004572315d5698f96:
- File
internal/sync/serve_http.go
— part of check-in
[6273a0750d]
at
2026-07-22 23:03:24
on branch trunk
— fix: correct go.mod module path to go-libfossil (#76) (#117)
* fix: correct go.mod module path to go-libfossil (#76)
The module directive still declared github.com/danmestas/libfossil after the repository was renamed to go-libfossil. pkg.go.dev/godoc scraping matches the declared module path against its hosting path, so the stale path broke SDK doc generation.
Rename the module path across all four in-repo modules (root, the two db/driver submodules, observer/otel, and docs/site) and every internal import, plus the release workflow, the doc-generation script, and the docs.
No tag has been published under the go-libfossil path yet, so the cross-module requires cannot resolve from the proxy. Add replace directives pointing the in-repo requires at their local directories so GOWORK=off builds and the CI matrix resolve without a published version. These replaces are ignored by downstream consumers and can be dropped once new-path tags are cut.
* Guard releases against replace directives; regenerate SDK docs
The go-libfossil rename bridges the in-repo modules with `replace` directives. Go refuses a main module that carries them, so a tag cut while they are present breaks `go install .../cmd/libfossil@<tag>` and every `@version` fetch of the driver submodules. Add a Release workflow step that fails the job before anything is published if any tracked go.mod still contains a replace directive, so the bridge is a CI-enforced TODO rather than a silent landmine. The step comment records what unblocks a release: re-tag the submodules with the corrected module paths, bump the root requires, then drop the replace blocks.
Regenerate the SDK reference docs. The previous pass only substituted URLs over the pre-existing generated files, leaving stale signatures (Repo.Timeline was still documented as taking LogOpts rather than TimelineOpts). Note that gomarkdoc must be run outside a git worktree — inside one it silently strips every source link.
Also reword the README rename note, which read as though the import path had not changed, and re-sort the ncruces require block. (user: [email protected], size: 3311) [annotate] [blame] [check-ins using]
package sync import ( "context" "fmt" "io" "log/slog" "net" "net/http" "github.com/danmestas/go-libfossil/internal/repo" "github.com/danmestas/go-libfossil/internal/xfer" ) // maxRequestBodyBytes caps the size of incoming xfer request bodies. // Fossil sync payloads are typically under 1MB; 50MB is generous headroom. const maxRequestBodyBytes = 50 * 1024 * 1024 // ServeHTTP starts an HTTP server that accepts Fossil xfer requests. // Blocks until ctx is cancelled. Stock fossil clone/sync can connect. func ServeHTTP(ctx context.Context, addr string, r *repo.Repo, h HandleFunc) error { if addr == "" { panic("sync.ServeHTTP: addr must not be empty") } if r == nil { panic("sync.ServeHTTP: r must not be nil") } if h == nil { panic("sync.ServeHTTP: h must not be nil") } mux := http.NewServeMux() mux.HandleFunc("/", xferHandler(r, h)) srv := &http.Server{ Handler: mux, } ln, err := net.Listen("tcp", addr) if err != nil { return fmt.Errorf("sync.ServeHTTP: listen: %w", err) } go func() { <-ctx.Done() srv.Close() }() err = srv.Serve(ln) if err == http.ErrServerClosed { return nil } return err } // XferHandler returns an http.HandlerFunc that decodes Fossil xfer requests, // dispatches to the HandleFunc, and encodes the response. Use this to compose // a custom mux when you need additional routes (e.g. /healthz) alongside xfer. func XferHandler(r *repo.Repo, h HandleFunc) http.HandlerFunc { if r == nil { panic("sync.XferHandler: r must not be nil") } if h == nil { panic("sync.XferHandler: h must not be nil") } return xferHandler(r, h) } // xferHandler is the unexported implementation. func xferHandler(r *repo.Repo, h HandleFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { // Fossil sends GET as a server probe — respond with a basic page. if req.Method != http.MethodPost { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "<html><body><h1>Fossil Sync Server</h1></body></html>") return } // Bound request body size to prevent resource exhaustion. req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes) body, err := io.ReadAll(req.Body) if err != nil { http.Error(w, "read body: "+err.Error(), http.StatusBadRequest) return } if len(body) == 0 { // Empty POST — respond with empty xfer message. writeXferResponse(w, &xfer.Message{}) return } msg, err := xfer.Decode(body) if err != nil { slog.Error("serve-http: decode failed", "bytes", len(body), "err", err) http.Error(w, fmt.Sprintf("decode xfer (%d bytes): %v", len(body), err), http.StatusBadRequest) return } resp, err := h(req.Context(), r, msg) if err != nil { http.Error(w, "handler: "+err.Error(), http.StatusInternalServerError) return } writeXferResponse(w, resp) } } // writeXferResponse encodes an xfer message and writes it as the HTTP response. func writeXferResponse(w http.ResponseWriter, msg *xfer.Message) { respBytes, err := msg.Encode() if err != nil { http.Error(w, "encode response: "+err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/x-fossil") if _, err := w.Write(respBytes); err != nil { slog.Error("serve-http: write response failed", "err", err) } }