feat(statuspage): refresh pages by checksum
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m46s
ci / test (pull_request) Successful in 14m46s
ci / build (pull_request) Successful in 18m49s
ci / image (pull_request) Successful in 3m24s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m46s
ci / test (pull_request) Successful in 14m46s
ci / build (pull_request) Successful in 18m49s
ci / image (pull_request) Successful in 3m24s
Each refresh sent the whole page again even when nothing had changed. Status page servers now send the checksum of their copy (If-None-Match) and the API answers 304 when the page, leaving out when it was generated, is unchanged; the server then only marks its copy current. The checksum is kept on disk with the page, so it survives restarts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
4e9a198b2f
commit
fcf615dce7
6 files changed
+120
-8
No files matched your search
@@ -125,6 +125,11 @@ func (c *Client) WithHTTPClient(hc *http.Client) *Client {
|
||||
func (c *Client) Instance() uuid.UUID { return c.instance }
|
||||
|
||||
func (c *Client) do(ctx context.Context, method, path string, in, out any) error {
|
||||
return c.doWith(ctx, method, path, nil, in, out)
|
||||
}
|
||||
|
||||
// doWith is do with extra request headers.
|
||||
func (c *Client) doWith(ctx context.Context, method, path string, header http.Header, in, out any) error {
|
||||
var body io.Reader
|
||||
if in != nil {
|
||||
b, err := json.Marshal(in)
|
||||
@@ -142,6 +147,9 @@ func (c *Client) do(ctx context.Context, method, path string, in, out any) error
|
||||
req.Header.Set(VersionHeader, fmt.Sprint(Version))
|
||||
req.Header.Set(InstanceHeader, c.instance.String())
|
||||
req.Header.Set(ReleaseHeader, c.release)
|
||||
for k, v := range header {
|
||||
req.Header[k] = v
|
||||
}
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -152,6 +160,8 @@ func (c *Client) do(ctx context.Context, method, path string, in, out any) error
|
||||
return ErrUnauthorized
|
||||
case resp.StatusCode == http.StatusNotFound:
|
||||
return ErrNotFound
|
||||
case resp.StatusCode == http.StatusNotModified:
|
||||
return ErrNotModified
|
||||
case resp.StatusCode >= 300:
|
||||
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
|
||||
return fmt.Errorf("agentapi: %s %s: %d %s", method, path, resp.StatusCode, bytes.TrimSpace(msg))
|
||||
|
||||
@@ -2,6 +2,9 @@ package agentapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -368,16 +371,37 @@ func (c *Client) Pages(ctx context.Context) ([]PageRef, error) {
|
||||
// ErrNotFound means no published page matches.
|
||||
var ErrNotFound = errors.New("agentapi: not found")
|
||||
|
||||
// ErrNotModified means the page still matches the checksum sent.
|
||||
var ErrNotModified = errors.New("agentapi: not modified")
|
||||
|
||||
// Checksum identifies the content of s, leaving out when it was generated.
|
||||
func (s PageSnapshot) Checksum() string {
|
||||
s.GeneratedAt = time.Time{}
|
||||
raw, _ := json.Marshal(s)
|
||||
sum := sha256.Sum256(raw)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// Page fetches the published page for a custom domain host, or for slug if
|
||||
// host is empty.
|
||||
func (c *Client) Page(ctx context.Context, host, slug string) (PageSnapshot, error) {
|
||||
return c.PageIfChanged(ctx, host, slug, "")
|
||||
}
|
||||
|
||||
// PageIfChanged is Page, but answers ErrNotModified instead of sending the
|
||||
// page again when it still has the checksum given.
|
||||
func (c *Client) PageIfChanged(ctx context.Context, host, slug, checksum string) (PageSnapshot, error) {
|
||||
q := url.Values{}
|
||||
if host != "" {
|
||||
q.Set("host", host)
|
||||
} else {
|
||||
q.Set("slug", slug)
|
||||
}
|
||||
var h http.Header
|
||||
if checksum != "" {
|
||||
h = http.Header{"If-None-Match": {`"` + checksum + `"`}}
|
||||
}
|
||||
var p PageSnapshot
|
||||
err := c.do(ctx, http.MethodGet, "/pages?"+q.Encode(), nil, &p)
|
||||
err := c.doWith(ctx, http.MethodGet, "/pages?"+q.Encode(), h, nil, &p)
|
||||
return p, err
|
||||
}
|
||||
Reference in new issue
Block a user