cherry

package module
v1.20.20 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 48 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

This section is empty.

Functions

func DefaultAccessLog added in v1.17.20

func DefaultAccessLog(ctxi *httpctx.Context, param *AccessLogParam)

func DefaultGrpcAccessLog added in v1.19.12

func DefaultGrpcAccessLog(ctxi *httpctx.Context, param *GrpcAccessLogParam)

func StreamValidator

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

Types

type AccessLog

type AccessLog = func(ctxi *httpctx.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
}

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(ctxi *httpctx.Context, pram *GrpcAccessLogParam)

type GrpcAccessLogParam added in v1.19.12

type GrpcAccessLogParam struct {
	Method string
	// contains filtered or unexported fields
}

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 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 WithTelemetry added in v1.16.7

func WithTelemetry(handler func(telemetry *TelemetryConfig)) Option

type PrometheusConfig added in v1.19.11

type PrometheusConfig struct {
	Enabled bool
	HttpUri string
	// contains filtered or unexported fields
}

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
	Telemetry      TelemetryConfig
	DebugHandler   DebugHandlerConfig
	BaseContext    context.Context
	Middlewares    []httpx.Middleware
	GinServer      *gin.Engine
	// 注册 grpc 服务
	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) WithOptions added in v1.16.7

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

type TelemetryConfig

type TelemetryConfig struct {
	Enabled bool

	Prometheus PrometheusConfig
	// contains filtered or unexported fields
}

func (*TelemetryConfig) SetMeterProvider

func (c *TelemetryConfig) SetMeterProvider(meterProvider *sdkmetric.MeterProvider)

func (*TelemetryConfig) SetOtelgrpcOptsHandlerOpts added in v1.18.6

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

func (*TelemetryConfig) SetOtelhttpHandlerOpts added in v1.18.6

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

func (*TelemetryConfig) SetPrometheusOpts added in v1.18.7

func (c *TelemetryConfig) SetPrometheusOpts(prometheusOpts []prometheus.Option)

func (*TelemetryConfig) SetTextMapPropagator

func (c *TelemetryConfig) SetTextMapPropagator(propagator propagation.TextMapPropagator)

func (*TelemetryConfig) SetTracerProvider

func (c *TelemetryConfig) SetTracerProvider(tracerProvider *sdktrace.TracerProvider)

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