diff --git a/readme.md b/readme.md index b92ed986fbd78ccf8479432a50c835915b5826a1..5b40d7695587d90154308a5eaee1961254539bb3 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 fc634d8aa24b2ba8834b41e9d6adbb23dbc519cb..1d290889e06e99f7203d9433badfe5b3c147cf4b 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 f49c2f1625f80d013e737e9481fb5695ab714475..cb97eaecbc0194093d308b46dada3f5eebf772d5 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 42235802c868f67a507c2fc2d51bed98a117cd77..6923ff63cc86cc6bc8b14a03b01fd0f31be02a28 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 c1cace5ab14bec2b79de3780bb8511fb23d39bee..60aee0bc06710caa2c145d6618a9c37eb60a8484 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 }