fgin

package module
v0.0.0-...-63ab2dd Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 44 Imported by: 0

Documentation

Overview

读取配置文件

处理用户使用自定义组件

Index

Constants

This section is empty.

Variables

View Source
var (
	FGINHTTPPLUGSMAP           = map[string]FginHttpPlugsInterface{}          // http自定义组件
	FGINGRPCMIDDPLUGSMAP       = map[string]FginGrpcMiddPlugsInterface{}      // grpc server拦截器自定义组件
	FGINGRPCCLIENTMIDDPLUGSMAP = map[string]FginGrpcClientMiddPlugInterface{} // grpc client端拦截器
	FGINGRPCPLUGSMAP           = map[string]FginGrpcPlugsInterface{}          // grpc自定义组件
)

全局插件记录

Functions

func ArrayFindString

func ArrayFindString(search []string, flag string) bool

查看数组内是否有

func FbindParams

func FbindParams(ctx *gin.Context, input interface{}) error

绑定参数,利用的gin的bind逻辑,可返回自定义错误 优化 支持嵌套结构体往下查找

func FginBindPlugConfig

func FginBindPlugConfig(plugConfig any, plugObj any) error

反序列化到自定义组件对象

func FginGetPlugConfig

func FginGetPlugConfig(plugName string) (any, error)

获取自定义组件的配置

func GenToken

func GenToken(authData map[string]string) (string, error)

func GetCtxToHeader

func GetCtxToHeader(ctx context.Context) http.Header

将context 转为header,实现span在http协议中传送

func GetGrpcClientConn deprecated

func GetGrpcClientConn(serviceName, tag string) (*grpc.ClientConn, error)

Deprecated: 该方法用于需要关闭conn的时候使用。已废弃,请使用兼容性更强的GetGrpcClientConnV2方法

func GetGrpcClientConnV2

func GetGrpcClientConnV2(serviceName, tag, address string) (*grpc.ClientConn, error)

该方法用于需要关闭conn的时候使用,优化方法兼容直接给grpc服务直连

func GetGrpcCtx

func GetGrpcCtx(ctx context.Context) context.Context

生成新context,自动植入grpc的信息

func ParsearseToken

func ParsearseToken(tokenStr string) (*customClaims, error)

func PlugsGrpcClientMiddAfterStart

func PlugsGrpcClientMiddAfterStart(ctx context.Context, method string, req, reply any, grpcErr error) error

grpc client 中间件拦截器运行 运行后

func PlugsGrpcClientMiddStart

func PlugsGrpcClientMiddStart(ctx context.Context, method string, req, reply any) (context.Context, error)

grpc client 中间件拦截器运行

func PlugsGrpcMiddAfterStart

func PlugsGrpcMiddAfterStart(ctx context.Context, req any, info *grpc.UnaryServerInfo, grpcErr error) error

grpc拦截器自定义组件运行后 接着运行

func PlugsGrpcMiddStart

func PlugsGrpcMiddStart(ctx context.Context, req any, info *grpc.UnaryServerInfo) (context.Context, error)

grpc拦截器自定义组件运行

func PlugsGrpcStart

func PlugsGrpcStart()

grpc自定义组件运行

func PlugsHttpStart

func PlugsHttpStart(router *gin.Engine)

http自定义组件运行

func RegisteGrpcClientMiddPlug

func RegisteGrpcClientMiddPlug(plugName string, plugObj FginGrpcClientMiddPlugInterface)

注册grpc client拦截器组件

func RegisteGrpcMiddPlug

func RegisteGrpcMiddPlug(plugName string, plugObj FginGrpcMiddPlugsInterface)

注册grpc拦截器组件

func RegisteGrpcPlug

func RegisteGrpcPlug(plugName string, plugObj FginGrpcPlugsInterface)

注册grpc组件

func RegisteHttpPlug

func RegisteHttpPlug(plugName string, plugObj FginHttpPlugsInterface)

注册http组件

func ReturnJson

func ReturnJson(ctx *gin.Context, code int, data any, err error)

返回数据

Types

type Auth

type Auth struct {
	AuthLs        []string `json:"auth_ls" mapstructure:"auth_ls"`
	Timeout       int      `json:"time_out" mapstructure:"time_out"`
	SecreKey      string   `json:"secre_key" mapstructure:"secre_key"`
	SigningMethod int      `json:"signing_method" mapstructure:"signing_method"`
	HeaderKey     string   `json:"header_key" mapstructure:"header_key"`
	DisableLs     []string `json:"disable_ls" mapstructure:"disable_ls"`
}

type Config

type Config struct {
	ServiceName    string         `yaml:"service_name" mapstructure:"service_name"`
	Tag            string         `yaml:"tag" mapstructure:"tag"`
	NetType        string         `yaml:"net_type" mapstructure:"net_type"`
	Ip             string         `yaml:"ip" mapstructure:"ip"`
	Port           int            `yaml:"port" mapstructure:"port"`
	Env            string         `yaml:"env" mapstructure:"env"`
	RateLimiter    bool           `yaml:"rate_limiter" mapstructure:"rate_limiter"`       // 限流器是否开启
	CircuitBreaker bool           `yaml:"circuit_breaker" mapstructure:"circuit_breaker"` // 熔断器是否开启
	Log            Log            `yaml:"log" mapstructure:"log"`
	Auth           Auth           `yaml:"auth" mapstructure:"auth"`
	Mysql          []Mysql        `yaml:"mysql" mapstructure:"mysql"`
	GormLog        GormLog        `yaml:"gorm_log" mapstructure:"gorm_log"`
	Redis          []Redis        `yaml:"redis" mapstructure:"redis"`
	Consul         Consul         `yaml:"consul" mapstructure:"consul"`
	Jaeger         Jaeger         `yaml:"jaeger" mapstructure:"jaeger"`
	Rabbitmq       []Rabbitmq     `yaml:"rabbitmq" mapstructure:"rabbitmq"`
	GrpcClient     []GrpcClient   `yaml:"grpc_client" mapstructure:"grpc_client"`
	Cors           Cors           `yaml:"cors" mapstructure:"cors"`
	Prometheus     Prometheus     `yaml:"prometheus" mapstructure:"prometheus"`
	Plugs          map[string]any `yaml:"plugs" mapstructure:"plugs"`
}

type Consul

type Consul struct {
	Address string `yaml:"address" mapstructure:"address"`
	Token   string `yaml:"token" mapstructure:"token"`
}

type Cors

type Cors struct {
	UseDefault       bool   `yaml:"use_default" mapstructure:"use_default"`
	AllowOrigins     string `yaml:"allow_origins" mapstructure:"allow_origins"`
	AllowMethods     string `yaml:"allow_methods" mapstructure:"allow_methods"`
	AllowHeaders     string `yaml:"allow_headers" mapstructure:"allow_headers"`
	ExposeHeaders    string `yaml:"expose_headers" mapstructure:"expose_headers"`
	AllowCredentials bool   `yaml:"allow_credentials" mapstructure:"allow_credentials"`
	MaxAge           int    `yaml:"max_age" mapstructure:"max_age"`
}

解决跨域配置

type Fgin

type Fgin struct{}

func (*Fgin) GetConfig

func (f *Fgin) GetConfig() Config

获得config

func (*Fgin) GetFginLogger

func (f *Fgin) GetFginLogger() *FginSugaredLogger

获取fgin日志操作

func (*Fgin) GetFgrpcClientConn

func (f *Fgin) GetFgrpcClientConn(serviceName string) (*FgrpcClientConn, error)

获取grpc已经dail的conn

func (*Fgin) GetGormDb

func (f *Fgin) GetGormDb(name string) *gorm.DB

获得数据库连接

func (*Fgin) GetGrpcServer

func (f *Fgin) GetGrpcServer(path string) *grpc.Server

获取一个grpc服务

func (*Fgin) GetHttpServer

func (f *Fgin) GetHttpServer(path string) *gin.Engine

获取一个路由

func (*Fgin) GetLogger

func (f *Fgin) GetLogger() *zap.SugaredLogger

获得日志操作

func (*Fgin) GetRabbitmqConn

func (f *Fgin) GetRabbitmqConn(queueName string) (*amqp.Connection, error)

获取rabbitmq dail的conn

func (*Fgin) GetRedisClient

func (f *Fgin) GetRedisClient() redis.UniversalClient

获取redis连接 默认取第一个

func (*Fgin) GetRedisClientByName

func (f *Fgin) GetRedisClientByName(redisName string) (redis.UniversalClient, error)

获取redis链接 通过名称获取

func (*Fgin) Run

func (f *Fgin) Run(router *gin.Engine)

优雅启动

func (*Fgin) RunGrpc

func (f *Fgin) RunGrpc(s *grpc.Server)

启动grpc服务

type FginGrpcClientMiddPlugInterface

type FginGrpcClientMiddPlugInterface interface {
	// grpc client端拦截器
	Start(ctx context.Context, method string, req, reply any) (context.Context, error)
	// 方法执行后
	AfterStart(ctx context.Context, method string, req, reply any, grpcErr error) error
}

type FginGrpcMiddPlugsInterface

type FginGrpcMiddPlugsInterface interface {
	// grpc server拦截器给到ctx
	Start(ctx context.Context, req any, info *grpc.UnaryServerInfo) (context.Context, error)
	// grpc server 方法执行后
	AfterStart(ctx context.Context, req any, info *grpc.UnaryServerInfo, grpcErr error) error
}

type FginGrpcPlugsInterface

type FginGrpcPlugsInterface interface {
	// grpc自定义组件
	Start()
}

type FginHttpPlugsInterface

type FginHttpPlugsInterface interface {
	// 给到router,可以实现中间件,也可以实现其他组件
	Start(router *gin.Engine)
}

type FginRabbitmq

type FginRabbitmq struct {
	QueueName     string
	MaxRetry      int32
	MaxConsume    int
	RabbitmqConn  *amqp.Connection
	RabbitmqCh    []*amqp.Channel
	RabbitmqQueue *amqp.Queue
}

框架的默认队列对象

func DefaultQueue

func DefaultQueue(queueName string) (*FginRabbitmq, error)

声明默认队列

func (*FginRabbitmq) DefaultConsumer

func (fr *FginRabbitmq) DefaultConsumer(hander func(msg amqp.Delivery) error) error

默认消费者

func (*FginRabbitmq) DefaultProducter

func (fr *FginRabbitmq) DefaultProducter(contentType string, body []byte) error

默认生产者

type FginResponseWriter

type FginResponseWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

定义一个自定义的 ResponseWriter,以便我们可以捕获响应数据

func (*FginResponseWriter) Write

func (w *FginResponseWriter) Write(b []byte) (int, error)

type FginSugaredLogger

type FginSugaredLogger struct {
	// contains filtered or unexported fields
}

func (*FginSugaredLogger) Debug

func (s *FginSugaredLogger) Debug(args ...interface{})

func (*FginSugaredLogger) Error

func (s *FginSugaredLogger) Error(args ...interface{})

func (*FginSugaredLogger) Info

func (s *FginSugaredLogger) Info(args ...interface{})

func (*FginSugaredLogger) Warn

func (s *FginSugaredLogger) Warn(args ...interface{})

type FgrpcClientConn

type FgrpcClientConn struct {
	*grpc.ClientConn
}

func (*FgrpcClientConn) Close

func (f *FgrpcClientConn) Close()

type GormLog

type GormLog struct {
	SlowThreshold             int  `yaml:"slow_threshold" mapstructure:"slow_threshold"`                               // 慢查询阈值,单位 ms
	Colorful                  bool `yaml:"colorful" mapstructure:"colorful"`                                           // 日志是否着色
	IgnoreRecordNotFoundError bool `yaml:"ignore_record_not_found_error" mapstructure:"ignore_record_not_found_error"` // 是否忽略记录不存在的错误
	ParameterizedQueries      bool `yaml:"parameterized_queries" mapstructure:"parameterized_queries"`                 // 不包含参数在sql log内
	LogLevel                  int  `yaml:"log_level" mapstructure:"log_level"`                                         // 1: Silent, 2: Error, 3: Warn, 4: Info
}

type GrpcClient

type GrpcClient struct {
	ServiceName string `yaml:"service_name" mapstructure:"service_name"`
	Tag         string `yaml:"tag" mapstructure:"tag"`
	Address     string `yaml:"address" mapstructure:"address"` // 用于直连grpc服务
}

type HealthService

type HealthService struct{}

手动实现健康检查部分

func (*HealthService) Check

func (*HealthService) Watch

type Jaeger

type Jaeger struct {
	Address     string `yaml:"address" mapstructure:"address"`
	AddressHTTP string `yaml:"address_http" mapstructure:"address_http"`
}

type Log

type Log struct {
	Level     string `yaml:"level" mapstructure:"level"`
	WriteType int    `yaml:"write_type" mapstructure:"write_type"`
	LogPath   string `yaml:"log_path" mapstructure:"log_path"`
	Rotate    string `yaml:"rotate" mapstructure:"rotate"` // 日志分割配置 y 年 M 月 d 日 h 时 m 分 s 秒
}

type Mysql

type Mysql struct {
	Name    string `yaml:"name" mapstructure:"name"`
	Target  string `yaml:"target" mapstructure:"Target"`
	MaxIdle int    `yaml:"max_idle" mapstructure:"max_idle"`
	MaxOpen int    `yaml:"max_open" mapstructure:"max_open"`
	MaxLift int    `yaml:"max_lift" mapstructure:"max_lift"`
}

type Prometheus

type Prometheus struct {
	Ip   string `yaml:"ip" mapstructure:"ip"`
	Port int    `yaml:"port" mapstructure:"port"`
	Path string `yaml:"path" mapstructure:"path"`
}

普罗米修斯配置

type Rabbitmq

type Rabbitmq struct {
	Address    string `yaml:"address" mapstructure:"address"`
	QueueName  string `yaml:"queue_name" mapstructure:"queue_name"`
	MaxRetry   int32  `yaml:"max_retry" mapstructure:"max_retry"`
	MaxConsume int    `yaml:"max_consume" mapstructure:"max_consume"`
}

type Redis

type Redis struct {
	RedisName  string   `yaml:"redis_name" mapstructure:"redis_name"` // redis服务的名称,使用多个不同redis
	Addrs      []string `yaml:"addrs" mapstructure:"addrs"`           // 多个地址,使用集群模式
	UserName   string   `yaml:"user_name" mapstructure:"user_name"`
	PassWord   string   `yaml:"pass_word" mapstructure:"pass_word"`
	Db         int      `yaml:"db" mapstructure:"db"`
	MasterName string   `yaml:"master_name" mapstructure:"master_name"` // 配置name,使用哨兵模式
}

type RestartFgrpcClient

type RestartFgrpcClient struct {
	ServiceName string // grpc服务名
	Tag         string // grpc服务tag
	Address     string // grpc直连地址 当serviceName和tag存在的时候,可以不填
	// contains filtered or unexported fields
}

重新grpc conn出现断开后,可以创建一个新的

func GetRestartGrpcObj

func GetRestartGrpcObj(serviceName string) (*RestartFgrpcClient, error)

获取grpc链接重启对象

func (*RestartFgrpcClient) RestartFgrpcClientConn

func (r *RestartFgrpcClient) RestartFgrpcClientConn() (*FgrpcClientConn, error)

重启grpc连接方法

Jump to

Keyboard shortcuts

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