cherry

package module
v1.16.7 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: MIT Imports: 55 Imported by: 8

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 -g _example/protobuf -e(enum扩展) -w(gin gateway) -q(graphql) -v(生成校验代码) -p proto目录 -g 输出pb.go目录

use docker(可选的)

docker run --rm -v $project:/work jybl/protogen protogen go -e -w -p $proto_path -g $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"
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"google.golang.org/grpc"
)

func main() {


	conf := cherry.NewConfig()
	conf.GrpcOptions = []grpc.ServerOption{
		grpc.StatsHandler(otelgrpc.NewServerHandler()),
	}
  cherry.Start(&cherry.Server{
        Config: config,
		GrpcHandler: func(gs *grpc.Server) {
			user.RegisterUserServiceServer(gs, userservice.GetUserService())
		},
		GinHandler: func(app *gin.Engine) {
			_ = user.RegisterUserServiceHandlerServer(app, userservice.GetUserService())
			app.Static("/static", "F:/upload")
		},
        /*	GraphqlHandler: model.NewExecutableSchema(model.Config{
                Resolvers: &model.GQLServer{
                UserService:  service.GetUserService(),
                OauthService: service.GetOauthService(),
            }}),*/
	})
}

TODO

  • unit test
  • english document
  • License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetAccessLog

func SetAccessLog(accessLog AccessLog)

func SetMetricsRecord

func SetMetricsRecord(metricsRecord MetricsRecord)

func StreamAccess

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

func StreamValidator

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

func UnaryValidator

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

Types

type AccessLog

type AccessLog = func(ctxi *httpctx.Context, uri, method, body, result string, code int)

type Http

type Http struct {
	http.Server
	CertFile string
	KeyFile  string
}

type Http3

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

type HttpOption

type HttpOption struct {
	ExcludeLogPrefixes []string
	IncludeLogPrefixes []string
	StaticFs           []StaticFsConfig
	Middlewares        []http.HandlerFunc
}

type IOption added in v1.16.7

type IOption interface {
	// contains filtered or unexported methods
}

type MetricsRecord

type MetricsRecord = func(ctxi *httpctx.Context, uri, method string, code int)

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(cors *cors.Options) Option

func WithGinHandler added in v1.16.7

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

func WithGraphqlHandler added in v1.16.7

func WithGraphqlHandler(graphqlHandler graphql.ExecutableSchema) Option

func WithGrpcHandler added in v1.16.7

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

func WithGrpcOptions added in v1.16.7

func WithGrpcOptions(options ...grpc.ServerOption) Option

func WithGrpcWeb added in v1.16.7

func WithGrpcWeb(options ...web.Option) Option

func WithHttp3Addr added in v1.16.7

func WithHttp3Addr(addr string) Option

func WithHttpAddr added in v1.16.7

func WithHttpAddr(addr string) Option

func WithMiddlewares added in v1.16.7

func WithMiddlewares(middlewares ...http.HandlerFunc) Option

func WithName added in v1.16.7

func WithName(name string) Option

func WithOnStart added in v1.16.7

func WithOnStart(onStart func(context.Context)) Option

func WithOnStop added in v1.16.7

func WithOnStop(onStop func(context.Context)) Option

func WithStopTimeout added in v1.16.7

func WithStopTimeout(stopTimeout time.Duration) Option

func WithTelemetry added in v1.16.7

func WithTelemetry(telemetry TelemetryConfig) Option

type Server

type Server struct {
	Name        string
	Http        Http
	Http2       http2.Server
	Http3       *Http3
	StopTimeout time.Duration
	Gin         gini.Config
	EnableCors  bool
	Cors        *cors.Options
	Middlewares []http.HandlerFunc
	HttpOption  HttpOption
	// Grpc options
	GrpcOptions                     []grpc.ServerOption
	EnableGrpcWeb                   bool
	GrpcWebOptions                  []web.Option
	EnableTelemetry, EnableDebugApi bool
	TelemetryConfig
	BaseContext context.Context
	// 注册 grpc 服务
	GrpcHandler func(*grpc.Server)
	// 注册 grpc-gateway 服务
	GatewayHandler runtime.GatewayHandler
	// 注册 gin 服务
	GinHandler func(*gin.Engine)
	// 注册 graphql 服务
	GraphqlHandler graphql.ExecutableSchema

	// 各种钩子函数
	OnStart func(context.Context)
	OnStop  func(context.Context)
}

func NewServer

func NewServer(options ...Option) *Server

func (*Server) Init added in v1.16.7

func (s *Server) Init()

func (*Server) InitAfterInject added in v1.16.7

func (s *Server) InitAfterInject()

func (*Server) InitBeforeInject added in v1.16.7

func (s *Server) InitBeforeInject()

implement initialize

func (*Server) Run added in v1.16.7

func (s *Server) Run()

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 StaticFsConfig

type StaticFsConfig struct {
	Prefix string
	Root   string
}

type TelemetryConfig

type TelemetryConfig struct {
	EnablePrometheus bool
	MetricsInterval  time.Duration
	// contains filtered or unexported fields
}

func (*TelemetryConfig) SetMeterProvider

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

func (*TelemetryConfig) SetTextMapPropagator

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

func (*TelemetryConfig) SetTracerProvider

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

Directories

Path Synopsis
_example
protobuf/user
Package user is a reverse proxy.
Package user is a reverse proxy.
gateway
gin

Jump to

Keyboard shortcuts

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