configs

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package configs manages runtime-editable configuration stored in the `configs` table. Adding a new app-level knob is a two-step change:

  1. Declare a default in appDefaults() below + add its key constant.
  2. (Optional) Add a typed accessor on Service — e.g. AppURL(), SessionSecret() — so callers don't juggle strings.

Module-level configs live in each module's Config struct; the framework reflects those at boot via entity.StructToConfigs.

Values live in a cache guarded by RWMutex, populated at startup and refreshed on every Set() — so hot-reload is transparent to callers.

Index

Constants

View Source
const (
	KeyAppName              = "app_name"
	DefaultAppName          = "Wick Mini Tools"
	KeyAppDescription       = "app_description"
	DefaultAppDescription   = "A lightweight internal tooling platform — build, deploy, and run custom tools for your team in minutes."
	KeyAppURL               = "app_url"
	KeyAllowedOrigins       = "allowed_origins"
	KeySessionSecret        = "session_secret"
	KeyAdminPasswordChanged = "admin_password_changed"
	KeyEncryptionKey        = "encryption_key"
	KeyStartupScript        = "startup_script"
	KeyStartupScriptEnabled = "startup_script_enabled"
	KeyProfile              = "profile"
	DefaultProfile          = "full"
)

Canonical key constants. Always reference a variable by these rather than the string literal — renaming then becomes a one-line change the compiler catches everywhere.

Variables

This section is empty.

Functions

func EnvOverrideFor added in v0.14.11

func EnvOverrideFor(key string) (envName, value string, set bool)

EnvOverrideFor reports the env-var override for a key, if any. Returns (envName, value, true) only when the env var is declared in envOverrides AND currently set to a non-empty value.

Types

type ConfigValidator added in v0.38.2

type ConfigValidator func(key, value string) error

ConfigValidator rejects an invalid value for one (key, value) pair of an owner's config. Returning an error aborts the write with that error surfaced to the caller; the stored value is left untouched.

It is called with the value as submitted, before secret-keeping and encryption, so a validator sees exactly what the operator typed.

type Encryptor added in v0.6.1

type Encryptor interface {
	EncryptMaster(plain string) (string, error)
	DecryptMaster(token string) (string, error)
	Disabled() bool
}

Encryptor is the subset of *enc.Service the configs layer uses for at-rest encryption of `secret`-tagged rows. Set once after boot via SetEncryptor; reconcile/setOwned check it before encrypting writes or decrypting reads.

type Service

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

Service exposes typed, cached access to runtime variables. The cache is loaded once at startup by Bootstrap() and refreshed on every Set() / Regenerate() call, so reads never touch the DB.

func NewService

func NewService(db *gorm.DB) *Service

func (*Service) AdminPasswordChanged

func (s *Service) AdminPasswordChanged() bool

func (*Service) AllowedOrigins added in v0.14.11

func (s *Service) AllowedOrigins() []string

AllowedOrigins returns the extra URLs/hosts allowed alongside app_url. ALLOWED_ORIGINS env wins (comma-separated) for the same bootstrap reason as APP_URL — needed for LAN/Termux access before the admin UI is reachable. Falls back to the DB-stored kvlist (JSON array of {url: "..."}) otherwise.

func (*Service) AppDescription

func (s *Service) AppDescription() string

func (*Service) AppName

func (s *Service) AppName() string

func (*Service) AppURL

func (s *Service) AppURL() string

AppURL returns the public URL. APP_URL env wins when set — lets the operator override the seeded localhost value without first reaching the admin UI (which is itself behind the host allowlist that uses this value). Falls back to the DB-stored value otherwise.

func (*Service) Bootstrap

func (s *Service) Bootstrap(ctx context.Context, extras ...entity.Config) error

Bootstrap reconciles the given extras plus the app-level defaults with the DB and seeds the in-memory cache. Pass module-declared Configs collected from every registered tool/job — wick assigns Owner before handing them off. Call once at startup, before anything else reads config.

func (*Service) DecryptSecret added in v0.13.0

func (s *Service) DecryptSecret(token string) (string, error)

DecryptSecret decrypts a wick_cenc_ token back to plaintext. Returns the input unchanged when it is not a master token or no encryptor is wired.

func (*Service) DeleteOwned added in v0.6.1

func (s *Service) DeleteOwned(ctx context.Context, owner string) error

DeleteOwned removes every row scoped to owner from the DB and the in-memory cache. Used when a connector / tool / job instance is destroyed. Returns nil when owner has no rows.

func (*Service) DeleteOwnedKey added in v0.13.0

func (s *Service) DeleteOwnedKey(ctx context.Context, owner, key string) error

DeleteOwnedKey removes one (owner, key) row from the DB and the in-memory cache. Used by one-shot migrations that retire a config key without touching siblings. Returns nil even when no such row existed — callers treat it as idempotent.

func (*Service) EncryptSecret added in v0.13.0

func (s *Service) EncryptSecret(plain string) (string, error)

EncryptSecret encrypts plain using the master key. Returns the wick_cenc_ token. No-op (returns plain) when no encryptor is wired or encryption is disabled.

func (*Service) EncryptionKey added in v0.6.0

func (s *Service) EncryptionKey() string

EncryptionKey returns the master key for the encrypted-fields layer. WICK_ENC_KEY in the environment wins when set — production deploys inject from a vault here so the secret never lands in the DB. Falls back to the DB-stored value (auto-generated on first boot via the generators map in spec.go).

func (*Service) EnsureOwned added in v0.6.1

func (s *Service) EnsureOwned(ctx context.Context, owner string, rows ...entity.Config) error

EnsureOwned reconciles a runtime-declared set of rows for owner. Used when a connector / tool / job instance is created after boot: the caller stamps Owner on each row and hands them in; the rows that already exist are left alone (metadata is refreshed, value preserved), missing rows are created.

The owner string scopes the rows — by convention "connector:{id}" for connector instances. Any owner string is accepted.

func (*Service) Get

func (s *Service) Get(key string) string

Get returns the cached value for the app-level key (Owner==""). Callers should prefer typed accessors (AppURL, SessionSecret) so renames are compiler-enforced.

func (*Service) GetOwned

func (s *Service) GetOwned(owner, key string) string

GetOwned returns the cached value for (owner, key), or empty string if missing. Tool/job handlers use this via Ctx helpers; cross-owner reads are allowed but should be rare and intentional.

func (*Service) List

func (s *Service) List() []entity.Config

List returns every app-level variable. Kept for backward compatibility with the admin settings page.

func (*Service) ListOwned

func (s *Service) ListOwned(owner string) []entity.Config

ListOwned returns every config scoped to owner in declaration order. Non-persisted metadata fields (Hidden, VisibleWhen, Options, …) are restored from the meta map because gorm:"-" tags strip them on DB round-trips.

func (*Service) Missing

func (s *Service) Missing(owner string) []string

Missing returns the keys of every Required row in owner that has no value stored. Tool handlers call this via Ctx.Missing() to render a "setup required" banner before doing any work.

func (*Service) Profile added in v0.22.0

func (s *Service) Profile() string

Profile returns the connector profile this instance should register at boot (full/agent/lite). Empty/unset falls back to DefaultProfile.

func (*Service) Regenerate

func (s *Service) Regenerate(ctx context.Context, key string) error

Regenerate replaces an app-level key's value by running its registered generator. Fails if the key is unknown, has no generator, or is not flagged CanRegenerate.

func (*Service) RegisterValidator added in v0.38.2

func (s *Service) RegisterValidator(owner string, fn ConfigValidator)

RegisterValidator installs owner's value validator, replacing any previous one. Call at boot, before the HTTP surface is serving.

func (*Service) SessionSecret

func (s *Service) SessionSecret() string

func (*Service) Set

func (s *Service) Set(ctx context.Context, key, value string) error

Set persists a new value for the app-level key and refreshes the cache. Returns an error if the key is unknown.

func (*Service) SetEncryptor added in v0.6.1

func (s *Service) SetEncryptor(e Encryptor)

SetEncryptor wires the at-rest cipher. Call once at boot after the enc service is built (which itself depends on Bootstrap having reconciled the encryption_key row). After this point, every Set against an IsSecret row writes ciphertext to the DB and the cache holds the decrypted plaintext.

Calling with a Disabled() encryptor is a no-op — the layer behaves as if SetEncryptor was never called.

func (*Service) SetOwned

func (s *Service) SetOwned(ctx context.Context, owner, key, value string) error

SetOwned persists a new value for (owner, key) and refreshes the cache. Returns an error if that pair was never declared.

Jump to

Keyboard shortcuts

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