grpcApi

package
v0.0.0-...-ccecdc3 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GrpcLogger

func GrpcLogger(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (resp interface{}, err error)

** GRPC INTERCEPTOR ** GRPC interceptor to log requests in the terminal UnaryServerInterceptor: func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (resp any, err error)

func HttpLogger

func HttpLogger(handler http.Handler) http.Handler

* HTTP MIDDLEWARE The above GrpcLogger middleware will work only for gRPC requests. If we try to send an HTTP request to the gateway server, we won't see any logs written. That's because we're using in-process translation on our gateway server . So the gateway will call the RPC handler function directly without going through any interceptors. If we run the gateway as a separate server, And use cross-process translation to call the gRPC server via a network call, Then the logs will show up in the gRPC server as normal. But that's another network hop that can increase the duration of the request. So, if we still want to keep using in-process translation, we will have to write a separate HTTP middleware to log the HTTP requests.

func NewServer

func NewServer(config utils.Config, store generated_db.Store) (*server, error)

NewServer creates a new gRPC server.

Types

type Metadata

type Metadata struct {
	UserAgent string
	ClientIP  string
}

type ResponseRecorder

type ResponseRecorder struct {
	http.ResponseWriter // embed the oroginal http.ResponseWriter
	// this StatusCode field will be updated to the correct value when the
	// WriteHeader method is called by the handler function.
	StatusCode int
	// In case of the error, it will be written to the response body
	// so we need to track the response body as well.
	Body []byte
}

ResponseRecorder struct that implements the http.ResponseWriter interface to track the status code and response body of the HTTP response from the handler.

func (*ResponseRecorder) Write

func (rec *ResponseRecorder) Write(body []byte) (int, error)

func (*ResponseRecorder) WriteHeader

func (rec *ResponseRecorder) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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