ginqq

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2025 License: MIT Imports: 9 Imported by: 0

README

个性化Gin框架(ginqq)

简介

本项目是一个基于Gin框架二次开发的框架,提供了一些部门通用的功能和扩展。

设计原则
  • 开箱即用:内置系统级可观测性能力、API规范化能力,业务研发只需要关注业务逻辑
  • 最小入侵:不更改或尽可能少更改暴露的公共方法,对业务代码极少入侵,历史项目无缝切换,无学习成本
  • 功能可控:通过初始化参数控制各个功能开关,默认开启
  • 可扩展性:不更改已有代码的基础上支持新增功能,如鉴权、限流
功能
功能类型 功能清单 说明
API规范化 - API规范化(服务端、客户端)
- 安全过滤
- API规范内容自动补全、不合规调用拦截
- 包含XSS、SQL注入、CSRF等安全过滤逻辑
系统级可观测性 - 日志
- 监控
- 链路
- 内、外部流水
- 接口监控
- 接口链路

快速开始

版本要求
  • 推荐 Go 1.23+
安装
go get github.com/XXXX/ginqq

简单使用

默认配置

package main

import (
	gin "chinatelecom.cn/framework/ginqq"
	"net/http"
)

func main() {
	r := gin.Default("Y122010101", "Y122")

	g := r.Group("/api")
	g.GET("/hello", func(c *gin.Context) {
		traceId := c.GetTraceId()
		http.Get("http://www.baidu.com")
		c.JSON(http.StatusOK, gin.H{"message": "Hello from ginqq!", "traceId": traceId})
	})

	r.Run(":8080")

}

个性化配置

package main

import (
	gin "chinatelecom.cn/framework/ginqq"
	"net/http"
)

func main() {
	cfg := &gin.Config{
		ServiceCode: "Y122010101",
		PlatCode:    "Y122",
		MetricsConfig: &gin.MetricsConfig{
			Buckets: []float64{100, 200, 500, 1000, 2000, 3000, 5000, 10000},
			CustomLabels: map[string]string{
				"operate_type": "OPERATE_TYPE",
			},
		},
		HttpClientEnhanceConfig: &gin.HttpClientEnhanceConfig{
			Transport: http.DefaultTransport,
		},
	}
	r := gin.NewEngineWithConfig(cfg)

	r.GET("/hello", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"message": "Hello from ginqq!"})
	})

	r.Run(":8080")

}

文档

Documentation

Overview

Package ginqq 自定义的context,用于传递系统上下文信息,如服务编码,traceId等

Index

Constants

View Source
const (
	CtxKeyMethodCode = "Method-Code"
	CtxKeyTraceID    = "Trace-ID"
)

Variables

This section is empty.

Functions

func ApiStandardServerMiddleware

func ApiStandardServerMiddleware() func(*Context)

func GenerateUuid

func GenerateUuid() string

func HttpEnhance

func HttpEnhance(cfg *HttpClientEnhanceConfig)

func LoggingServerMiddleware

func LoggingServerMiddleware() func(*Context)

LoggingServerMiddleware 系统日志中间件

func MetricsServerMiddleware

func MetricsServerMiddleware() func(*Context)

func SecurityMiddleware

func SecurityMiddleware() func(*Context)

func TracingServerMiddleware

func TracingServerMiddleware() func(*Context)

Types

type ChainBuilder

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

ChainBuilder 链式构建器,用于构建中间件链,

func NewChainBuilder

func NewChainBuilder(base http.RoundTripper) *ChainBuilder

func (*ChainBuilder) Build

func (b *ChainBuilder) Build() http.RoundTripper

Build 返回调用链头指针

func (*ChainBuilder) Use

func (b *ChainBuilder) Use(middlewares ...http.RoundTripper) *ChainBuilder

Use 构建中间件链表,头插法

type Config

type Config struct {
	// 基础信息,必传字段
	ServiceCode string // 服务编码
	PlatCode    string // 平台编码
	// 功能开关&配置
	// 服务端系统可观测性
	DisableMetrics       bool
	MetricsConfig        *MetricsConfig
	DisableTracing       bool // 链路
	DisableLoggingServer bool // 内部流水

	// 服务端API规范化
	DisableApiStandardServer bool // 服务端API规范调用&校验拦截

	// Http客户端配置
	DisableHttpClientEnhance bool // http增强
	HttpClientEnhanceConfig  *HttpClientEnhanceConfig
}

type Context

type Context struct {
	*gin.Context
}

func Wrap

func Wrap(c *gin.Context) *Context

func (*Context) GetMethodCode

func (c *Context) GetMethodCode() string

func (*Context) GetTraceId

func (c *Context) GetTraceId() string

func (*Context) SetMethodCode

func (c *Context) SetMethodCode(methodCode string)

SetMethodCode 框架中间件数据传递

type GinQQ

type GinQQ struct {
	*gin.Engine
	Config *Config
}

GinQQ 自定义框架结构体

func Default

func Default(serviceCode string, platCode string) *GinQQ

Default 创建默认的 GinQQ 实例

func NewEngineWithConfig

func NewEngineWithConfig(config *Config) *GinQQ

func (*GinQQ) DELETE

func (g *GinQQ) DELETE(relativePath string, handlers ...func(*Context))

func (*GinQQ) GET

func (g *GinQQ) GET(relativePath string, handlers ...func(*Context))

func (*GinQQ) Group

func (g *GinQQ) Group(relativePath string, handlers ...func(*Context)) *RouterGroup

Group 重写分组,返回自定义RouterGroup

func (*GinQQ) POST

func (g *GinQQ) POST(relativePath string, handlers ...func(*Context))

func (*GinQQ) PUT

func (g *GinQQ) PUT(relativePath string, handlers ...func(*Context))

func (*GinQQ) Use

func (g *GinQQ) Use(handlers ...func(*Context))

type H

type H gin.H

type HttpClientEnhanceConfig

type HttpClientEnhanceConfig struct {
	Transport                http.RoundTripper // 基础transport,默认使用http.DefaultTransport,可自定义transport设置连接池参数、超时时间等
	DisableSkipVerify        bool              // 跳过证书认证,默认跳过 TODO 后续增加证书认证体系
	DisableApiStandardClient bool              // 客户端API规范调用&校验拦截
	DisableLoggingClient     bool              // 外部流水
}

type LoggingTripper

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

func NewLoggingTripper

func NewLoggingTripper() *LoggingTripper

func (*LoggingTripper) RoundTrip

func (l *LoggingTripper) RoundTrip(req *http.Request) (*http.Response, error)

func (*LoggingTripper) SetNext

func (l *LoggingTripper) SetNext(next http.RoundTripper)

type LoggingTripper2

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

LoggingTripper2 中间件示例,需要实现RoundTrip处理接口,SetNext设置下个中间接口

func NewLoggingTripper2

func NewLoggingTripper2() *LoggingTripper2

func (*LoggingTripper2) RoundTrip

func (l *LoggingTripper2) RoundTrip(req *http.Request) (*http.Response, error)

func (*LoggingTripper2) SetNext

func (l *LoggingTripper2) SetNext(next http.RoundTripper)

type MetricsConfig

type MetricsConfig struct {
	// bucket
	Buckets []float64 // 桶设置,单位毫秒,默认 [100, 200, 500, 1000, 2000, 3000, 5000, 10000]
	// 自定义标签,key:标签名,value:ctx中获取标签值的方法名,如:ctx.Get("operate_type")
	CustomLabels map[string]string
}

type RouterGroup

type RouterGroup struct {
	*gin.RouterGroup
}

RouterGroup 自定义路由组,强制处理函数接收自定义的*Context

func (*RouterGroup) DELETE

func (g *RouterGroup) DELETE(relativePath string, handlers ...func(*Context))

func (*RouterGroup) GET

func (g *RouterGroup) GET(relativePath string, handlers ...func(*Context))

func (*RouterGroup) POST

func (g *RouterGroup) POST(relativePath string, handlers ...func(*Context))

func (*RouterGroup) PUT

func (g *RouterGroup) PUT(relativePath string, handlers ...func(*Context))

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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