cherry

package module
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 54 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 -c _example/user/config.toml

package main

import (
	"github.com/hopeio/utils/net/http/gin/handler"
	"github.com/gin-gonic/gin"
	"github.com/hopeio/cherry"
	"github.com/hopeio/initialize"
	"user/protobuf/user"
	uconf "user/confdao"
	udao "user/dao"
	userservice "user/service"
	"github.com/hopeio/utils/log"
	
	"google.golang.org/grpc"
)

func main() {
	//配置初始化应该在第一位
	defer initialize.Start(uconf.Conf, udao.Dao)()
	
  config := uconf.Conf.Server.Origin()
  config.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 Start

func Start(s *Server)

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 UnaryAccess

func UnaryAccess(conf *Config) grpc.UnaryServerInterceptor

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 Config

type Config struct {
	ServerName  string
	Http        Http
	Http2       http2.Server
	Http3       *Http3 `json:"http3"`
	StopTimeout time.Duration
	Gin         gini.Config `json:"gin"`
	EnableCors  bool
	Cors        *cors.Options `json:"cors"`
	Middlewares []http.HandlerFunc
	HttpOption  HttpOption
	// Grpc options
	GrpcOptions                                  []grpc.ServerOption
	EnableGrpcWeb                                bool
	GrpcWebOption                                []web.Option `json:"grpc_web"`
	EnableTelemetry, EnableDebugApi, GenerateDoc bool
	TelemetryConfig
	BaseContext func() context.Context
}

func NewConfig

func NewConfig() *Config

func (*Config) Init

func (c *Config) Init()

type Http

type Http struct {
	http.Server
	CertFile string `json:"cert_file"`
	KeyFile  string `json:"key_file"`
}

type Http3

type Http3 struct {
	http3.Server
	CertFile string `json:"cert_file"`
	KeyFile  string `json:"key_file"`
}

type HttpOption

type HttpOption struct {
	ExcludeLogPrefixes []string
	IncludeLogPrefixes []string
	StaticFs           []StaticFsConfig `json:"static_fs"`
	Middlewares        []http.HandlerFunc
}

type MetricsRecord

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

type Server

type Server struct {
	Config *Config
	// 注册 grpc 服务
	GrpcHandler func(*grpc.Server)
	// 注册 grpc-gateway 服务
	GatewayHandler runtime.GatewayHandler
	// 注册 gin 服务
	GinHandler func(*gin.Engine)
	// 注册 graphql 服务
	GraphqlHandler graphql.ExecutableSchema

	// 各种钩子函数
	OnBeforeStart func(context.Context)
	OnAfterStart  func(context.Context)
	OnBeforeStop  func(context.Context)
	OnAfterStop   func(context.Context)
}

func NewServer

func NewServer(config *Config, ginhandler func(*gin.Engine), grpchandler func(*grpc.Server), gatewayhandler runtime.GatewayHandler, graphqlhandler graphql.ExecutableSchema) *Server

func (*Server) Start

func (s *Server) Start()

type ServerOption

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

TODO

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