utils

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPIDFile added in v0.4.0

func CheckPIDFile(dataPath string) (exists bool, alive bool, pid int, err error)

CheckPIDFile reads the postmaster.pid file in the given data directory and determines whether the PostgreSQL process is still running. Returns:

  • exists: true if the postmaster.pid file exists (even if unreadable)
  • alive: true if the process referenced by the PID is currently running
  • pid: the process ID from the file, or 0 if the file doesn't exist or is invalid
  • err: non-nil for unexpected I/O errors (permission denied, etc.)

Note: os.FindProcess always succeeds on Unix; the Signal(0) probe is the actual liveness check. On Windows, this file is excluded via build constraint.

func GetFreePort added in v0.3.0

func GetFreePort() (int, error)

GetFreePort asks the OS to allocate an unused port on localhost. Note: There is a small race window between when the port is returned and when it is actually used by the caller. In high-contention scenarios, the port may be taken by another process.

func IsDataPathInitialized added in v0.4.0

func IsDataPathInitialized(dataPath string) (bool, error)

IsDataPathInitialized checks if a PostgreSQL data directory has been initialized by verifying the existence of the PG_VERSION file created by initdb. Returns true if PG_VERSION exists, false if it does not exist. Returns an error for I/O or permission issues (distinct from "not initialized").

func IsEmbeddedPGRunning added in v0.4.0

func IsEmbeddedPGRunning(dataPath string) bool

IsEmbeddedPGRunning is a composite check that determines whether an embedded PostgreSQL instance is already running at the given data directory and port. It delegates to ReuseEmbeddedPG which checks PID file liveness and port. Returns true only if the process is alive AND the port is accepting connections.

func IsFromLocalhost

func IsFromLocalhost(req *http.Request) bool

func IsPortListening added in v0.4.0

func IsPortListening(host string, port int, timeout time.Duration) bool

IsPortListening checks if a TCP port is accepting connections on the given host. Returns true if a connection can be established within the specified timeout.

func MapToStruct

func MapToStruct[T any](m map[string]any) *T

MapToStruct deserializes a map[string]any back into a typed struct pointer. Returns nil if m is nil or empty.

func PrettyJSON

func PrettyJSON(v any) json.RawMessage

func ReadPostmasterPort added in v0.4.0

func ReadPostmasterPort(dataPath string) (int, error)

ReadPostmasterPort reads the TCP port number from line 4 of the postmaster.pid file in the given data directory. The postmaster.pid format is:

Line 1: PID
Line 2: data directory path
Line 3: timestamp
Line 4: port number
Line 5: Unix socket directory

func RequestHost added in v0.2.1

func RequestHost(req *http.Request) string

func ReuseEmbeddedPG added in v0.4.0

func ReuseEmbeddedPG(dataPath string) (running bool, port int)

ReuseEmbeddedPG checks whether an embedded PostgreSQL instance is already running at the given data directory. If running, it returns (true, port) where port is read from the postmaster.pid file. Returns (false, 0) if no running instance is detected or the data path is empty.

func StructToMap

func StructToMap[T any](v *T) map[string]any

StructToMap serializes a pointer to any json-tagged struct into a map[string]any. Returns nil if v is nil.

func StructToMapLC added in v0.1.0

func StructToMapLC(v any, opts ...Option) map[string]any

StructToMapLC converts a struct to map[string]any using reflection, converting PascalCase field names to camelCase (first character lowercased). Embedded structs are flattened into the parent map. Optional behaviors (ID suffix conversion, omit-empty) can be enabled via Option. Returns nil if v is nil or not a struct.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, status int, v any) error

Types

type Option added in v0.1.1

type Option func(*options)

Option configures the behavior of StructToMapLC.

func WithIDSuffix added in v0.1.1

func WithIDSuffix() Option

WithIDSuffix converts field names ending in "ID" to end in "Id". e.g. "UserID" becomes "userId" instead of "userID".

func WithNilMapToEmpty added in v0.1.2

func WithNilMapToEmpty() Option

WithNilMapToEmpty converts nil map fields to empty maps {} instead of null.

func WithNilSliceToEmpty added in v0.1.2

func WithNilSliceToEmpty() Option

WithNilSliceToEmpty converts nil slice fields to empty slices [] instead of null.

func WithOmitEmpty added in v0.1.1

func WithOmitEmpty() Option

WithOmitEmpty skips fields with empty values: empty string, nil pointer, nil or zero-length slice. Numeric and boolean fields are never omitted.

type SplitLevelHandler

type SplitLevelHandler struct {
	StdHandler slog.Handler
	ErrHandler slog.Handler
}

SplitLevelHandler routes log records to stdout for levels below Error, and to stderr for Error and above.

func (*SplitLevelHandler) Enabled

func (h *SplitLevelHandler) Enabled(_ context.Context, level slog.Level) bool

func (*SplitLevelHandler) Handle

func (h *SplitLevelHandler) Handle(ctx context.Context, r slog.Record) error

func (*SplitLevelHandler) WithAttrs

func (h *SplitLevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SplitLevelHandler) WithGroup

func (h *SplitLevelHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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