mapsstyle

package
v0.14.10 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: GPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package mapsstyle is a server-side pull-through cache for MapLibre style assets served by the graywolf-maps Worker. The browser hits /api/maps/style/{path}; on disk-miss, graywolf fetches from upstream once, writes the body to <TileCacheDir>/style/, and serves it. The next request comes off disk — including after a reboot, from a different browser, or from a LAN guest.

No startup network: graywolf does NOT pre-fetch on boot. The first online browser request is the trigger. A new user with internet sees a working map immediately; a never-online user sees a broken map (which is the correct visible failure mode — they have no tiles either). See docs/wiki/system-topology.md for the offline maps story.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RewriteStyleJSON

func RewriteStyleJSON(in []byte, localPrefix string) ([]byte, error)

RewriteStyleJSON rewrites absolute maps.nw5w.com URLs inside a style.json payload to point at the local proxy prefix (e.g. "/api/maps/style"). Rewritten fields:

  • top-level "glyphs" (URL template)
  • top-level "sprite" (URL base)
  • sources.*.url that points at maps.nw5w.com/tiles.json

Other source types (raster-dem on s3.amazonaws.com) are left untouched.

Types

type Cache

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

Cache is a disk-backed pull-through cache for MapLibre style assets. Concurrent reads are safe; in-flight upstream fetches for the same asset are coalesced (stampede protection).

func New

func New(cfg Config) *Cache

New constructs a Cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, rel string) ([]byte, string, error)

Get returns the body + content-type for rel. Tries disk first; on miss, fetches upstream and writes to disk before returning. Returns an error if the path is unsafe or both disk + upstream are unavailable.

Concurrent Gets for the same rel coalesce: the first caller fetches, subsequent callers wait and re-read from disk.

func (*Cache) PrewarmGlyphs

func (c *Cache) PrewarmGlyphs(ctx context.Context) error

PrewarmGlyphs reads the cached style.json from disk, discovers fontstacks via `text-font` layer keys, and fetches every glyph range 0..prewarmMaxRange for each fontstack. Per fontstack, after prewarmStop404 consecutive 404s the walk bails on that stack.

Serial by design: each range is fetched in order so the consecutive-404 counter advances correctly. The total wall-clock is bounded by the caller's context (the map-download piggyback uses a 5-minute deadline). Best-effort — returns the first genuine error, callers log and move on.

Idempotent — files already on disk are skipped.

func (*Cache) SetPrewarmLimits

func (c *Cache) SetPrewarmLimits(maxRange, stopAfterConsecutive404 int)

SetPrewarmLimits overrides the defaults for PrewarmGlyphs. Tests use it to avoid running through 1000+ ranges per case.

type Config

type Config struct {
	// BaseURL is the maps worker root, e.g. https://maps.nw5w.com.
	BaseURL string
	// CacheDir is the directory holding cached assets. Callers should
	// pass <TileCacheDir>/style so the catalog, downloads, and style
	// cache all coexist under TileCacheDir without colliding.
	CacheDir string
	// TokenProvider returns the current bearer token; called per fetch
	// so re-registration rotates the token without restart. May be nil
	// (tests). An empty token sends no ?t= parameter.
	TokenProvider func(context.Context) string
	// LocalPrefix is the URL prefix the browser uses to reach this
	// cache (e.g. "/api/maps/style"). Used to rewrite absolute upstream
	// URLs inside style.json so the served copy points at the local
	// proxy. Required for style.json correctness.
	LocalPrefix string
	// Logger defaults to slog.Default().
	Logger *slog.Logger
	// HTTPClient is the upstream HTTP client. Defaults to a 5s-timeout
	// client; tests override with httptest-provided clients (no
	// timeout) or a custom timeout. The 5s default bounds the worst
	// case where the upstream is slow and the browser is waiting.
	HTTPClient *http.Client
}

Config configures a Cache.

Jump to

Keyboard shortcuts

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