Documentation
¶
Overview ¶
Package server provides the go-orb server. It is responsible for managing entrypoints.
Index ¶
- Constants
- Variables
- type Config
- type ConfigOption
- type Entrypoint
- type EntrypointConfig
- type EntrypointConfigType
- type EntrypointNew
- type EntrypointProvider
- type Middleware
- type MiddlewareCallHandler
- type MiddlewareConfig
- type MiddlewareProvider
- type MiddlewareStreamHandler
- type Option
- type RegistrationFunc
- type Server
- func New(name string, version string, configData map[string]any, logger log.Logger, ...) (Server, error)
- func Provide(svcCtx *cli.ServiceContextWithConfig, components *types.Components, ...) (Server, error)
- func ProvideNoOpts(svcCtx *cli.ServiceContextWithConfig, components *types.Components, ...) (Server, error)
Constants ¶
const ComponentType = "server"
ComponentType is the server component type name.
const EntrypointType = "server.Entrypoint"
EntrypointType will be returned by each entrypoint on Type().
Variables ¶
var ( ErrUnknownMiddleware = errors.New("unknown middleware") ErrUnknownHandler = errors.New("unknown handler") ErrUnknownEntrypoint = errors.New("unknown entrypoint") )
Errors.
var ( // Plugins is the plugins container for registry. Plugins = container.NewMap[string, EntrypointProvider]() // PluginsNew holds the New function of Entrypoints, it's here // to create entrypoints from given configs. PluginsNew = container.NewMap[string, EntrypointNew]() // Handlers is a container of registration functions that can be used to // dynamically configure entrypoints. // // You need to register your handlers with a registration function to use it. // Example: // Handlers.Register("myHandler", RegisterEchoHandler) Handlers = container.NewMap[string, RegistrationFunc]() )
var DefaultConfigSection = "server" //nolint:gochecknoglobals
DefaultConfigSection is the section key used in config files used to configure the server options.
var ( // Middlewares is the map of Middlewares available for servers. Middlewares = container.NewSafeMap[string, MiddlewareProvider]() //nolint:gochecknoglobals )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Middlewares []MiddlewareConfig `json:"middlewares,omitempty" yaml:"middlewares,omitempty"`
Handlers []string `json:"handlers,omitempty" yaml:"handlers,omitempty"`
Entrypoints map[string]EntrypointConfig `json:"entrypoints,omitempty" yaml:"entrypoints,omitempty"`
// contains filtered or unexported fields
}
Config is the global config for servers.
func NewConfig ¶
func NewConfig(opts ...ConfigOption) Config
NewConfig creates a new config struct with the given opts.
type ConfigOption ¶ added in v0.1.0
type ConfigOption func(*Config)
ConfigOption allows to set options for MyConfig.
func WithEntrypointConfig ¶ added in v0.1.0
func WithEntrypointConfig(epName string, config EntrypointConfigType) ConfigOption
WithEntrypointConfig allows you to create an entrypoint functionally.
type Entrypoint ¶
type Entrypoint interface {
types.Component
// Name returns the entrypoints name.
Name() string
// Enabled returns if this entrypoint has been enabled in config.
Enabled() bool
// AddHandler adds a handler for registration during startup.
AddHandler(fun RegistrationFunc)
// Register is used to register handlers.
//
// A registration function takes a pointer to the server, which can then
// be used to register handlers in the server specific way.
Register(fun RegistrationFunc)
// Transport returns the client transport that is required to talk to this entrypoint.
Transport() string
// Network returns the network the entrypoint is listening on.
Network() string
// Address returns the address the entrypoint is listening on.
Address() string
}
Entrypoint is a server, and represents an entrypoint into the web.
type EntrypointConfig ¶
type EntrypointConfig struct {
Plugin string `json:"plugin" yaml:"plugin"`
Enabled bool `json:"enabled" yaml:"enabled"`
OptMiddlewares []Middleware `json:"-" yaml:"-"`
OptHandlers []RegistrationFunc `json:"-" yaml:"-"`
}
EntrypointConfig is the base config for all entrypoints.
func NewEntrypointConfig ¶ added in v0.3.0
func NewEntrypointConfig(opts ...Option) *EntrypointConfig
NewEntrypointConfig creates a new entrypoint config with the given opts.
type EntrypointConfigType ¶ added in v0.1.0
type EntrypointConfigType interface {
// contains filtered or unexported methods
}
EntrypointConfigType is here so we can do magic work on options.
type EntrypointNew ¶ added in v0.1.0
type EntrypointNew func( name string, version string, epName string, acfg any, logger log.Logger, reg registry.Type, ) (Entrypoint, error)
EntrypointNew is the function type to create a new entrypoint.
type EntrypointProvider ¶ added in v0.1.0
type EntrypointProvider func( name string, version string, epName string, configData map[string]any, logger log.Logger, reg registry.Type, opts ...Option, ) (Entrypoint, error)
EntrypointProvider is the function type to create a new entrypoint. It should create a new config, configure it the run EntrypointFromConfig with it.
type Middleware ¶ added in v0.1.0
type Middleware interface {
// Start/Stop will be called many times, if you require the call once, you have to ensure it on your own.
types.Component
Call(next MiddlewareCallHandler) MiddlewareCallHandler
}
Middleware is an interface that must be implemented by server Middlewares.
type MiddlewareCallHandler ¶ added in v0.1.0
MiddlewareCallHandler is the Handler for unary RPC Calls.
type MiddlewareConfig ¶ added in v0.1.0
type MiddlewareConfig struct {
Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"`
}
MiddlewareConfig is the base config for all middlewares.
type MiddlewareProvider ¶ added in v0.1.0
type MiddlewareProvider func( configSection []string, configKey string, configData map[string]any, logger log.Logger, ) (Middleware, error)
MiddlewareProvider is the provider for a middleware, each Middleware must supply this to register itself.
type MiddlewareStreamHandler ¶ added in v0.1.0
MiddlewareStreamHandler is the handler for streaming RPC calls.
type Option ¶
type Option func(EntrypointConfigType)
Option is a functional entrypoint option.
func WithEntrypointDisabled ¶ added in v0.3.0
func WithEntrypointDisabled() Option
WithEntrypointDisabled disables an entrypoint.
func WithEntrypointHandlers ¶ added in v0.3.0
func WithEntrypointHandlers(hs ...RegistrationFunc) Option
WithEntrypointHandlers appends the given handlers.
func WithEntrypointMiddlewares ¶ added in v0.3.0
func WithEntrypointMiddlewares(mws ...Middleware) Option
WithEntrypointMiddlewares appends the given middlewares.
func WithEntrypointPlugin ¶ added in v0.3.0
WithEntrypointPlugin sets the plugin of the entrypoint.
type RegistrationFunc ¶
type RegistrationFunc func(srv any)
RegistrationFunc is executed to register a handler to a server (entrypoint) passed as srv. srv can be of any of the various server types, should be a pointer.
You can write your own custom registration functions to register extra handlers.
Inside the registration function, you need to convert the server type and assert that you are working with the server type you are expecting, and otherwise no-op. For an example, see the implementation of NewRegistrationFunc.
func NewRegistrationFunc ¶
func NewRegistrationFunc[Tsrv any, Thandler any](register func(Tsrv, Thandler), handler Thandler) RegistrationFunc
NewRegistrationFunc takes a registration function and handler and returns a registration func that can be used with one specific server type.
This function is useful if a user wants to register a non-micro project proto handler. For any internal proto services generated in your project you will already have a pre-defined registration function which only needs the handler implementation.
type Server ¶ added in v0.1.0
type Server struct {
// contains filtered or unexported fields
}
Server is responsible for managing entrypoints. Entrypoints are the actual servers that bind to a port and accept connections. Entrypoints can be dynamically configured.
For more info look at the entrypoint types.
func New ¶ added in v0.3.0
func New( name string, version string, configData map[string]any, logger log.Logger, reg registry.Type, opts ...ConfigOption, ) (Server, error)
New creates a new server.
func Provide ¶ added in v0.1.0
func Provide( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, reg registry.Type, opts ...ConfigOption, ) (Server, error)
Provide creates a new server.
func ProvideNoOpts ¶ added in v0.3.0
func ProvideNoOpts( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, reg registry.Type, ) (Server, error)
ProvideNoOpts creates a new server without functional options.
func (*Server) GetEntrypoint ¶ added in v0.1.0
func (s *Server) GetEntrypoint(name string) (Entrypoint, error)
GetEntrypoint returns the requested entrypoint, if present.
func (*Server) GetEntrypoints ¶ added in v0.3.0
func (s *Server) GetEntrypoints() *container.Map[string, Entrypoint]
GetEntrypoints returns a map of entrypoints.