dev

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func New(opts Options) (*Server, error)

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.

func (*Server) Register

func (s *Server) Register(ctx context.Context) error

Register starts the file-watching goroutine and returns immediately, satisfying system.Module so a *Server drops into system.RunForever alongside the HTTP server.

Always returns nil — fsnotify failures surfaced at New time.

func (*Server) Shutdown

func (s *Server) Shutdown(_ context.Context) error

Shutdown releases the underlying fsnotify watcher. Satisfies the shutdown half of system.Module so RunForever's reverse-order drain frees the OS handle before exit.

Jump to

Keyboard shortcuts

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