From ff11388b50a80e85746cab9b9a1f12ac231cf3f9 Mon Sep 17 00:00:00 2001 From: gaoshiyao Date: Thu, 24 Oct 2019 20:50:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B7=AF=E7=94=B1=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 9 ++++++++- src/handler/counthandler.go | 22 +++++++--------------- src/handler/proxyhandler.go | 9 +++++++-- src/main.go | 4 ---- src/router/router.go | 4 +++- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/readme.md b/readme.md index b92ed98..5b40d76 100644 --- a/readme.md +++ b/readme.md @@ -13,4 +13,11 @@ PROXY_PATH=/cigservice/baseservice/fillder/turnover?applyId=0419f3e1-8e16-4e5e-a # 前端界面 # 注: 前端项目不可有前缀路由 PROXY_HOST=apaas.wodcloud.com -PROXY_PATH=/vuemap \ No newline at end of file +PROXY_PATH=/vuemap + +# 查询调用次数 +/${prefix}/count + + +# 代理 +/${prefix}/count \ No newline at end of file diff --git a/src/handler/counthandler.go b/src/handler/counthandler.go index fc634d8..1d29088 100644 --- a/src/handler/counthandler.go +++ b/src/handler/counthandler.go @@ -2,27 +2,19 @@ package handler import ( "fmt" - "net/http" - + "github.com/gin-gonic/gin" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" ) -// CountHandler , a proxy handler -type CountHandler struct { -} - -// CreateCountHandler , a proxy handler -func CreateCountHandler() (*CountHandler, error) { - handler := new(CountHandler) - return handler, nil -} - -func (handler *CountHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func GetCount(c *gin.Context) { redis, err := client.GetRedisClient() if err != nil { - w.Write([]byte(err.Error())) + c.Error(err) } ic, err := redis.Get(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) - w.Write([]byte(ic)) + c.JSON(200, gin.H{ + "Key": fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath), + "Count": ic, + }) } diff --git a/src/handler/proxyhandler.go b/src/handler/proxyhandler.go index f49c2f1..cb97eae 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -3,6 +3,7 @@ package handler import ( "fmt" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "net/http" "strings" @@ -26,16 +27,20 @@ func Proxy(c *gin.Context) { return } req := c.Request + baseUrl := "http://" + req.Host + req.URL.Path req.URL = testutils.ParseURI(getProxyURL(req)) req.RequestURI = getRequestURI(req) req.Host = req.URL.Host - fmt.Println(req.URL, req.RequestURI, req.Host) + logrus.Info("************************************") + logrus.Info("* 调用地址:", baseUrl) + logrus.Info("* 转发地址:", req.URL) + logrus.Info("************************************") fwd.ServeHTTP(c.Writer, req) } func getProxyURL(req *http.Request) string { path := req.URL.Path - path = strings.Replace(req.URL.Path, config.Prefix, config.ProxyPath, 1) + path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) rawQuery := req.URL.RawQuery var result = "" if rawQuery == "" { diff --git a/src/main.go b/src/main.go index 4223580..6923ff6 100644 --- a/src/main.go +++ b/src/main.go @@ -25,10 +25,6 @@ func main() { pflag.Parse() initEnv() initConfig() - //proxyhandler, _ := handler.CreateProxyHandler() - //counthandler, _ := handler.CreateCountHandler() - //http.Handle(fmt.Sprintf("%s%s", *argPrefix, "/count"), counthandler) - //http.Handle(*argPrefix, proxyhandler) server() } diff --git a/src/router/router.go b/src/router/router.go index c1cace5..60aee0b 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -31,7 +31,9 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { e.Use(middleware...) root := e.Group(config.Prefix) { - root.Any("*action", handler.Proxy) + root.GET("/count", handler.GetCount) + root.Any("/proxy/*action", handler.Proxy) } + return e } -- 2.26.0