plugisservice

package module
v0.0.0-...-cf7586a Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 17 Imported by: 0

README

plugisservice

plugisservice is a package to write plugis3 services in Go.

A Plugis3 service is a NATS micro service with some specific properties that must implement the PlugisServiceIntf interface.

NATS Micro PlugisServiceIntf Integration

For NATS services, see:

Files

The plugisservice.go file defines the PlugisServiceIntf interface, which specifies the contract that all plugis services must implement.

The plugis.go file defines the PlugisIntf interface, which specifies the functions provided by plugis

The example/echoService/echoService.go file is a minimal service implementation sample.

The example/echoService/cmd/main.go file is a sample of ServiceRunner usage.

How to declare a service




Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNatsNotConnected  = errors.New("nats not connected")
	ErrNatsConnectionNil = errors.New("nats connection is nil")
)

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	Name       string   `json:"name"`
	Subject    string   `json:"subject"`
	QueueGroup string   `json:"queue_group"`
	Metadata   Metadata `json:"metadata,omitempty"`
}

Endpoint is the information about an endpoint.

type Event

type Event[T any] struct {
	Type string `json:"type,omitempty"`
	Data T      `json:"data,omitempty"`
}

type Metadata

type Metadata map[string]string

Metadata is a map of key-value pairs that can be used to store additional information about a service/endpoint

type Plugis

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

Plugis is the default implementation of the PlugisIntf that provides the functionality to plugis services.

func (*Plugis) GetServices

func (plugis *Plugis) GetServices(ctx context.Context) ([]ServiceInfo, error)

GetServices sends a request to the $SRV.INFO subject and returns a list of services.

func (*Plugis) Logger

func (plugis *Plugis) Logger() *slog.Logger

Logger returns the logger for the Plugis instance.

func (*Plugis) Nats

func (plugis *Plugis) Nats() *nats.Conn

Nats returns the nats connection for the Plugis instance.

func (*Plugis) Prefix

func (plugis *Plugis) Prefix() string

Prefix returns the prefix for the Plugis instance.

func (*Plugis) Publish

func (plugis *Plugis) Publish(topic string, payload []byte) error

Publish publishes a message to the nats connection.

func (*Plugis) Request

func (plugis *Plugis) Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)

Request sends a request to the nats connection.

func (*Plugis) RequestCtx

func (plugis *Plugis) RequestCtx(ctx context.Context, subj string, data []byte) (*nats.Msg, error)

RequestCtx sends a request to the nats connection and returns a single message. context is used for timeout and cancellation.

func (*Plugis) RequestMany

func (plugis *Plugis) RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)

RequestMany sends a request to the nats connection and returns a sequence of messages.

func (*Plugis) SetLogger

func (plugis *Plugis) SetLogger(log *slog.Logger)

SetLogger sets the logger for the Plugis instance.

func (*Plugis) SetNats

func (plugis *Plugis) SetNats(nc *nats.Conn)

SetNats sets the nats connection for the Plugis instance.

func (*Plugis) SetPrefix

func (plugis *Plugis) SetPrefix(prefix string)

SetPrefix sets the prefix for the Plugis instance.

func (*Plugis) StartService

func (plugis *Plugis) StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error)

StartService initializes and starts a NATS service for the given PlugisServiceIntf implementation. It returns the created NatsService and any error encountered during the creation process.

func (*Plugis) VariableSet

func (plugis *Plugis) VariableSet(name string, value any, varType string) error

VariableSet sets a variable with the given name, value, and type, then publishes it to a corresponding topic.

func (*Plugis) VariableUnset

func (plugis *Plugis) VariableUnset(name string) error

VariableUnset unsets a variable with the given name, then publishes it to a corresponding topic.

type PlugisIntf

type PlugisIntf interface {
	SetLogger(*slog.Logger)
	Logger() *slog.Logger
	SetNats(*nats.Conn)
	Nats() *nats.Conn
	SetPrefix(prefix string)
	Prefix() string
	Publish(topic string, payload []byte) error
	Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)
	RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)
	GetServices(ctx context.Context) ([]ServiceInfo, error)
	StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error)
	VariableSet(name string, value any, varType string) error
	VariableUnset(name string) error
}

PlugisIntf holds the methods that can be used by a plugis service.

type PlugisServiceIntf

type PlugisServiceIntf interface {
	Run(ctx context.Context) error
	Name() string
	Description() string
	Version() string
	Metadata() Metadata
	PlugisIntf
}

PlugisServiceIntf is the interface that must be implemented by all plugis services.

type ServiceInfo

type ServiceInfo struct {
	Name        string     `json:"name"`
	Id          string     `json:"id"`
	Description string     `json:"description"`
	Version     string     `json:"version"`
	Type        string     `json:"type"`
	Metadata    Metadata   `json:"metadata"` // contains at least ServiceMetadata fields
	Endpoints   []Endpoint `json:"endpoints"`
}

ServiceInfo is the information about a service.

type ServiceMetadata

type ServiceMetadata struct {
	Hostname  string `json:"hostname"`
	MAC       string `json:"mac"`
	Platform  string `json:"platform"`
	Prefix    string `json:"prefix"`
	StartedAt string `json:"started_at"`
}

ServiceMetadata is the metadata about a service.

func NewServiceMetadata

func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, error)

NewServiceMetadata creates and fills a ServiceMetadata structure

func (*ServiceMetadata) Meta

func (smd *ServiceMetadata) Meta() Metadata

Meta returns ServiceMetaData as map[string]string

type ServiceRunner

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

ServiceRunner is a struct that runs one or more services.

func NewServiceRunner

func NewServiceRunner(nc *nats.Conn, log *slog.Logger, prefix string) *ServiceRunner

func (*ServiceRunner) Start

func (sr *ServiceRunner) Start(ctx context.Context, svc PlugisServiceIntf)

Start starts a service.

func (*ServiceRunner) Wait

func (sr *ServiceRunner) Wait()

Wait waits for all services to finish.

Directories

Path Synopsis
example
echoService/cmd command
pkg

Jump to

Keyboard shortcuts

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