ghrelease

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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

type AssetMatcher func(name string) (version string, ok bool)

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.

func (*Refresher) Register

func (m *Refresher) Register(ctx context.Context) error

Register starts the refresh loop anchored to ctx and returns immediately.

type Release

type Release struct {
	Version string
	URL     string
}

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

func (r Release) AtLeast(minimum string) bool

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.

func (Release) Resolved

func (r Release) Resolved() bool

Resolved reports whether r carries a concrete asset (as opposed to the releases-page fallback).

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver caches the latest release. Safe for concurrent use.

func New

func New(opts Options) (*Resolver, error)

New builds a Resolver from opts.

func (*Resolver) Handler

func (r *Resolver) Handler() http.Handler

Handler answers with a 302 to the latest asset (or the releases page), with a short shared Cache-Control.

func (*Resolver) Latest

func (r *Resolver) Latest(ctx context.Context) Release

Latest returns the cached release, refreshing it first when the cache is older than the TTL and no failed attempt happened within the last minute. It never returns an error: failures degrade to the last good release, then to the fallback page.

func (*Resolver) Peek

func (r *Resolver) Peek() Release

Peek returns the cached release without any network I/O, or the fallback when nothing has been fetched yet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL