storage

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package storage defines the Storage-backend registry used by OpenURL to construct a den.Storage from a URL-style DSN. Concrete backend implementations live in sub-packages and register themselves on import:

import (
    "github.com/oliverandrich/den/storage"
    _ "github.com/oliverandrich/den/storage/file" // registers file://
)

s, err := storage.OpenURL("file:///data/media?url_prefix=/media")

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyContent = errors.New("storage: refusing to store empty content")

ErrEmptyContent is returned by Storage implementations when a Store call receives zero bytes. Exposed here so HTTP-layer code can check for it regardless of which backend is in use.

View Source
var ErrUnsupportedScheme = errors.New("storage: no backend registered for scheme")

ErrUnsupportedScheme is returned by OpenURL when no opener is registered for the DSN's scheme — typically because the caller forgot the side-effect import (e.g. `_ "github.com/oliverandrich/den/storage/file"`). Wrapped with the actual scheme via fmt.Errorf so callers can use errors.Is to detect this case without scraping error strings.

Functions

func OpenURL added in v0.10.0

func OpenURL(dsn string) (den.Storage, error)

OpenURL parses dsn as "<scheme>://<location>" and delegates to the OpenerFunc registered for that scheme. The opener receives the full location verbatim, including any query string; backends that honour the `?url_prefix=…` convention should use URLPrefixFromLocation to extract it.

The scheme is matched case-insensitively: "file://...", "File://..." and "FILE://..." all resolve to the same backend.

Returns an error with a helpful message for empty DSNs, missing schemes, or schemes without a registered opener — the last usually means a backend sub-package needs to be side-effect imported.

func Register added in v0.10.0

func Register(scheme string, opener OpenerFunc)

Register associates an OpenerFunc with a URL scheme. Typical usage is from a backend sub-package's init():

func init() {
    storage.Register("file", openFileStorage)
}

The scheme is normalized to lowercase before storage so registration and lookup stay case-insensitive, matching URL-scheme semantics: "file", "File", and "FILE" all address the same backend.

Panics if a different opener is already registered for scheme — mirrors Den's database-backend registration semantics. Duplicate registrations surface mis-wiring (two backend packages claiming the same scheme, a replace-directive fork, or a manual call after a side-effect import) at process startup instead of at first lookup.

func URLPrefixFromLocation added in v0.11.0

func URLPrefixFromLocation(location string) (stripped, prefix string)

URLPrefixFromLocation splits the conventional `url_prefix=…` query parameter out of a DSN location, returning the location with that param removed and the value of the prefix. Other query parameters survive, re-encoded via url.Values.Encode (so they may be reordered alphabetically — opener implementations don't depend on order).

A location with no query string, or a query string without `url_prefix`, is returned unchanged with an empty prefix. An empty value (`?url_prefix=`) is treated the same as not specified.

Falls back to returning the location unchanged when the query string is malformed enough to fail url.ParseQuery — opener gets the raw location and can decide whether to error.

Backends that want to expose a configurable URL prefix should call this on the location they receive in their OpenerFunc, use the prefix as needed, and parse the stripped location for any other query parameters they understand. Backends that return absolute URLs (e.g. S3) can call this to silently discard a stray `url_prefix` before their own parser runs, matching the framework's "url_prefix is meaningless for absolute-URL backends" contract.

Types

type OpenerFunc added in v0.10.0

type OpenerFunc func(location string) (den.Storage, error)

OpenerFunc constructs a den.Storage from the location portion of a DSN — everything after the `<scheme>://`, including any query string. Backends that want to honour the conventional `?url_prefix=…` query parameter should call URLPrefixFromLocation to extract it before parsing the rest of their location. Registered per scheme via Register.

Directories

Path Synopsis
Package file provides a local-filesystem Storage backend for Den.
Package file provides a local-filesystem Storage backend for Den.
Package s3 is the S3 (and S3-compatible, e.g.
Package s3 is the S3 (and S3-compatible, e.g.

Jump to

Keyboard shortcuts

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