transporthelper

package
v0.0.0-...-7b190fc Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 19 Imported by: 4

README

Transport Helper

This package provides helpers for creating gRPC & HTTP servers using go-kit and protoc auto-generates gateways.

Architecture

The API-Gateways autogenerated using protoc are used for starting up both for gRPC and HTTP servers.

The default server configuration is provided in server.go, while the default got-kit transport layer configuration is provided in kitapi.go

Middleware can be attached on three levels:

  • go-kit Endpoints (kitapi.go): Middleware on this level runs on both servers (gRPC & HTTP)
  • gRPC Server UnaryInterceptor (server.go): On this level, the given middleware only runs on the request coming on gRPC server
  • HTTP Server Mux (server.go): On this level, the given middleware only runs on the request coming on HTTP server
flowchart TD
    API["Transport Layer (transport.go)"]
    GOKIT["gokit Endpoint & Middleware (kitapi.go)"]
    Service["Service (service.go)"]
    PCGRPC["protoc gRPC gateway (.pb.gw.go)"]
    PCHTTP["protoc HTTP gateway (.pb.gw.go)"]
    ConfGRPC["GRPC config (server.go)"]
    ConfHTTP["HTTP config (server.go)"]

    PCGRPC --> ConfGRPC
    PCGRPC --> API
    PCHTTP --> API
    PCHTTP --> ConfHTTP
    API --> GOKIT
    GOKIT --> Service

Documentation

Index

Constants

View Source
const (
	MdKeyGRPCMethod    = "grpc_method"
	MdKeyHTTPMethod    = "http_method"
	MdKeyHTTPHost      = "http_host"
	MdKeyHTTPPath      = "http_path"
	MdKeyHTTPRemote    = "http_remote"
	MdKeyHTTPRequestID = "request_id"

	HTTPHdrRequestID = "X-Request-Id"
)

Variables

View Source
var (
	ErrReqInvalidType = status.Error(codes.Internal, "invalid request type")
	ErrReqInvalid     = status.Error(codes.InvalidArgument, "request validation failed")
)

Errors

Functions

func CreateJSONBodyMarshaler

func CreateJSONBodyMarshaler() *runtime.HTTPBodyMarshaler

func DefaultAPI

func DefaultAPI[REQ, RES any](ctx context.Context, req *REQ, serviceFunc ServiceFunc[REQ, RES], middlewareOptions ...endpoint.Middleware) (*RES, error)

DefaultAPI is a convenience function for handling API requests.

func DefaultEP

func DefaultEP[REQ, RES any](ctx context.Context, serviceFunc ServiceFunc[REQ, RES]) endpoint.Endpoint

DefaultEP returns an endpoint that calls the service function.

func DefaultEPCall

func DefaultEPCall[RES any](ctx context.Context, ep endpoint.Endpoint, req interface{}) (*RES, error)

DefaultEPCall validates the request and calls the given handler.

func GetReqIdFromCtx

func GetReqIdFromCtx(ctx context.Context) string

func ValidateReq

func ValidateReq(req interface{}) error

ValidateReq runs validation on the proto request object.

func WithLoggingEPMiddleware

func WithLoggingEPMiddleware() endpoint.Middleware

WithLoggingEPMiddleware returns a middleware that logs API calls.

func WithReqValidatorEPMiddleware

func WithReqValidatorEPMiddleware() endpoint.Middleware

WithReqValidatorEPMiddleware returns a middleware that validates the request.

Types

type CtxKey

type CtxKey string

type Server

type Server struct {
	HTTPMux    *runtime.ServeMux
	HTTPMw     []func(http.Handler) http.Handler
	GRPCServer *grpc.Server
	Options    ServerOptions
}

Server represents the server object.

func NewServer

func NewServer(options ServerOptions) *Server

NewServer creates a new instance of the Server.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run starts the servers and blocks until all are running.

type ServerOptions

type ServerOptions struct {
	HTTPPort int
	GRPCPort int
}

ServerOptions holds the server configuration options.

type ServiceFunc

type ServiceFunc[REQ, RES any] func(context.Context, *REQ) (*RES, error)

ServiceFunc represents the function signature for a service.

Jump to

Keyboard shortcuts

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