apiservice

package
Version: v0.0.0-...-a54a33a Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: AGPL-3.0 Imports: 10 Imported by: 21

Documentation

Overview

Package apiservice provides a simple system to instantiate clients of the luIDS api.

This package is a work in progress and makes no API stability promises.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterBuilder

func RegisterBuilder(api string, builder BuildFn)

RegisterBuilder registers a service builder for an api signature.

Types

type Autoloader

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

Autoloader implements Discover using a lazy build.

func NewAutoloader

func NewAutoloader(defs []ServiceDef, opt ...AutoloaderOption) *Autoloader

NewAutoloader creates a new Autoloader with service definitions.

func (*Autoloader) CloseAll

func (a *Autoloader) CloseAll() error

CloseAll registered services.

func (*Autoloader) GetService

func (a *Autoloader) GetService(id string) (Service, bool)

GetService implements Discover interface.

func (*Autoloader) ListServices

func (a *Autoloader) ListServices() []string

ListServices implements Discover interface.

func (*Autoloader) Ping

func (a *Autoloader) Ping() error

Ping all registered services.

type AutoloaderOption

type AutoloaderOption func(*autoOptions)

AutoloaderOption is used for Autoloader configuration.

func SetLogger

func SetLogger(l yalogi.Logger) AutoloaderOption

SetLogger option allows set a custom logger.

type BuildFn

type BuildFn func(def ServiceDef, logger yalogi.Logger) (Service, error)

BuildFn defines a function that constructs a service using a service definition.

type Discover

type Discover interface {
	// GetService by id, returning false if not available
	GetService(id string) (Service, bool)
	// ListServices returns the ids of services available
	ListServices() []string
}

Discover interface used for service discovering.

type Registry

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

Registry stores service items indexed by an id. Implements Discover interface.

func NewRegistry

func NewRegistry() *Registry

NewRegistry instantiates a new registry.

func (*Registry) CloseAll

func (r *Registry) CloseAll() error

CloseAll registered services.

func (*Registry) GetService

func (r *Registry) GetService(id string) (Service, bool)

GetService implements Discover interface.

func (*Registry) ListServices

func (r *Registry) ListServices() []string

ListServices implements Discover interface.

func (*Registry) Ping

func (r *Registry) Ping() error

Ping all registered services.

func (*Registry) Register

func (r *Registry) Register(id string, svc Service) error

Register a service using an id.

type Service

type Service interface {
	// API returns signature
	API() string
	// Close client
	Close() error
	// Ping status
	Ping() error
}

Service is the interface that must be implemented by service API clients.

func Build

func Build(def ServiceDef, logger yalogi.Logger) (Service, error)

Build creates a new service using a service definition struct.

type ServiceDef

type ServiceDef struct {
	// ID must exist and be unique for its correct operation
	ID string `json:"id"`
	// Disabled
	Disabled bool `json:"disabled,omitempty"`
	// API defines the api implemented by the service
	API string `json:"api"`
	// Endpoint url
	Endpoint string `json:"endpoint"`
	// Client configuration
	Client *grpctls.ClientCfg `json:"client,omitempty"`
	// Enable log
	Log bool `json:"log,omitempty"`
	// Enable metrics
	Metrics bool `json:"metrics,omitempty"`
	// Enable cache
	Cache bool `json:"cache,omitempty"`
	// Opts stores custom fields
	Opts map[string]interface{} `json:"opts,omitempty"`
}

ServiceDef is used for define and construct microservices

func ServiceDefsFromFile

func ServiceDefsFromFile(path string) ([]ServiceDef, error)

ServiceDefsFromFile reads from file a slice of ServiceDef.

func (ServiceDef) ClientCfg

func (def ServiceDef) ClientCfg() grpctls.ClientCfg

ClientCfg returns a copy of client configuration. It returns an empty struct if a null pointer is stored.

func (ServiceDef) Validate

func (def ServiceDef) Validate() error

Validate checks field values.

Jump to

Keyboard shortcuts

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