From 53243082fd042a5efe9e8bb1a783170a5ed8ec8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Tue, 25 Nov 2025 13:52:20 +0800 Subject: [PATCH] =?UTF-8?q?http=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E3=80=81=E8=B7=AF=E7=94=B1=E6=B3=A8=E5=86=8C=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/http.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/http/http.go b/http/http.go index dfccac5..05e8dac 100644 --- a/http/http.go +++ b/http/http.go @@ -3,6 +3,8 @@ package http import ( "context" "errors" + "fmt" + "gitee.com/red-future---jilin-g/common/jaeger" "gitee.com/red-future---jilin-g/common/utils" "github.com/gogf/gf/contrib/registry/consul/v2" "github.com/gogf/gf/v2/frame/g" @@ -13,6 +15,9 @@ import ( "github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/util/gconv" "net/http" + "reflect" + "regexp" + "strings" ) type ResponseEmpty struct { @@ -26,6 +31,29 @@ type Page struct { Total int //总页数 } +var HttpServer = g.Server() + +func init() { + //s.Use(common.Cors) //中间件验证 + //s.EnablePProf() //启用性能分析 + HttpServer.SetOpenApiPath("/api.json") + HttpServer.SetSwaggerPath("/swagger") //api文档访问路径 + HttpServer.SetDumpRouterMap(true) //关闭打印路由注册信息 + HttpServer.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse, jaeger.NewTracer) //使用默认http返回结构 + go HttpServer.Run() +} +func RouteRegister(controllers []interface{}) { + re := regexp.MustCompile("[A-Z]") + for _, t := range controllers { + sName := reflect.ValueOf(t).Elem().Type().Name() + convertedStr := re.ReplaceAllStringFunc(sName, func(s string) string { + return fmt.Sprintf("/%s", strings.ToLower(s)) + }) + HttpServer.Group(convertedStr, func(group *ghttp.RouterGroup) { + group.Bind(t) + }) + } +} func getHttpClient(ctx context.Context) (client *gclient.Client, err error) { consulCfg, _ := g.Cfg().Get(context.Background(), "consul.address") consulAddr := consulCfg.String()