Documentation ¶
Overview ¶
This lightweight gRPC proxy was based on the work: https://github.com/mwitkow/grpc-proxy I am grateful for publishing that package, helped me a lot! Thanks for the authors!
Index ¶
- Constants
- Variables
- type Context
- type ContextKey
- type Gateway
- func (gw *Gateway) Delete(url string, handler HandlerFunc) Route
- func (gw *Gateway) Get(url string, handler HandlerFunc) Route
- func (gw *Gateway) GetService(name string) (Service, error)
- func (gw *Gateway) Head(url string, handler HandlerFunc) Route
- func (gw *Gateway) ListenForMocks(_ *[]any)
- func (gw *Gateway) Post(url string, handler HandlerFunc) Route
- func (gw *Gateway) Put(url string, handler HandlerFunc) Route
- func (gw *Gateway) RegisterMiddleware(mw ...Middleware) error
- func (g *Gateway) RegisterService(conf *ServiceConfig) error
- func (gw *Gateway) Start()
- type GatewayConfig
- type GatewayInfo
- type GatewayOptionFunc
- func ReadConfig(path string) ([]GatewayOptionFunc, error)
- func WithAddress(address int) GatewayOptionFunc
- func WithDisabledLoggers(disabled logTypeValue) GatewayOptionFunc
- func WithGrpcProxy(addr int) GatewayOptionFunc
- func WithHealthCheckFrequency(t time.Duration) GatewayOptionFunc
- func WithMiddlewaresEnabled(val runLevel) GatewayOptionFunc
- func WithProductionLevel(val runLevel) GatewayOptionFunc
- func WithSecretKey(key string) GatewayOptionFunc
- func WithService(conf *ServiceConfig) GatewayOptionFunc
- type GrpcProxyConfig
- type HandlerFunc
- type LoggerConfig
- type Middleware
- type MiddlewareFunc
- type Route
- type Service
- type ServiceConfig
- type ServiceInfo
Constants ¶
const ( IncomingDecodedKey ContextKey = "incomingDecoded" X_GW_HEADER_KEY string = "X-GATEWAY-KEY" )
const ( JsonContentType = "application/json" JsonContentTypeUTF8 = JsonContentType + "; charset=UTF-8" TextHtmlContentType = "text/html" XmlContentType = "application/xml" )
const ( StateRegistered serviceState = iota StateUnknown StateRefused StateAvailable )
const Version = "v0.5.3"
Variables ¶
var (
ErrServiceNotExists = errors.New("[registry]: service not exists")
)
Functions ¶
This section is empty.
Types ¶
type ContextKey ¶ added in v0.5.0
type ContextKey = gorouter.ContextKey
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts ...GatewayOptionFunc) *Gateway
New returns a new instance of the gateway decorated with the given opts.
func NewFromConfig ¶ added in v0.3.0
NewFromConfig creates and returns a new Gateway based on the given config file path. In case of any errors – due to IO reading or marshal error – it returns the error also.
func (*Gateway) Delete ¶ added in v0.3.0
func (gw *Gateway) Delete(url string, handler HandlerFunc) Route
Delete registers a custom route with method @DELETE.
func (*Gateway) Get ¶
func (gw *Gateway) Get(url string, handler HandlerFunc) Route
Get registers a custom route with method @GET.
func (*Gateway) GetService ¶
GetService searches for a service by its name. Returns error if, there is no service by the given name.
func (*Gateway) Head ¶ added in v0.3.0
func (gw *Gateway) Head(url string, handler HandlerFunc) Route
Head registers a custom route with method @HEAD.
func (*Gateway) Post ¶
func (gw *Gateway) Post(url string, handler HandlerFunc) Route
Post registers a custom route with method @POST.
func (*Gateway) Put ¶ added in v0.3.0
func (gw *Gateway) Put(url string, handler HandlerFunc) Route
Put registers a custom route with method @PUT.
func (*Gateway) RegisterMiddleware ¶
func (gw *Gateway) RegisterMiddleware(mw ...Middleware) error
RegisterMiddleware registers a middleware instance to the gateway.
func (*Gateway) RegisterService ¶ added in v0.3.0
func (g *Gateway) RegisterService(conf *ServiceConfig) error
RegisterService creates and registers a new Service to the registry based on the given config. In case of validation error or duplicate service, it returns error.
type GatewayConfig ¶ added in v0.3.0
type GatewayConfig struct { Address int `json:"address"` MiddlewaresEnabled *runLevel `json:"middlewaresEnabled"` ProductionLevel *runLevel `json:"productionLevel"` SecretKey string `json:"secretKey"` HealthCheckInterval string `json:"healthCheckInterval"` TimeOutSec int `json:"timeOutSec"` LoggerConfig *LoggerConfig `json:"loggerConfig"` GrpcProxy *GrpcProxyConfig `json:"grpcProxy"` Services []*ServiceConfig `json:"services"` }
type GatewayInfo ¶ added in v0.3.0
type GatewayInfo struct {
// contains filtered or unexported fields
}
type GatewayOptionFunc ¶ added in v0.3.0
type GatewayOptionFunc = func(*Gateway)
func ReadConfig ¶ added in v0.3.0
func ReadConfig(path string) ([]GatewayOptionFunc, error)
ReadConfig reads the JSON config from given path, then returns it as a slice of GatewayOptionFunc, which can be passed into the New factory. In case of unexpected behaviour, it returns error.
func WithAddress ¶ added in v0.3.0
func WithAddress(address int) GatewayOptionFunc
func WithDisabledLoggers ¶ added in v0.3.4
func WithDisabledLoggers(disabled logTypeValue) GatewayOptionFunc
func WithGrpcProxy ¶ added in v0.4.0
func WithGrpcProxy(addr int) GatewayOptionFunc
func WithHealthCheckFrequency ¶ added in v0.3.0
func WithHealthCheckFrequency(t time.Duration) GatewayOptionFunc
func WithMiddlewaresEnabled ¶ added in v0.3.0
func WithMiddlewaresEnabled(val runLevel) GatewayOptionFunc
func WithProductionLevel ¶ added in v0.3.0
func WithProductionLevel(val runLevel) GatewayOptionFunc
func WithSecretKey ¶ added in v0.3.0
func WithSecretKey(key string) GatewayOptionFunc
func WithService ¶ added in v0.3.0
func WithService(conf *ServiceConfig) GatewayOptionFunc
type GrpcProxyConfig ¶ added in v0.4.0
type GrpcProxyConfig struct {
Address int `json:"address"`
}
type HandlerFunc ¶ added in v0.3.0
type HandlerFunc = gorouter.HandlerFunc
type LoggerConfig ¶ added in v0.3.4
type LoggerConfig struct {
DisabledLoggers []logTypeName `json:"disabledLoggers"`
}
type Middleware ¶ added in v0.3.0
type Middleware = gorouter.Middleware
type MiddlewareFunc ¶ added in v0.3.0
type MiddlewareFunc = gorouter.MiddlewareFunc
type Service ¶ added in v0.3.0
type Service interface { Handle(Context) Get(string, ...http.Header) (*http.Response, error) Post(string, []byte, ...http.Header) (*http.Response, error) Put(string, []byte, ...http.Header) (*http.Response, error) Delete(string, ...http.Header) (*http.Response, error) GetConfig() *ServiceConfig GetAddressWithProtocol() string GetAddress() string }
type ServiceConfig ¶ added in v0.3.0
type ServiceConfig struct { // See the type def. ServiceType serviceType `json:"serviceType"` // The name of the service, must be unique. Name string `json:"name"` // The unique prefix which identified a URL Prefix string `json:"prefix"` // Which protocol is used to call the service. // Only http and https are supported. Protocol string `json:"protocol"` // The basic host:port format, where each service listens at. Host string `json:"host"` Port string `json:"port"` // How many seconds it should wait before timeout. TimeOutSec int `json:"timeOutSec"` // The url to call for healtcheck. StatusPath string `json:"statusPath"` }
type ServiceInfo ¶ added in v0.3.0
type ServiceInfo struct { *ServiceConfig State string `json:"state"` }