Commit a747a087 authored by gaoshiyao's avatar gaoshiyao

更新路由设置

parent ff11388b
......@@ -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
......
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"]
{
"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
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"`
}
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,
//})
}
......@@ -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
}
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)
}
......@@ -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)
......
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