http

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package http provides HTTP middleware and handlers for statekit state machines. It offers framework-agnostic utilities as well as adapters for popular Go web frameworks.

Index

Constants

View Source
const (
	// MachineContextKey is the key for storing the interpreter in request context.
	MachineContextKey contextKey = "statekit.machine"
)

Variables

This section is empty.

Functions

func MachineFromContext

func MachineFromContext[C any](ctx context.Context) (*statekit.Interpreter[C], bool)

MachineFromContext retrieves an interpreter from the request context.

func NewServeMux

func NewServeMux[C any](interp *statekit.Interpreter[C], prefix string) *http.ServeMux

NewServeMux creates a standard library mux with state machine routes.

func WithMachine

func WithMachine[C any](ctx context.Context, interp *statekit.Interpreter[C]) context.Context

WithMachine adds an interpreter to the request context.

Types

type EventRequest

type EventRequest struct {
	Type    string         `json:"type"`
	Payload map[string]any `json:"payload,omitempty"`
}

EventRequest represents an incoming event.

type EventResponse

type EventResponse struct {
	PreviousState string `json:"previousState"`
	CurrentState  string `json:"currentState"`
	Transitioned  bool   `json:"transitioned"`
	Done          bool   `json:"done"`
}

EventResponse represents the response after sending an event.

type MachineHandler

type MachineHandler[C any] struct {
	// contains filtered or unexported fields
}

MachineHandler provides HTTP handlers for interacting with a state machine.

func NewMachineHandler

func NewMachineHandler[C any](interp *statekit.Interpreter[C]) *MachineHandler[C]

NewMachineHandler creates a new HTTP handler for a state machine.

func (*MachineHandler[C]) HandleGetContext

func (h *MachineHandler[C]) HandleGetContext(w http.ResponseWriter, r *http.Request)

HandleGetContext returns the current context (as JSON).

func (*MachineHandler[C]) HandleGetState

func (h *MachineHandler[C]) HandleGetState(w http.ResponseWriter, r *http.Request)

HandleGetState returns the current state.

func (*MachineHandler[C]) HandleSendEvent

func (h *MachineHandler[C]) HandleSendEvent(w http.ResponseWriter, r *http.Request)

HandleSendEvent processes an event and returns the new state.

func (*MachineHandler[C]) ServeHTTP

func (h *MachineHandler[C]) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler for basic routing.

type MachineRegistry

type MachineRegistry[C any] struct {
	// contains filtered or unexported fields
}

MachineRegistry manages multiple state machine instances.

func NewMachineRegistry

func NewMachineRegistry[C any](factory func(id string) (*statekit.Interpreter[C], error)) *MachineRegistry[C]

NewMachineRegistry creates a new registry with a factory function.

func (*MachineRegistry[C]) Get

func (r *MachineRegistry[C]) Get(id string) (*statekit.Interpreter[C], error)

Get retrieves or creates an interpreter for the given ID.

func (*MachineRegistry[C]) List

func (r *MachineRegistry[C]) List() []string

List returns all registered machine IDs.

func (*MachineRegistry[C]) Remove

func (r *MachineRegistry[C]) Remove(id string)

Remove stops and removes an interpreter.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a function that wraps an http.Handler.

func MachineMiddleware

func MachineMiddleware[C any](interp *statekit.Interpreter[C]) Middleware

MachineMiddleware returns middleware that injects a machine into the request context.

func RegistryMiddleware

func RegistryMiddleware[C any](registry *MachineRegistry[C], idExtractor func(*http.Request) string) Middleware

RegistryMiddleware returns middleware that looks up a machine by ID from the request.

type StateResponse

type StateResponse struct {
	CurrentState string `json:"currentState"`
	Done         bool   `json:"done"`
	MachineID    string `json:"machineId,omitempty"`
}

StateResponse represents the state machine state in JSON.

Jump to

Keyboard shortcuts

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