Documentation
¶
Overview ¶
Package dev wires hot-reload onto a rules engine for local development. It watches a YAML directory on disk and rebuilds the engine whenever a pipeline file changes, swapping the active instance atomically so in-flight requests see a consistent view.
Wire it from main when HB_ENV is "local":
devSrv, err := rulesdev.New(rulesdev.Options{
EngineFn: buildEngineFromDisk, // fresh engine per call
WatchRoot: "services/service-foo/pipelines",
})
if err != nil { /* fail boot */ }
mux.Handle("/__rules/visitor", devSrv.HTTPHandler("visitor_flags"))
// devSrv satisfies system.Module — drop into RunForever
// alongside the HTTP server module.
system.RunForever(ctx, server, devSrv)
Never run this in production:
- re-parses YAML on every file change with no rate limit;
- swaps a process-wide pointer with no auditing;
- logs the parse error and keeps serving the stale engine so a malformed save doesn't take the page offline — a contract that is fine for laptops, not for live traffic.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// EngineFn returns a fresh, already-Built engine. The dev
// wrapper calls it once at New (so a misconfigured YAML or
// missing rule fails at boot) and again on every YAML change.
// Typical implementation:
//
// func() (*rulesengine.Engine, error) {
// eng := rulesengine.NewEngine().
// Register(rules.All()...).
// Load(os.DirFS("services/service-foo/pipelines"), "*.yaml")
// return eng, eng.Build()
// }
//
// Required.
EngineFn func() (*rulesengine.Engine, error)
// WatchRoot is the directory whose subtree the watcher monitors
// for YAML pipeline changes. A change anywhere underneath fires
// EngineFn after the debounce window. Typically the service's
// pipelines/ directory. Required.
WatchRoot string
// CodeWatchRoots are additional directories whose *.go files
// trigger a warning log when they change. Useful for laptop
// development: Go binaries can't hot-reload code, so when you
// edit a rule implementation the watcher tells you to restart
// the process — without spamming the log with no-op engine
// rebuilds that wouldn't pick up the change anyway.
//
// Roots that don't exist on disk are skipped with a single boot
// log line, never a hard error — keeping a missing `packages/
// rules` (e.g. when running from outside the repo root) from
// taking the service offline. Optional.
CodeWatchRoots []string
// DebounceWindow collapses bursty editor saves. Zero → 100 ms.
DebounceWindow time.Duration
}
Options configures a dev Server.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the dev wrapper. Build with New, drop into system.RunForever via Register/Shutdown, expose endpoints via HTTPHandler.
func New ¶
New validates opts, calls EngineFn once so boot fails fast on a bad config, and stages the watcher. Register starts the loop.
func (*Server) Engine ¶
func (s *Server) Engine() *rulesengine.Engine
Engine returns the currently active rules engine. Reads are lock-free; the value may change between calls if a rebuild lands in between.
func (*Server) HTTPHandler ¶
func (s *Server) HTTPHandler(pipeline string) http.HandlerFunc
HTTPHandler returns an http.HandlerFunc that always dispatches through the current engine — even after a hot-reload swaps it. Bind it once at boot and forget; the closure resolves the engine on every request.