Documentation
¶
Overview ¶
Package ghrelease resolves the newest downloadable asset of a GitHub repository from the Releases API, so a landing page's download button follows every new release without a redeploy.
The Resolver keeps the last good answer in memory. Callers on the hot path (template helpers) read it with Peek, which never touches the network; the download handler calls Latest, which refreshes the cache once it is older than the TTL. When GitHub is unreachable, rate-limited or returns a release without a matching asset, the resolver keeps serving the last good release, and before the first successful fetch it answers with the repository's releases/latest page, which always lists the newest assets.
Wiring:
resolver, err := ghrelease.New(ghrelease.Options{
Repo: "owner/app",
Asset: ghrelease.PrefixSuffix("App-", ".dmg"),
})
mux.Handle("GET /download", resolver.Handler())
modules = append(modules, &ghrelease.Refresher{Resolver: resolver})
Index ¶
Constants ¶
const ( // DefaultTTL is how long a fetched release is served before the // next Latest call refreshes it. DefaultTTL = 10 * time.Minute // DefaultUserAgent identifies the client to the GitHub API when // no service was booted (see Options.UserAgent). DefaultUserAgent = "hblabs-kit-ghrelease/1 (+https://hblabs.co)" // DefaultCacheControl is set on the Handler's redirect: short, so // CDNs and browsers do not pin an old version for long. DefaultCacheControl = "public, max-age=300" )
Defaults applied by New.
Variables ¶
var ( ErrNoAsset = errors.New("ghrelease: Options.Asset is required") ErrNoRepo = errors.New("ghrelease: Options.Repo is required unless APIURL and FallbackURL are set") )
Errors returned by New.
Functions ¶
This section is empty.
Types ¶
type AssetMatcher ¶
AssetMatcher reports whether an asset name is the download and, if so, the version it carries (shown on the page, e.g. "1.2.3").
func PrefixSuffix ¶
func PrefixSuffix(prefix, suffix string) AssetMatcher
PrefixSuffix matches assets named <prefix><version><suffix>, such as "App-1.2.3.dmg" for PrefixSuffix("App-", ".dmg"). The version part must not be empty.
type Options ¶
type Options struct {
// Repo is "owner/name". It derives APIURL and FallbackURL when
// those are empty.
Repo string
// Asset picks the download among the release's assets. Required.
Asset AssetMatcher
// APIURL overrides the latest-release API endpoint.
APIURL string
// FallbackURL overrides the page served before the first
// successful fetch.
FallbackURL string
// TTL defaults to DefaultTTL.
TTL time.Duration
// Client defaults to http.DefaultClient.
Client *http.Client
// UserAgent defaults to "HB Labs <service>/<version>
// (+https://hblabs.co)" from system.ServiceName and the build
// version ("dev" when unstamped), or DefaultUserAgent when
// system.Boot has not run.
UserAgent string
// CacheControl for the Handler's redirect; defaults to
// DefaultCacheControl.
CacheControl string
}
Options configures a Resolver.
type Refresher ¶
type Refresher struct {
Resolver *Resolver
// Interval overrides the refresh period (defaults to the
// resolver's TTL).
Interval time.Duration
}
Refresher is a system.Module that warms the cache at boot and refreshes it every interval, so Peek (used while rendering pages) stays current without visitors waiting on GitHub.
type Release ¶
Release is one resolved download. Version is empty when the resolver has never fetched successfully; URL then points at the releases/latest page instead of an asset.
func (Release) AtLeast ¶
AtLeast reports whether r.Version is at least minimum. Both are dotted numeric versions ("1.2.3"); a leading "v" and a pre-release suffix after '-' are ignored, missing components count as 0. Unresolved or unparseable versions answer false, so callers fall back to neutral copy.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver caches the latest release. Safe for concurrent use.
func (*Resolver) Handler ¶
Handler answers with a 302 to the latest asset (or the releases page), with a short shared Cache-Control.