Documentation
¶
Overview ¶
Package mapscache manages on-disk PMTiles archives for offline use.
Index ¶
- Constants
- Variables
- type Manager
- func (m *Manager) Delete(ctx context.Context, slug string) error
- func (m *Manager) List(ctx context.Context) ([]Status, error)
- func (m *Manager) MigrateLegacyArchives(ctx context.Context) error
- func (m *Manager) PathFor(slug string) string
- func (m *Manager) Start(ctx context.Context, slug string) error
- func (m *Manager) Status(ctx context.Context, slug string) (Status, error)
- type Status
Constants ¶
const DefaultMapsBaseURL = "https://maps.nw5w.com"
DefaultMapsBaseURL is the production tile/download host. Override per env via New if you ever spin up a staging endpoint.
Variables ¶
var ErrAlreadyInflight = errors.New("download already in flight")
ErrAlreadyInflight is returned by Start when another download for the same slug is already running. Callers may ignore this and tell the user the download is already happening.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager orchestrates per-state PMTiles downloads. Active downloads run in goroutines; the in-memory `inflight` map exposes live byte counters so the HTTP handler can report progress without hammering the store. Completion (or failure) writes the final state to the store and removes the inflight entry. At most maxConcurrent downloads run at once.
func New ¶
func New(cacheDir string, store *configstore.Store, tokenProvider func(context.Context) string, mapsBaseURL string, maxConcurrent int) *Manager
New constructs a Manager. tokenProvider is called per-download to fetch the current bearer token (it may change if the user re-registers); mapsBaseURL should be DefaultMapsBaseURL in production; maxConcurrent caps how many downloads run in parallel (defaults to 2 if non-positive — be polite to the upstream).
func (*Manager) Delete ¶
Delete cancels any in-flight download for slug, removes the persisted row, and best-effort removes the on-disk file. Idempotent on absent slugs.
func (*Manager) List ¶
List returns the status of every state with a persisted row plus any in-flight downloads not yet persisted (first-time downloads before the upstream Content-Length comes back). Inflight entries override persisted ones so re-downloads show live progress.
func (*Manager) MigrateLegacyArchives ¶ added in v0.12.1
MigrateLegacyArchives moves legacy bare-slug files (<cache>/colorado.pmtiles) into the new namespaced state subdir (<cache>/state/colorado.pmtiles). Idempotent: skips files already in subdirs and skips non-pmtiles files.
Collision policy: if the namespaced target already exists, the legacy file is removed rather than overwriting the (presumably newer) namespaced file. Otherwise os.Rename clobbers a file an operator may have already redownloaded under the new layout. Designed to run once on startup; safe to re-run.
func (*Manager) PathFor ¶
PathFor returns the on-disk path of slug's PMTiles archive. For namespaced slugs, the slashes become subdirectory separators:
state/colorado -> <cache>/state/colorado.pmtiles country/de -> <cache>/country/de.pmtiles province/ca/british-... -> <cache>/province/ca/british-...pmtiles
func (*Manager) Start ¶
Start kicks off a download for slug. Returns ErrAlreadyInflight if another download for the same slug is already running. Idempotent otherwise — re-downloads succeed by replacing the file atomically. The caller does not block: this returns as soon as the goroutine is spawned.
type Status ¶
type Status struct {
Slug string `json:"slug"`
State string `json:"state"` // "absent" | "pending" | "downloading" | "complete" | "error"
BytesTotal int64 `json:"bytes_total"`
BytesDownloaded int64 `json:"bytes_downloaded"`
DownloadedAt time.Time `json:"downloaded_at,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}
Status is a snapshot of one slug's download state.