render

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package render plugs FastConf into the long tail of legacy daemons that only consume on-disk configuration files (nginx.conf, envoy.yaml, postgresql.conf, ...). It mirrors the Consul-Template / Spring Cloud Config "render to disk + signal" workflow but stays inside the calling Go process, so there is no separate sidecar to operate.

A Wire[T] call subscribes to a Manager's per-T notifications, renders the typed snapshot through a Renderer, atomically writes the bytes to outPath via a temp-file + rename(2), and finally fires every registered OnChange hook (SIGHUP a pid, POST to a webhook, restart a systemd unit, ...). On any error the previous file is preserved and a structured log line is emitted; the framework never partially overwrites the destination.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wire

func Wire[T any](
	mgr *fastconf.Manager[T],
	r Renderer[T],
	outPath string,
	opts Options,
	hooks ...Hook,
) (cancel func(), err error)

Wire subscribes to mgr's typed snapshot stream, atomically writes the rendered bytes to outPath, then runs every hook in order. The returned cancel removes the subscription; the renderer fires once eagerly for the current snapshot so the file exists before the function returns.

Types

type Hook

type Hook func(ctx context.Context, outPath string) error

Hook runs after a successful atomic write. Hooks should be fast and non-fatal; an error is logged but does not roll back the write.

func HTTPGet

func HTTPGet(url string) Hook

HTTPGet returns a Hook that issues a GET to url and treats any 2xx/3xx response as success. Use it to poke a webhook or kick off an external orchestrator after every config change.

func ReloadSystemd

func ReloadSystemd(unit string) Hook

ReloadSystemd returns a Hook that runs `systemctl reload <unit>`. It is implemented via os/exec rather than the dbus binding to keep this module dependency-free; mocked in tests via Options.OnError verification.

func SignalProcess

func SignalProcess(pidFile string, sig os.Signal) Hook

SignalProcess returns a Hook that reads a PID from pidFile and sends the signal to it. Useful for "kill -HUP nginx" style reloads.

type Options

type Options struct {
	// FileMode for the rendered file (default 0o644).
	FileMode os.FileMode
	// HookTimeout caps each hook invocation (default 5s).
	HookTimeout time.Duration
	// OnError, when non-nil, receives every render/write/hook failure
	// in addition to the slog logger (the manager's default).
	OnError func(err error)
	// SkipFirstHook, when true, writes the file once on Wire() but does
	// NOT fire hooks for that initial render. Use when downstream
	// daemons are not yet up at boot and a SIGHUP would race with their
	// own startup. Phase 20 BUG-203.
	SkipFirstHook bool
}

Options configures Wire. The zero value is valid: temp files are created next to the destination, the hook context defaults to context.Background(), and write mode falls back to 0o644.

type Renderer

type Renderer[T any] interface {
	Render(value *T) ([]byte, error)
}

Renderer turns a strongly-typed configuration snapshot into bytes. The most common impl is GoTemplate; users can supply their own (e.g. a pongo2 or jet renderer) without depending on this module's templating.

func GoTemplate

func GoTemplate[T any](tmplPath string, funcs template.FuncMap) (Renderer[T], error)

GoTemplate returns a Renderer that loads the named text/template file once and re-executes it on every snapshot. The template's data is the dereferenced *T (so users write `{{ .Server.Port }}`), and FuncMap can inject helpers (e.g. quote, indent). Pass nil FuncMap for the defaults.

type RendererFunc

type RendererFunc[T any] func(*T) ([]byte, error)

RendererFunc adapts a free function to the Renderer interface.

func (RendererFunc[T]) Render

func (f RendererFunc[T]) Render(v *T) ([]byte, error)

Render implements Renderer.

Jump to

Keyboard shortcuts

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