svcroot

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 9 Imported by: 0

README

svcroot

Service instance roots, runtime file layout, project-root discovery, and known-instance registries for long-running local services.

Install

From pkg.go.dev:

go get github.com/brandonkramer/svcroot

Configure once with Service

svc := &svcroot.Service{
    EnvVar:       "MYAPP_HOME",
    DefaultDir:   ".myapp",
    RegistryFile: "sessions/known-instances.json", // optional
    Layout: svcroot.Layout{
        SessionsDir: "sessions",
        SocketName:  "rpc.sock",
        LockName:    "service.lock",
    },
}

root, err := svc.Resolve()
h := svc.Open(root)

socket := h.Socket()
lock := h.Lock()
tasksDir := h.Join("tasks")
registry := h.RuntimeJoin("state.json")

Legacy helpers

layout := &svcroot.DefaultLayout
socket := svcroot.Socket(root, layout)
lock := svcroot.Lock(root, layout)

Development

make check          # race tests + linux compile + golangci-lint
make install-hooks  # lefthook pre-commit/pre-push

Documentation

Overview

Package svcroot resolves service instance roots, runtime file layout, project-root discovery, and optional known-instance registries.

Configure a Service once, then call Service.Resolve and Service.Open for path helpers. Use Layout directly when you prefer functional helpers.

Examples

svc := &svcroot.Service{
    EnvVar:     "MYAPP_HOME",
    DefaultDir: ".myapp",
    Layout:     svcroot.DefaultLayout,
}
root, err := svc.Resolve()
socket := svc.Open(root).Socket()

Index

Constants

This section is empty.

Variables

View Source
var DefaultLayout = Layout{
	SessionsDir:       defaultSessionsDir,
	SocketName:        defaultSocketName,
	ObserveSocketName: defaultObserveSocketName,
	LockName:          defaultLockName,
	PipePrefix:        defaultPipePrefix,
}

DefaultLayout fills empty Layout fields with conventional names.

View Source
var ErrEmptyHome = errors.New("svcroot: empty home path")

ErrEmptyHome is returned when a service root path resolves to an empty string.

View Source
var ErrRegistryNotConfigured = errors.New("svcroot: registry file not configured")

ErrRegistryNotConfigured is returned when registry operations run without RegistryFile.

Functions

func CandidateHomes

func CandidateHomes(registryPath, envVar, envHome, defaultHome string) ([]string, error)

CandidateHomes returns deduplicated service roots from envVar, envHome, defaultHome, and the registry file.

func DefaultHomeDir

func DefaultHomeDir(defaultDirName string) (string, error)

DefaultHomeDir returns ~/.defaultDirName under the user home directory.

func FindProjectRoot

func FindProjectRoot(from, markerDir string) (string, bool)

FindProjectRoot walks upward from from to find markerDir as a subdirectory.

func Lock

func Lock(root string, layout *Layout) string

Lock returns the lock file path under root.

func ObserveSocket

func ObserveSocket(root string, layout *Layout) string

ObserveSocket returns the read-only observe HTTP socket path under root.

func RegisterKnownHome

func RegisterKnownHome(registryPath, home, now string) error

RegisterKnownHome records root in the registry at registryPath.

func ResolveHome

func ResolveHome(envVar, defaultDirName string) (string, error)

ResolveHome returns the envVar value when set, otherwise ~/.defaultDirName.

func Sessions

func Sessions(root string, layout *Layout) string

Sessions returns root joined with the configured runtime directory.

func Socket

func Socket(root string, layout *Layout) string

Socket returns the Unix domain socket path under root.

Types

type Home

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

Home resolves paths under one service root directory.

func (*Home) Join

func (h *Home) Join(elem ...string) string

Join joins path elements directly under the service root.

func (*Home) Lock

func (h *Home) Lock() string

Lock returns the service lock file path when configured.

func (*Home) ObserveSocket

func (h *Home) ObserveSocket() (string, bool)

ObserveSocket returns the observe socket path and whether this layout defines one.

func (*Home) PipePrefix

func (h *Home) PipePrefix() (string, bool)

PipePrefix returns the Windows named-pipe prefix and whether this layout defines one.

func (*Home) Root

func (h *Home) Root() string

Root returns the service root directory.

func (*Home) Runtime

func (h *Home) Runtime() string

Runtime returns the runtime state directory under root (typically root/sessions).

func (*Home) RuntimeJoin

func (h *Home) RuntimeJoin(elem ...string) string

RuntimeJoin joins path elements under the runtime directory.

func (*Home) Socket

func (h *Home) Socket() string

Socket returns the primary RPC socket path when configured.

type HomeEntry

type HomeEntry struct {
	// Path is the absolute service root directory.
	Path string `json:"path"`
	// LastSeen is an optional RFC3339 timestamp from the last registration.
	LastSeen string `json:"last_seen,omitempty"`
}

HomeEntry records one known service root.

type KnownHomes

type KnownHomes struct {
	// Homes lists previously registered service roots.
	Homes []HomeEntry `json:"homes"`
}

KnownHomes is the on-disk known-instance registry.

func LoadKnownHomes

func LoadKnownHomes(path string) KnownHomes

LoadKnownHomes reads the registry at path, returning an empty file when missing.

type Layout

type Layout struct {
	// SessionsDir is the runtime state directory name under the service root.
	SessionsDir string
	// SocketName is the primary RPC socket file name inside SessionsDir.
	SocketName string
	// ObserveSocketName is the observe socket file name; leave empty to disable.
	ObserveSocketName string
	// LockName is the service lock file name inside SessionsDir.
	LockName string
	// PipePrefix is the Windows named-pipe prefix; leave empty to disable.
	PipePrefix string
}

Layout names runtime files under a service root directory.

func (*Layout) At

func (l *Layout) At(root string, underRuntime bool, elem ...string) string

At joins path elements under root. When underRuntime is true, the runtime directory is inserted first.

func (*Layout) ObserveSocketPath

func (l *Layout) ObserveSocketPath(root string) (string, bool)

ObserveSocketPath returns the observe socket path when ObserveSocketName is set.

func (*Layout) PipePrefixName

func (l *Layout) PipePrefixName() (string, bool)

PipePrefixName returns the configured Windows pipe prefix when set.

func (*Layout) RuntimeDir

func (l *Layout) RuntimeDir(root string) string

RuntimeDir returns root joined with the runtime directory name.

func (*Layout) WithDefaults

func (l *Layout) WithDefaults() Layout

WithDefaults returns layout with empty core fields filled from DefaultLayout.

type Service

type Service struct {
	// EnvVar overrides DefaultDir when set in the process environment.
	EnvVar string
	// DefaultDir is appended to the user home when EnvVar is unset (e.g. ".myapp").
	DefaultDir string
	// Layout names runtime files under each service root.
	Layout Layout
	// RegistryFile is joined with DefaultHome() for known-instance registry paths.
	// Leave empty when the service does not persist known instances.
	RegistryFile string
}

Service describes how one long-running service resolves its root directory, lays out runtime files, and optionally tracks known instances on disk.

func (*Service) Candidates

func (s *Service) Candidates(explicitRoot string) ([]string, error)

Candidates returns deduplicated service roots from explicitRoot, EnvVar, DefaultHome, and the registry.

func (*Service) DefaultHome

func (s *Service) DefaultHome() (string, error)

DefaultHome returns ~/.DefaultDir regardless of EnvVar.

func (*Service) Open

func (s *Service) Open(root string) *Home

Open returns path helpers for an explicit service root.

func (*Service) Register

func (s *Service) Register(root, seenAt string) error

Register records root in this service's known-instance registry.

func (*Service) RegistryPath

func (s *Service) RegistryPath() (string, error)

RegistryPath returns the absolute known-instance registry path, or ("", nil) when unset.

func (*Service) Resolve

func (s *Service) Resolve() (string, error)

Resolve returns the configured service root directory.

Jump to

Keyboard shortcuts

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