From a747a087c6bb093a9cdee4e6d235f417d7afac6d Mon Sep 17 00:00:00 2001 From: gaoshiyao Date: Wed, 6 Nov 2019 14:33:03 +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 --- .beagle.yml | 6 ++--- build/dockerfile | 1 + docs/proxy.yaml | 5 ++++ src/config/conf.go | 20 ++++++++++------ src/handler/counthandler.go | 21 +++++++--------- src/handler/proxyhandler.go | 48 ++++++++++++++++++------------------- src/main.go | 31 +++++++++++++++++------- src/router/router.go | 3 +-- 8 files changed, 78 insertions(+), 57 deletions(-) create mode 100644 docs/proxy.yaml diff --git a/.beagle.yml b/.beagle.yml index 893c7c2..7f34d45 100644 --- a/.beagle.yml +++ b/.beagle.yml @@ -32,9 +32,9 @@ pipeline: branch: [master] deploy: image: registry-vpc.cn-qingdao.aliyuncs.com/wod-devops/kubernetes:1.0 - namespace: cig - deployment: apaas-meshproxy - container: apaas-meshproxy + namespace: cloud + deployment: bgproxy-test + container: bgproxy-test registry: hub.wodcloud.com secrets: - source: KUBERNETES_SERVER diff --git a/build/dockerfile b/build/dockerfile index b04d70c..c723573 100644 --- a/build/dockerfile +++ b/build/dockerfile @@ -1,6 +1,7 @@ FROM {{ BASEIMAGE }} MAINTAINER {{ AUTHOR }} LABEL Author={{ AUTHOR }} Name={{ PROJECT }} Version={{ VERSION }} +RUN mkdir /app/config COPY ./dist/. /app EXPOSE 80 ENTRYPOINT ["/app/proxy", "--port=80","--prefix=test"] diff --git a/docs/proxy.yaml b/docs/proxy.yaml new file mode 100644 index 0000000..930cde4 --- /dev/null +++ b/docs/proxy.yaml @@ -0,0 +1,5 @@ +{ + "url":"https://apaas.wodcloud.com/cigservice/baseservice/fillder/turnover?applyId=0419f3e1-8e16-4e5e-a187-1760364c04e2", + "method":"GET", + "params":null +} \ No newline at end of file diff --git a/src/config/conf.go b/src/config/conf.go index 5855a69..a2e8302 100644 --- a/src/config/conf.go +++ b/src/config/conf.go @@ -1,11 +1,17 @@ package config -import "os" - var ( - ProxyHost string = os.Getenv("PROXY_HOST") - ProxyPath string = os.Getenv("PROXY_PATH") - RedisURL string - RedisTag string - Prefix string + RedisURL string + RedisTag string ) + +var ProxyConf = struct { + Url string `json:"url"` + Method string `json:"method"` + Params []Param `json:"params"` +}{} + +type Param struct { + Name string `json:"name"` + Value string `json:"value"` +} diff --git a/src/handler/counthandler.go b/src/handler/counthandler.go index 1d29088..30bb14a 100644 --- a/src/handler/counthandler.go +++ b/src/handler/counthandler.go @@ -1,20 +1,17 @@ package handler import ( - "fmt" "github.com/gin-gonic/gin" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" ) func GetCount(c *gin.Context) { - redis, err := client.GetRedisClient() - if err != nil { - c.Error(err) - } - ic, err := redis.Get(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) - c.JSON(200, gin.H{ - "Key": fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath), - "Count": ic, - }) + //redis, err := client.GetRedisClient() + //if err != nil { + // c.Error(err) + //} + //ic, err := redis.Get(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) + //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 cb97eae..c08b94d 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -5,9 +5,6 @@ import ( "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "net/http" - "strings" - - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" "github.com/vulcand/oxy/forward" "github.com/vulcand/oxy/testutils" @@ -39,20 +36,22 @@ func Proxy(c *gin.Context) { } func getProxyURL(req *http.Request) string { - path := req.URL.Path - path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) - rawQuery := req.URL.RawQuery - var result = "" - if rawQuery == "" { - result = fmt.Sprintf("http://%s%s", config.ProxyHost, path) - } else { - result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) - } - if strings.Contains(result, "?") { - if strings.HasSuffix(result, "/") { - result = strings.TrimRight(result, "/") - } - } + // 获取转发地址 + result := config.ProxyConf.Url + //path := req.URL.Path + //path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) + //rawQuery := req.URL.RawQuery + //var result = "" + //if rawQuery == "" { + // result = fmt.Sprintf("http://%s%s", config.ProxyHost, path) + //} else { + // result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) + //} + //if strings.Contains(result, "?") { + // if strings.HasSuffix(result, "/") { + // result = strings.TrimRight(result, "/") + // } + //} return result } @@ -72,11 +71,12 @@ func getRequestURI(req *http.Request) string { 计数 */ func count() error { - redis, err := client.GetRedisClient() - if err != nil { - return err - } - ic, err := redis.Incr(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) - _, err = ic.Result() - return err + //redis, err := client.GetRedisClient() + //if err != nil { + // return err + //} + //ic, err := redis.Incr(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) + //_, err = ic.Result() + //return err + return nil } diff --git a/src/main.go b/src/main.go index 6923ff6..9201711 100644 --- a/src/main.go +++ b/src/main.go @@ -1,13 +1,16 @@ package main import ( + "encoding/json" "fmt" - "github.com/sirupsen/logrus" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router" + "io/ioutil" "net/http" "os" "time" + "github.com/sirupsen/logrus" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" "github.com/gin-gonic/contrib/ginrus" @@ -15,23 +18,23 @@ import ( ) var ( - argPort = pflag.Int("port", 8088, "") - argPrefix = pflag.String("prefix", "/test", "") - redisUrl = pflag.String("redisUrl", "redis://k8s.wodcloud.com:16379", "") - redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "") + argPort = pflag.Int("port", 8088, "") + redisUrl = pflag.String("redisUrl", "redis://k8s.wodcloud.com:16379", "") + redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "") + confPath = pflag.String("confPath", "/app/config/proxy.yaml", "") ) func main() { pflag.Parse() initEnv() initConfig() + getProxyConf() server() } func server() error { - config.Prefix = *argPrefix handler := router.Load(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true)) - fmt.Println(fmt.Sprintf(":%d", *argPort)) + fmt.Println(fmt.Sprintf("Listen Server on :%d", *argPort)) return http.ListenAndServe( fmt.Sprintf(":%d", *argPort), handler, @@ -49,5 +52,15 @@ func initEnv() { func initConfig() { config.RedisTag = *redisTag config.RedisURL = *redisUrl - config.Prefix = *argPrefix +} + +// 获取代理参数 +func getProxyConf() { + b, err := ioutil.ReadFile(*confPath) + if err != nil { + logrus.Error(err) + return + } + json.Unmarshal(b, &config.ProxyConf) + fmt.Println("代理参数:", config.ProxyConf) } diff --git a/src/router/router.go b/src/router/router.go index 60aee0b..ee86674 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -16,7 +16,6 @@ package router import ( "github.com/gin-gonic/gin" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/handler" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router/middleware/header" "net/http" @@ -29,7 +28,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { e.Use(header.Options) e.Use(header.Secure) e.Use(middleware...) - root := e.Group(config.Prefix) + root := e.Group("/") { root.GET("/count", handler.GetCount) root.Any("/proxy/*action", handler.Proxy) -- 2.26.0