cherry

package module
v1.20.27 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 47 Imported by: 9

README

cherry

一个开箱即用,高度集成的微服务组件库,可以快速开发集grpc,http,graphql的云原生微服务

cherry服务器,各种服务接口的保留,集成支持,一个服务暴露grpc,http,graphql接口

  • 集成opentelemetry实现调用链路跟踪记录,配合context及utils/log 实现完整的请求链路日志记录
  • 集成prometheus及pprof实现性能监控及性能问题排查
  • 支持框架自生成的由gin提供支持的grpc转http,也支持原生的grpc-gateway server

quick start

go get github.com/hopeio/cherry

install tools
generate protobuf

protogen go -e -w -v -p _example/proto -o _example/protobuf -d(openapi doc) -e(enum扩展) -w(gin gateway) -g(graphql) -v(生成校验代码) -i proto目录 -o 输出pb.go目录

use docker(可选的)

docker run --rm -v $project:/work jybl/protogen protogen go -d -e -w -i $proto_path -o $proto_output_path

run

go run _example/user/main.go

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/hopeio/cherry"
	"protobuf/user"
	userservice "user/service"
	"google.golang.org/grpc"
)

func main() {

	cherry.NewServer(cherry.WithGrpcHandler(func(gs *grpc.Server) {
		user.RegisterUserServiceServer(gs, userservice.GetUserService())
	}), cherry.WithGinHandler(func(app *gin.Engine) {
		_ = user.RegisterUserServiceHandlerServer(app, userservice.GetUserService())
		app.Static("/static", "F:/upload")
	})).Run()
}

grpc-gateway

protobuf插件生成基于gin的gateway,实现基于grpc对外提供http接口

grpc-web

grpc-web集成,通过浏览器调用grpc服务

http3

通过quic实现http3的支持

可观测性

集成opentelemetry

graphql(废弃)

鉴于国内实际落地并不多,移除掉graphql支持

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MetadataKey = metadataKey{}

Functions

func DefaultAccessLog added in v1.17.20

func DefaultAccessLog(ctx context.Context, param *AccessLogParam)

func DefaultGrpcAccessLog added in v1.19.12

func DefaultGrpcAccessLog(ctx context.Context, param *GrpcAccessLogParam)

func WithMetadata added in v1.20.26

func WithMetadata(ctx context.Context, metadata *Metadata) context.Context

Types

type AccessLog

type AccessLog = func(ctx context.Context, pram *AccessLogParam)

type AccessLogConfig added in v1.19.11

type AccessLogConfig struct {
	RecordFunc      AccessLog
	ExcludePrefixes []string
	IncludePrefixes []string
}

type AccessLogParam added in v1.17.15

type AccessLogParam struct {
	Method, Url string
	*httpx.Recorder
	Metadata *Metadata
}

type ApiDocConfig added in v1.18.6

type ApiDocConfig struct {
	Enabled        bool
	UriPrefix, Dir string
}

type Body added in v1.17.21

type Body struct {
	ContentType string
	Raw         []byte
	Data        any
}

type CorsConfig added in v1.18.6

type CorsConfig struct {
	Enabled bool
	cors.Options
}

type DebugHandlerConfig added in v1.18.16

type DebugHandlerConfig struct {
	Enabled   bool
	UriPrefix string
}

type GRPCStatus added in v1.19.13

type GRPCStatus interface {
	GRPCStatus() *status.Status
}

type GrpcAccessLog added in v1.19.12

type GrpcAccessLog = func(ctx context.Context, pram *GrpcAccessLogParam)

type GrpcAccessLogParam added in v1.19.12

type GrpcAccessLogParam struct {
	Method            string
	Request, Response any
	Err               error
	Metadata          *Metadata
}

type GrpcConfig added in v1.18.6

type GrpcConfig struct {
	RecordFunc               GrpcAccessLog
	EnableGrpcWeb            bool
	GrpcWebOptions           []web.Option
	Options                  []grpc.ServerOption
	UnaryServerInterceptors  []grpc.UnaryServerInterceptor
	StreamServerInterceptors []grpc.StreamServerInterceptor
}

type Http3

type Http3 struct {
	Enabled bool
	http3.Server
	CertFile string
	KeyFile  string
}

type Metadata added in v1.20.26

type Metadata struct {
	sync.RWMutex
	Logger                *log.Logger
	Data                  any
	DataM                 map[any]any
	TraceId               string
	RequestType           RequestType
	Token                 string
	AuthRaw               []byte
	AuthID                string
	Request               *http.Request
	ResponseWriter        http.ResponseWriter
	GinContext            *gin.Context // http only
	RequestAt             time.Time
	GrpcMD                metadata.MD                // grpc only
	ServerTransportStream grpc.ServerTransportStream // grpc only
	AccessLogFields       []zap.Field
	Bagage                baggage.Baggage // can not edit
}

func GetMetadata added in v1.20.26

func GetMetadata(ctx context.Context) *Metadata

func (*Metadata) Del added in v1.20.26

func (m *Metadata) Del(key any)

func (*Metadata) Get added in v1.20.26

func (m *Metadata) Get(key any) any

func (*Metadata) Set added in v1.20.26

func (m *Metadata) Set(key, value any)

type Option added in v1.16.7

type Option func(server *Server)

func WithContext added in v1.16.7

func WithContext(ctx context.Context) Option

func WithCors added in v1.16.7

func WithCors(handler func(cors *cors.Options)) Option

func WithGinHandler added in v1.16.7

func WithGinHandler(handler func(*gin.Engine)) Option

func WithGrpc added in v1.18.6

func WithGrpc(handler func(option *GrpcConfig)) Option

func WithGrpcHandler added in v1.16.7

func WithGrpcHandler(handler func(*grpc.Server)) Option

func WithHTTP3 added in v1.18.6

func WithHTTP3(handler func(s *Http3)) Option

func WithHttp added in v1.18.6

func WithHttp(handler func(s *http.Server)) Option

func WithHttp2 added in v1.19.11

func WithHttp2(handler func(s *http2.Server)) Option

func WithMiddleware added in v1.19.12

func WithMiddleware(mw ...httpx.Middleware) Option

func WithOtel added in v1.20.27

func WithOtel(handler func(otel *OtelConfig)) Option

type OtelConfig added in v1.20.26

type OtelConfig struct {
	Enabled      bool
	OtelhttpOpts []otelhttp.Option
	OtelgrpcOpts []otelgrpc.Option
}

func (*OtelConfig) SetOtelgrpcOptsHandlerOpts added in v1.20.26

func (c *OtelConfig) SetOtelgrpcOptsHandlerOpts(otelgrpcOpts []otelgrpc.Option)

func (*OtelConfig) SetOtelhttpHandlerOpts added in v1.20.26

func (c *OtelConfig) SetOtelhttpHandlerOpts(otelhttpOpts []otelhttp.Option)

type PrometheusConfig added in v1.19.11

type PrometheusConfig struct {
	Enabled bool
	HttpURI string
	promhttp.HandlerOpts
}

type RequestType added in v1.20.26

type RequestType int
const (
	RequestTypeHttp RequestType = iota
	RequestTypeGrpc
	RequestTypeGrpcWeb
)

type Server

type Server struct {
	http.Server
	CertFile       string
	KeyFile        string
	AccessLog      AccessLogConfig
	HTTP2          http2.Server
	HTTP3          Http3
	Cors           CorsConfig
	Grpc           GrpcConfig
	InternalServer http.Server
	ApiDoc         ApiDocConfig
	Otel           OtelConfig
	Prometheus     PrometheusConfig
	DebugHandler   DebugHandlerConfig
	BaseContext    context.Context
	Middlewares    []httpx.Middleware
	GinServer      *gin.Engine
	GrpcHandler    func(*grpc.Server)
}

func NewServer

func NewServer(options ...Option) *Server

func (*Server) AfterInject added in v1.16.17

func (s *Server) AfterInject()

func (*Server) BeforeInject added in v1.16.17

func (s *Server) BeforeInject()

implement initialize

func (*Server) Init added in v1.16.7

func (s *Server) Init()

func (*Server) InternalHandler added in v1.18.16

func (s *Server) InternalHandler()

func (*Server) Run added in v1.16.7

func (s *Server) Run()

func (*Server) StreamAccess added in v1.19.20

func (s *Server) StreamAccess(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error)

func (*Server) UnaryAccess added in v1.16.7

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

func (*Server) WithContext added in v1.20.26

func (s *Server) WithContext(ctx context.Context) *Server

func (*Server) WithOptions added in v1.16.7

func (s *Server) WithOptions(options ...Option) *Server

Directories

Path Synopsis
_example
client command
protobuf/user
Package user is a reverse proxy.
Package user is a reverse proxy.
user command

Jump to

Keyboard shortcuts

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