ngrpc

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

README

ngrpc

Go Report Card GitHub license codecov

Utility for gRPC Server and gRPC Gateway

Installing

go get github.com/nbs-go/ngrpc

Usage

TODO

Contributors

Documentation

Index

Constants

View Source
const (
	HeaderRealIP           = "x-real-ip"
	HeaderForwardedFor     = "x-forwarded-for"
	HeaderSubjectId        = "x-subject-id"
	HeaderSubjectFullName  = "x-subject-name"
	HeaderSubjectRole      = "x-subject-role"
	HeaderGatewayUserAgent = "x-gateway-user-agent"
	HeaderRequestId        = "x-request-id"
	HeaderHttpStatus       = "x-http-status"
	HeaderUserAgent        = "user-agent"
	HeaderContentType      = "Content-Type"
)
View Source
const (
	KeyGrpcStatus      = "grpcStatus"
	KeyOverrideMessage = "message"
	KeyErrorMetadata   = "errorMetadata"
)
View Source
const (
	ResponseCodeSuccess    = "OK"
	ResponseMessageSuccess = "Success"
)
View Source
const (
	GrpcMetadataHttpStatus  = "Grpc-Metadata-X-Http-Status"
	GrpcMetadataContentType = "Grpc-Metadata-Content-Type"
	GrpcMetadataRequestId   = "Grpc-Metadata-X-Request-Id"
)

Variables

View Source
var AnonymousUser = Subject{
	Id:       "ANON",
	FullName: "Anonymous User",
	Role:     "USER",
}
View Source
var BadRequestError = b.NewError("400", "Bad Request",
	WithStatus(codes.InvalidArgument),
)
View Source
var CancelError = b.NewError("408", "Request Canceled",
	WithStatus(codes.Canceled),
)
View Source
var File_error_details_proto protoreflect.FileDescriptor
View Source
var ForbiddenError = b.NewError("403", "Forbidden",
	WithStatus(codes.PermissionDenied),
)
View Source
var InternalError = b.NewError("500", "Internal Error",
	WithStatus(codes.Internal),
)
View Source
var NotFoundError = b.NewError("404", "Not Found",
	WithStatus(codes.NotFound),
)
View Source
var UnauthorizedError = b.NewError("401", "Unauthorized",
	WithStatus(codes.Unauthenticated),
)

Functions

func GetBearerToken

func GetBearerToken(ctx context.Context) string

func GetHeaderFromContext

func GetHeaderFromContext(ctx context.Context, key string) (string, bool)

func GetHeaderValue

func GetHeaderValue(headers metadata.MD, key string) (string, bool)

func GetListenPort

func GetListenPort(v uint16) string

func NewClientTLSFromBase64

func NewClientTLSFromBase64(certB64, serverNameOverride string) (credentials.TransportCredentials, error)

NewClientTLSFromBase64 constructs TLS credentials from the provided root certificate authority certificate file(s) to validate server connections. If certificates to establish the identity of the client need to be included in the credentials (eg: for mTLS), use NewTLS instead, where a complete tls.Config can be specified. serverNameOverride is for testing only. If set to a non-empty string, it will override the virtual host name of authority (e.g. :authority header field) in requests.

func OverrideMessage

func OverrideMessage(message string) errx.SetOptionFn

func SetSubject

func SetSubject(ctx context.Context, id, fullName, role string) context.Context

func WithStatus

func WithStatus(status codes.Code) errx.SetOptionFn

func X509KeyPairFromBase64

func X509KeyPairFromBase64(certB64, keyB64 string) (*tls.Certificate, error)

X509KeyPairFromBase64 parses a public/private key pair from a pair of PEM encoded data. On successful return, Certificate. Leaf will be nil because the parsed form of the certificate is not retained.

Encoded certificate and key is base64 encoded string

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

func GetBasicAuth

func GetBasicAuth(ctx context.Context) *BasicAuth

type ContextKey

type ContextKey string
const (
	ContextKeyBasicAuth       ContextKey = "basic-auth"
	ContextKeyBearerToken     ContextKey = "bearer-token"
	ContextKeyRequestMetadata ContextKey = "request-metadata"
	ContextKeySubject         ContextKey = "subject"
)

type ErrorDetails

type ErrorDetails struct {
	Code    string               `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
	Message string               `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
	Source  *ErrorDetails_Source `protobuf:"bytes,3,opt,name=source,proto3" json:"source,omitempty"`
	// contains filtered or unexported fields
}

func NewErrorDetails

func NewErrorDetails(err error, withSource bool) (codes.Code, *ErrorDetails)

func (*ErrorDetails) Descriptor deprecated

func (*ErrorDetails) Descriptor() ([]byte, []int)

Deprecated: Use ErrorDetails.ProtoReflect.Descriptor instead.

func (*ErrorDetails) GetCode

func (x *ErrorDetails) GetCode() string

func (*ErrorDetails) GetMessage

func (x *ErrorDetails) GetMessage() string

func (*ErrorDetails) GetSource

func (x *ErrorDetails) GetSource() *ErrorDetails_Source

func (*ErrorDetails) ProtoMessage

func (*ErrorDetails) ProtoMessage()

func (*ErrorDetails) ProtoReflect

func (x *ErrorDetails) ProtoReflect() protoreflect.Message

func (*ErrorDetails) Reset

func (x *ErrorDetails) Reset()

func (*ErrorDetails) String

func (x *ErrorDetails) String() string

type ErrorDetails_Source

type ErrorDetails_Source struct {
	Message  string            `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	Traces   []string          `protobuf:"bytes,2,rep,name=traces,proto3" json:"traces,omitempty"`
	Metadata map[string]string `` /* 157-byte string literal not displayed */
	// contains filtered or unexported fields
}

func (*ErrorDetails_Source) Descriptor deprecated

func (*ErrorDetails_Source) Descriptor() ([]byte, []int)

Deprecated: Use ErrorDetails_Source.ProtoReflect.Descriptor instead.

func (*ErrorDetails_Source) GetMessage

func (x *ErrorDetails_Source) GetMessage() string

func (*ErrorDetails_Source) GetMetadata

func (x *ErrorDetails_Source) GetMetadata() map[string]string

func (*ErrorDetails_Source) GetTraces

func (x *ErrorDetails_Source) GetTraces() []string

func (*ErrorDetails_Source) ProtoMessage

func (*ErrorDetails_Source) ProtoMessage()

func (*ErrorDetails_Source) ProtoReflect

func (x *ErrorDetails_Source) ProtoReflect() protoreflect.Message

func (*ErrorDetails_Source) Reset

func (x *ErrorDetails_Source) Reset()

func (*ErrorDetails_Source) String

func (x *ErrorDetails_Source) String() string

type RequestHandler

type RequestHandler interface {
	IsCanceled(err error) bool
	OnPanic(ctx context.Context, fullMethod string, errValue any, stack []byte, req any)
	OnRequestError(ctx context.Context, err error)
	OnRequestHandled(ctx context.Context, fullMethod string, status codes.Code, reqMeta *RequestMetadata)
}

type RequestMetadata

type RequestMetadata struct {
	RequestId string    `json:"requestId"`
	ClientIP  string    `json:"clientIP"`
	UserAgent string    `json:"userAgent"`
	StartedAt time.Time `json:"startedAt"`
}

func GetRequestMetadata

func GetRequestMetadata(ctx context.Context) *RequestMetadata

func NewRequestMetadata

func NewRequestMetadata(ctx context.Context, trustProxy string) RequestMetadata

type Subject

type Subject struct {
	Id       string `json:"id"`
	FullName string `json:"fullName"`
	Role     string `json:"role"`
}

func GetSubject

func GetSubject(ctx context.Context) Subject

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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