loader

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package loader provides a file-system–based key loader with live updates.

It monitors a directory for resource files (such as signing keys), extracts a key identifier (KeyID) from file paths based on configurable rules, and stores the file contents in a key–value storage backend.

Typical use cases include:

  • Cryptographic key management
  • Dynamic configuration loading
  • Hot-reloading of resources from disk

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExtensionIsEmpty is returned when WithExtension("") is called.
	ErrExtensionIsEmpty = errors.New("extension is empty")

	// ErrStorageNotSpecified is returned when a nil storage is passed to WithStorage.
	ErrStorageNotSpecified = errors.New("storage not specified")
)

Functions

This section is empty.

Types

type KeyIDType

type KeyIDType uint32

KeyIDType defines how the loader generates Key IDs from file paths.

Different strategies are available depending on how keys should be identified.

const (
	// FileNameWithoutExtension uses the base file name without its extension
	// as the Key ID.
	//
	// Example:
	//   Path:       /tmp/keys/signing.pem
	//   Extension:  .pem
	//   KeyID:      signing
	FileNameWithoutExtension KeyIDType = 1 << iota

	// FileNameWithExtension uses the base file name including its extension
	// as the Key ID.
	//
	// Example:
	//   Path:   /tmp/keys/signing.pem
	//   KeyID:  signing.pem
	FileNameWithExtension

	// FileFullPathRelativeToLocation uses the file path relative to the
	// configured root location as the Key ID.
	//
	// Example:
	//   Location:  /tmp/keys
	//   Path:      /tmp/keys/sub/key.pem
	//   KeyID:     /sub/key.pem
	FileFullPathRelativeToLocation

	// FileFullPath uses the absolute file path as the Key ID.
	//
	// Example:
	//   Path:   /tmp/keys/sub/key.pem
	//   KeyID:  /tmp/keys/sub/key.pem
	FileFullPath
)

type Loader

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

Loader watches a directory for resource files and maintains a key–value store of file contents, indexed by Key IDs.

func Create

func Create(location string, opts ...Option) (*Loader, error)

Create initializes a Loader that watches the given location for files.

Options may be provided to customize KeyID extraction, file extension handling, and storage backend.

Example:

ldr, err := loader.Create(
    "/etc/keys",
    loader.WithExtension(".pem"),
    loader.WithKeyIDType(loader.FileNameWithoutExtension),
)
if err != nil {
    log.Fatal(err)
}

if err := ldr.StartWatching(); err != nil {
    log.Fatal(err)
}
defer ldr.StopWatching()

func (*Loader) StartWatching

func (dl *Loader) StartWatching() error

StartWatching starts the file system watcher and performs an initial load of all resources.

Returns an error if the watcher cannot be started or if resources cannot be loaded.

func (*Loader) StopWatching

func (dl *Loader) StopWatching() error

StopWatching stops the watcher and releases resources. Safe to call multiple times.

func (*Loader) Storage

Storage returns a read-only view of the Loader’s storage. Consumers can use this to retrieve key data without modifying the internal storage.

type Option

type Option func(*Loader) error

Option represents a configuration option for Loader.

func WithExtension

func WithExtension(value string) Option

WithExtension configures the file extension that identifies relevant files.

If the extension is missing a leading ".", it will be added automatically. The extension must not be empty, otherwise ErrExtensionIsEmpty is returned.

This option is only used when KeyIDType is FileNameWithoutExtension.

func WithKeyIDType

func WithKeyIDType(value KeyIDType) Option

WithKeyIDType configures the KeyID extraction strategy. The default KeyIDType is FileFullPath.

func WithStorage

func WithStorage(storage keyvalue.StringToBytesStorage) Option

WithStorage configures the storage backend used by Loader.

If storage is nil, ErrStorageNotSpecified is returned. By default, an in-memory storage is used.

Jump to

Keyboard shortcuts

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