feathers

package
v0.0.0-...-43028c6 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddStructDecodeHookFunc

func AddStructDecodeHookFunc(f mapstructure.DecodeHookFunc)

func ConfigureHttpProvider

func ConfigureHttpProvider(app *App, config map[string]interface{}) error

Use this in combination with `App.Configure` to be able to listen for http requests

func ConfigureSocketIOProvider

func ConfigureSocketIOProvider(app *App, config map[string]interface{}) error

ConfigureSocketIOProvider registers a new socketio provider in app

func Contains

func Contains(s []string, e string) bool

Contains checks if a string slice contains another string

func LoadConfigDirectory

func LoadConfigDirectory(dirPath string) (map[string]map[string]interface{}, error)

LoadConfigDirectory loads a whole directory of .yaml config files. DEPRECATED! use config.LoadConfigMap instead

func LoadConfigFile

func LoadConfigFile(path string) (map[string]interface{}, error)

LoadConfigFile loads a yaml config file DEPRECATED! use config.LoadConfigFile instead

func MapToStruct

func MapToStruct(data map[string]interface{}, target interface{}) error

MapToStruct maps service data into a struct (passed by pointer)

Example: ```` model := Model{} err := MapToStruct(data, &model) ````

func RequestVars

func RequestVars(request http.Request) (requestRegistration, error)

RequestVars parses a http request and extracts service related information

func StructToMap

func StructToMap(data interface{}) (map[string]interface{}, error)

StructToMap converts a model struct into an interface

func ToMap

func ToMap(result interface{}, errs ...error) (map[string]interface{}, error)

func ToMapSlice

func ToMapSlice(result interface{}, errs ...error) ([]map[string]interface{}, error)

Types

type App

type App struct {
	*EventEmitter
	// contains filtered or unexported fields
}

App is the feathers-go applications instance. Instanciate through NewApp

func NewApp

func NewApp() *App

NewApp returns a new feathers-go app instance

func (*App) AddProvider

func (a *App) AddProvider(name string, provider Provider) error

AddProvider adds a network provider to the app.

Primarily called through modules Providers can listen for client connections and are also handling requests

func (*App) AddService

func (a *App) AddService(name string, service Service)

AddService registers a new service for the application

func (*App) Close

func (a *App) Close() error

func (*App) Config

func (a *App) Config(key string) (interface{}, bool)

Config retrieves a config key from the applications config

func (*App) Configure

func (a *App) Configure(appModule AppModule, config map[string]interface{})

Configure configures modules similar to the original feathers api.

Modules can registers Services, Providers etc.

func (*App) HandleRequest

func (a *App) HandleRequest(provider string, method RestMethod, c Caller, service string, data map[string]interface{}, id string, query map[string]interface{})

HandleRequest handles a request received by a provider. It starts the pipeline and schedules tasks

func (*App) Listen

func (a *App) Listen()

Listen makes the app listen to the port specified in the configuration

func (*App) LoadConfig

func (a *App) LoadConfig() error

LoadConfig loads configuration files from `./config directory`

By default `default.yaml` file is loaded.

It also looks for a environment variable named `APP_ENV`. If it is specified it looks for a config file named after the environment and merges it with default configuration Configuration can be retrieved by `GetConfig`. Keys kan be set and overwritten by `SetConfig` Example: In case of `APP_ENV=development`, looks for `development.yaml` and if it exists merges it with APP_ENV

func (*App) PublishToProviders

func (a *App) PublishToProviders(room string, event string, data interface{}, path string, publishProvider string)

PublishToProviders publishes a event to all providers. Each can decide what to do with the publish by themselfs

func (*App) Service

func (a *App) Service(name string) Service

Service returns a a wrapped service instance of a service for easy calling methods

Wrapping is necessary because otherwise hooks will not be triggered If service does not exist returns nil

func (*App) ServiceClass

func (a *App) ServiceClass(name string) interface{}

ServiceClass returns a service instance as an interface{}

This is useful for parsing a service to a specific interface or struct for calling custom service methods

func (*App) SetAppHooks

func (a *App) SetAppHooks(hookTree HooksTree)

SetAppHooks specifies the HookTree of the application (Similar to feathers-go)

func (*App) SetConfig

func (a *App) SetConfig(key string, value interface{})

SetConfig sets a config key in the applications config

func (*App) Shutdown

func (a *App) Shutdown(ctx context.Context) error

func (*App) TriggerUpdate

func (a *App) TriggerUpdate(ctx *Context)

type AppModule

type AppModule = func(app *App, config map[string]interface{}) error

AppModules is a module which can configure the application

Are supposed to be passed to Configure method of App

type BasePublishableService

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

BasePublishableService is a basic implementation of PublishableService

func NewBasePublishableService

func NewBasePublishableService() *BasePublishableService

func (*BasePublishableService) BeforePublish

func (s *BasePublishableService) BeforePublish(topic string, data interface{}, ctx *Context) (interface{}, error)

BeforePublish is called before data is published to a topic. data can be manipulated at this point

func (*BasePublishableService) Publish

func (s *BasePublishableService) Publish(topic string, data interface{}, ctx *Context) ([]string, error)

Publish calls PublishHandler if registerd and publishes data to returned topics

func (*BasePublishableService) RegisterPublishHandler

func (s *BasePublishableService) RegisterPublishHandler(topic string, handler PublishHandler)

RegisterPublishHandler registers a new handler for a topic

type BaseService

type BaseService struct {
	Hooks HooksTree
	// contains filtered or unexported fields
}

BaseService (every service should extend from this)

func (*BaseService) HookTree

func (b *BaseService) HookTree() HooksTree

HookTree returns hook tree of service

func (*BaseService) Name

func (b *BaseService) Name() string

type BoolHook

type BoolHook = func(ctx *Context) (bool, error)

BoolHook works just like a hook but returns a boolean and cannot modify the context

type Caller

type Caller interface {
	Callback(data interface{})
	CallbackError(err error)
	IsSocket() bool
	SocketConnection() Connection
}

Caller represents a caller of a request. It handles Callbacks

type Connection

type Connection interface {
	Join(room string) error
	Leave(room string) error
	IsAuthenticated() bool
	AuthEntity() interface{}
	SetAuthEntity(interface{})
}

type Context

type Context struct {
	context.Context
	// App is a reference to the current application instance
	App App
	// Data is the data passed from the requesting instance
	Data Data
	// Error contains the error which was triggered while executing the route
	Error error
	// ID is the id passed fromt the requesting instance
	ID string
	// Method containts the method which was called (Get, Patch, Find, etc.)
	Method RestMethod
	// Path is the path to the service
	Path string
	// Result is the result of the service call (only defined in after hooks)
	Result interface{}
	// Service is the called service, but wrapped by the application to also trigger hooks (If you wanna call service class directly for custom methods use ServiceClass)
	Service Service
	// ServiceClass is the current service without the wrapper. Do NOT Call Patch, Find, Get, Remove and Update since they do not call hooks!
	ServiceClass interface{}
	StatusCode   int
	// Type of the hook (Before or after)
	Type HookType
	// Params for this call
	Params Params
}

Context is the context which is passed to every go-feathers hook (Can be used as context.Context)

func (*Context) DataDecode

func (c *Context) DataDecode(target interface{}, path ...string) error

DataDecode decodes data at `path` to target (pointer)

func (*Context) DataGet

func (c *Context) DataGet(key ...string) interface{}

func (*Context) DataHas

func (c *Context) DataHas(key ...string) bool

func (*Context) DataMerge

func (c *Context) DataMerge(data Data)

DataMerge merges new data with already exsting data

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

type Data

type Data = map[string]interface{}

type Defaulter

type Defaulter interface {
	SetDefaults()
}

type EventEmitter

type EventEmitter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewEventEmitter

func NewEventEmitter() *EventEmitter

func (*EventEmitter) Emit

func (el *EventEmitter) Emit(event string, data interface{})

func (*EventEmitter) On

func (el *EventEmitter) On(event string) (<-chan interface{}, EventListenerUnregister)

func (*EventEmitter) Once

func (el *EventEmitter) Once(event string) <-chan interface{}

type EventListenerUnregister

type EventListenerUnregister = func() bool

type Hook

type Hook = func(ctx *Context) error

Hook is a function which can be used to modify request params

type HookType

type HookType string

HookType is either `Before, After or Error` and describes the current type of the hook chain

const (
	// Before Hooks are executed before the service method
	Before HookType = "before"
	// After Hooks are executed after service method
	After HookType = "after"
	// Error Hooks are executed if a hook or service method returns a error
	Error HookType = "error"
)

func (HookType) String

func (c HookType) String() string

type HooksTree

type HooksTree struct {
	//Before hooks are executed before service method
	Before HooksTreeBranch
	// After hooks are executed after service method
	After HooksTreeBranch
	// Error hooks are executed in case hook or service method returns error
	Error HooksTreeBranch
}

HooksTree is the complete hooks definition of a service or the application

func (*HooksTree) Append

func (t *HooksTree) Append(branchType HookType, method RestMethod, hooks []Hook)

func (HooksTree) Branch

func (t HooksTree) Branch(branchType HookType) HooksTreeBranch

type HooksTreeBranch

type HooksTreeBranch struct {
	// All hooks are executed every time before route specific hooks are executed
	All []Hook
	// Find hooks are executed for find calls
	Find []Hook
	// Get hooks are executed for get calls
	Get []Hook
	// Create hooks are executed for create calls
	Create []Hook
	// Patch hooks are executed for patch calls
	Patch []Hook
	// Update hooks are executed for update calls
	Update []Hook
	// Remove hooks are executed for remove hooks
	Remove []Hook
}

HooksTreeBranch is a single branch of hooks (e.g. for Before, After or Error)

func (*HooksTreeBranch) Append

func (b *HooksTreeBranch) Append(method RestMethod, hooks []Hook)

func (HooksTreeBranch) Branch

func (b HooksTreeBranch) Branch(method RestMethod) []Hook

type HttpProvider

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

HttpProvider is a provider for feathers-go which listens to http requests

func NewHttpProvider

func NewHttpProvider(app *App) *HttpProvider

NewHttpProvider creates a new http provider (injection to app happens through module: `onfigureHttpProvider`)

func (*HttpProvider) Listen

func (h *HttpProvider) Listen(port int, serveMux *http.ServeMux)

Listen is required by Provider interface. It starts listening for incoming http requests

func (*HttpProvider) Publish

func (p *HttpProvider) Publish(room string, event string, data interface{}, path string, provider string)

func (*HttpProvider) ServeHTTP

func (h *HttpProvider) ServeHTTP(response http.ResponseWriter, request *http.Request)

ServceHttp is implemented from http.Handler. It handles a request

type Mappable

type Mappable interface {
	ToMap() map[string]interface{}
}

type ModelFactory

type ModelFactory = func() interface{}

ModelFactory returns a new instance of the model (used for `ModelService` struct)

type ModelService

type ModelService struct {
	Model ModelFactory
	// contains filtered or unexported fields
}

ModelService is a service which offers model validation and parsing (create new with `NewModelService`)

func NewModelService

func NewModelService(model ModelFactory) *ModelService

Creates a new ModelService based of a existing model

func (*ModelService) MapAndValidate

func (m *ModelService) MapAndValidate(data map[string]interface{}) (interface{}, error)

MapAndValidate is the same as calling `MapToModel` and `ValidateModel`

func (*ModelService) MapAndValidateStruct

func (m *ModelService) MapAndValidateStruct(data map[string]interface{}, target interface{}) error

MapAndValidate is the same as calling `MapToStruct` and `ValidateModel` on the returned struct

func (*ModelService) MapToModel

func (m *ModelService) MapToModel(data map[string]interface{}) (interface{}, error)

MapToModel parses data passed to a service and returns a model instance

func (*ModelService) StructToMap

func (m *ModelService) StructToMap(data interface{}) (map[string]interface{}, error)

StructToMap converts a model struct into an interface

func (*ModelService) ValidateModel

func (m *ModelService) ValidateModel(model interface{}) error

ValidateModel validates a model based on its validation rules

type NewParamsOpt

type NewParamsOpt = func(params *Params)

func WithOption

func WithOption(option string, value interface{}) NewParamsOpt

func WithQuery

func WithQuery(query Query) NewParamsOpt

type Params

type Params struct {
	Params map[string]interface{}
	// Name of provider from which is called from (empty string for server)
	Provider string
	// Route which is called (service name)
	Route string
	//Caller instance of who has called this
	Connection Connection
	// True if connection is socket based
	IsSocket bool
	// Headers from client call
	Headers map[string]string

	// Query conatains query fields specified by client
	Query Query

	User map[string]interface{}

	Authenticated bool
	// contains filtered or unexported fields
}

Params is the params passed to functions and go-feathers hooks

func NewAuthenticatedParams

func NewAuthenticatedParams(user map[string]interface{}) *Params

func NewParams

func NewParams(opts ...NewParamsOpt) *Params

func NewParamsFrom

func NewParamsFrom(origParams *Params, opts ...NewParamsOpt) *Params

func NewParamsQuery

func NewParamsQuery(query map[string]interface{}) *Params

NewParamsQuery returns a new HookParms struct only containng specified query

func (*Params) Copy

func (hc *Params) Copy() Params

func (*Params) Get

func (hc *Params) Get(key string) interface{}

Get retrieves a field from the hooks

func (*Params) Has

func (hc *Params) Has(key string) bool

func (*Params) Lookup

func (hc *Params) Lookup(key string) (interface{}, bool)

Get retrieves a field from the hooks

func (*Params) New

func (hc *Params) New() Params

func (*Params) NewWithQuery

func (hc *Params) NewWithQuery(query map[string]interface{}) Params

func (*Params) Set

func (hc *Params) Set(key string, value interface{})

Set sets a hook field (e.g. user, additional information etc.)

type Provider

type Provider interface {
	Listen(port int, mux *http.ServeMux)
	Publish(room string, event string, data interface{}, path string, provider string)
}

Provider handles requests and can listen for new connections

type PublishHandler

type PublishHandler = func(data interface{}, ctx *Context) []string

PublishHandler is a function which handles a publish of a service and returns a list of rooms to publish to

type PublishableService

type PublishableService interface {
	RegisterPublishHandler(topic string, handler PublishHandler)
	Publish(topic string, data interface{}, ctx *Context) ([]string, error)
	BeforePublish(topic string, data interface{}, ctx *Context) (interface{}, error)
}

PublishableService which can publish events

type Query

type Query = map[string]interface{}

type RestMethod

type RestMethod string

RestMethod represents the method which was called

const (
	// Find method retrieves multiple documents
	Find RestMethod = "find"
	// Get method retrieves a single document
	Get RestMethod = "get"
	// Create method creates a new document
	Create RestMethod = "create"
	// Update method replaces a whole doucmnet
	Update RestMethod = "update"
	// Patch method inserts new keys or updates existing keys
	Patch RestMethod = "patch"
	// Remove method removes a document
	Remove RestMethod = "remove"
	// All only represents a hook branch type
	All RestMethod = "all"
)

func (RestMethod) String

func (c RestMethod) String() string

type Service

type Service interface {
	// Find retrieves multiple entities (`interface{} is a slice`)
	Find(ctx context.Context, params Params) (interface{}, error)
	// Get retrives a single entity
	Get(ctx context.Context, id string, params Params) (interface{}, error)
	// Create creates a new entity should be created
	Create(ctx context.Context, data map[string]interface{}, params Params) (interface{}, error)
	// Update replaces a whole entity
	Update(ctx context.Context, id string, data map[string]interface{}, params Params) (interface{}, error)
	// Patch updates or replaces specified entity keys
	Patch(ctx context.Context, id string, data map[string]interface{}, params Params) (interface{}, error)
	// Remove removes a entity
	Remove(ctx context.Context, id string, params Params) (interface{}, error)

	// HookTree returns the hook tree (mainly uses internally)
	HookTree() HooksTree

	// Name returns the name of the current service
	Name() string
	// contains filtered or unexported methods
}

Service is a callable instance inside feathers which is responslible for a single kind of entity

type Setupable

type Setupable interface {
	Setup(app *App)
}

type SocketIOProvider

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

SocketIOProvider handles socket.io connections and events

func NewSocketIOProvider

func NewSocketIOProvider(app *App, config map[string]interface{}) *SocketIOProvider

Creates a new SocketIOProvider instance (use module `ConfigureSocketIOProvider` with apps `Configure` method)

func (*SocketIOProvider) Handle

func (p *SocketIOProvider) Handle(callMethod RestMethod, caller socketCaller, service string, data map[string]interface{}, id string, query map[string]interface{})

Handle handles a new event to a service

func (*SocketIOProvider) Listen

func (fs *SocketIOProvider) Listen(port int, serveMux *http.ServeMux)

Listen starts listening for new socket.io connections

func (*SocketIOProvider) Publish

func (p *SocketIOProvider) Publish(room string, event string, data interface{}, path string, provider string)

Publish publishes a event to connections subscibed to room

type WritableConnection

type WritableConnection interface {
	Emit(event string, data interface{}) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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