Commit ff11388b authored by gaoshiyao's avatar gaoshiyao

更新路由设置

parent 64dde16d
......@@ -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
......@@ -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,
})
}
......@@ -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 == "" {
......
......@@ -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()
}
......
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment