Commit a747a087 authored by gaoshiyao's avatar gaoshiyao

更新路由设置

parent ff11388b
...@@ -32,9 +32,9 @@ pipeline: ...@@ -32,9 +32,9 @@ pipeline:
branch: [master] branch: [master]
deploy: deploy:
image: registry-vpc.cn-qingdao.aliyuncs.com/wod-devops/kubernetes:1.0 image: registry-vpc.cn-qingdao.aliyuncs.com/wod-devops/kubernetes:1.0
namespace: cig namespace: cloud
deployment: apaas-meshproxy deployment: bgproxy-test
container: apaas-meshproxy container: bgproxy-test
registry: hub.wodcloud.com registry: hub.wodcloud.com
secrets: secrets:
- source: KUBERNETES_SERVER - source: KUBERNETES_SERVER
......
FROM {{ BASEIMAGE }} FROM {{ BASEIMAGE }}
MAINTAINER {{ AUTHOR }} MAINTAINER {{ AUTHOR }}
LABEL Author={{ AUTHOR }} Name={{ PROJECT }} Version={{ VERSION }} LABEL Author={{ AUTHOR }} Name={{ PROJECT }} Version={{ VERSION }}
RUN mkdir /app/config
COPY ./dist/. /app COPY ./dist/. /app
EXPOSE 80 EXPOSE 80
ENTRYPOINT ["/app/proxy", "--port=80","--prefix=test"] ENTRYPOINT ["/app/proxy", "--port=80","--prefix=test"]
{
"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
package config package config
import "os"
var ( var (
ProxyHost string = os.Getenv("PROXY_HOST")
ProxyPath string = os.Getenv("PROXY_PATH")
RedisURL string RedisURL string
RedisTag string RedisTag string
Prefix 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"`
}
package handler package handler
import ( import (
"fmt"
"github.com/gin-gonic/gin" "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) { func GetCount(c *gin.Context) {
redis, err := client.GetRedisClient() //redis, err := client.GetRedisClient()
if err != nil { //if err != nil {
c.Error(err) // c.Error(err)
} //}
ic, err := redis.Get(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) //ic, err := redis.Get(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath))
c.JSON(200, gin.H{ //c.JSON(200, gin.H{
"Key": fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath), // "Key": fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath),
"Count": ic, // "Count": ic,
}) //})
} }
...@@ -5,9 +5,6 @@ import ( ...@@ -5,9 +5,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"net/http" "net/http"
"strings"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client"
"github.com/vulcand/oxy/forward" "github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/testutils" "github.com/vulcand/oxy/testutils"
...@@ -39,20 +36,22 @@ func Proxy(c *gin.Context) { ...@@ -39,20 +36,22 @@ func Proxy(c *gin.Context) {
} }
func getProxyURL(req *http.Request) string { func getProxyURL(req *http.Request) string {
path := req.URL.Path // 获取转发地址
path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) result := config.ProxyConf.Url
rawQuery := req.URL.RawQuery //path := req.URL.Path
var result = "" //path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1)
if rawQuery == "" { //rawQuery := req.URL.RawQuery
result = fmt.Sprintf("http://%s%s", config.ProxyHost, path) //var result = ""
} else { //if rawQuery == "" {
result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) // result = fmt.Sprintf("http://%s%s", config.ProxyHost, path)
} //} else {
if strings.Contains(result, "?") { // result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery)
if strings.HasSuffix(result, "/") { //}
result = strings.TrimRight(result, "/") //if strings.Contains(result, "?") {
} // if strings.HasSuffix(result, "/") {
} // result = strings.TrimRight(result, "/")
// }
//}
return result return result
} }
...@@ -72,11 +71,12 @@ func getRequestURI(req *http.Request) string { ...@@ -72,11 +71,12 @@ func getRequestURI(req *http.Request) string {
计数 计数
*/ */
func count() error { func count() error {
redis, err := client.GetRedisClient() //redis, err := client.GetRedisClient()
if err != nil { //if err != nil {
return err // return err
} //}
ic, err := redis.Incr(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath)) //ic, err := redis.Incr(fmt.Sprintf("%s%s", config.ProxyHost, config.ProxyPath))
_, err = ic.Result() //_, err = ic.Result()
return err //return err
return nil
} }
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/sirupsen/logrus" "io/ioutil"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router"
"net/http" "net/http"
"os" "os"
"time" "time"
"github.com/sirupsen/logrus"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config"
"github.com/gin-gonic/contrib/ginrus" "github.com/gin-gonic/contrib/ginrus"
...@@ -16,22 +19,22 @@ import ( ...@@ -16,22 +19,22 @@ import (
var ( var (
argPort = pflag.Int("port", 8088, "") argPort = pflag.Int("port", 8088, "")
argPrefix = pflag.String("prefix", "/test", "")
redisUrl = pflag.String("redisUrl", "redis://k8s.wodcloud.com:16379", "") redisUrl = pflag.String("redisUrl", "redis://k8s.wodcloud.com:16379", "")
redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "") redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "")
confPath = pflag.String("confPath", "/app/config/proxy.yaml", "")
) )
func main() { func main() {
pflag.Parse() pflag.Parse()
initEnv() initEnv()
initConfig() initConfig()
getProxyConf()
server() server()
} }
func server() error { func server() error {
config.Prefix = *argPrefix
handler := router.Load(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true)) 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( return http.ListenAndServe(
fmt.Sprintf(":%d", *argPort), fmt.Sprintf(":%d", *argPort),
handler, handler,
...@@ -49,5 +52,15 @@ func initEnv() { ...@@ -49,5 +52,15 @@ func initEnv() {
func initConfig() { func initConfig() {
config.RedisTag = *redisTag config.RedisTag = *redisTag
config.RedisURL = *redisUrl 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)
} }
...@@ -16,7 +16,6 @@ package router ...@@ -16,7 +16,6 @@ package router
import ( import (
"github.com/gin-gonic/gin" "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/handler"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router/middleware/header" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router/middleware/header"
"net/http" "net/http"
...@@ -29,7 +28,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { ...@@ -29,7 +28,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.Use(header.Options) e.Use(header.Options)
e.Use(header.Secure) e.Use(header.Secure)
e.Use(middleware...) e.Use(middleware...)
root := e.Group(config.Prefix) root := e.Group("/")
{ {
root.GET("/count", handler.GetCount) root.GET("/count", handler.GetCount)
root.Any("/proxy/*action", handler.Proxy) root.Any("/proxy/*action", handler.Proxy)
......
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