Commit c91916a4 authored by leitao.zhang's avatar leitao.zhang

修改代理

parent a498ce3f
package dao
type ServiceApply struct {
Id int `json:"id" xorm:"id"`
Service_id string `json:"service_id" xorm:"service_id"`
User_id int `json:"user_id" xorm:"user_id"`
Request_count int `json:"request_count" xorm:"request_count"`
ResFields string `json:"res_fields" xorm:"res_fields text"`
Id int `json:"id" xorm:"id"`
ServiceId string `json:"service_id" xorm:"service_id"`
UserId int `json:"user_id" xorm:"user_id"`
RequestCount int `json:"request_count" xorm:"request_count"`
ResFields string `json:"res_fields" xorm:"res_fields text"`
}
func (ServiceApply) TableName() string {
......
......@@ -2,19 +2,20 @@ package dao
import "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools"
type Service_request_record struct {
Id int `json:"id" xorm:"id"`
Service_id int `json:"service_id" xorm:"service_id"`
Apply_id int `json:"apply_id" xorm:"apply_id"`
User_id string `json:"user_id" xorm:"user_id"`
Request_time tools.JsonTime `json:"request_time" xorm:"request_time created"`
Date_day int `json:"date_day" xorm:"date_day"`
Date_month int `json:"date_month" xorm:"date_month"`
Date_year int `json:"date_year" xorm:"date_year"`
Status int `json:"status" xorm:"status"`
Error string `json:"error" xorm:"error"`
type ServiceRequestRecord struct {
Id int `json:"id" xorm:"id"`
ServiceId int `json:"service_id" xorm:"service_id"`
ApplyId int `json:"apply_id" xorm:"apply_id"`
UserId string `json:"user_id" xorm:"user_id"`
RequestTime tools.JsonTime `json:"request_time" xorm:"request_time created"`
DateDay int `json:"date_day" xorm:"date_day"`
DateMonth int `json:"date_month" xorm:"date_month"`
DateYear int `json:"date_year" xorm:"date_year"`
Status int `json:"status" xorm:"status"`
Error string `json:"error" xorm:"error"`
ReqUrl string `json:"req_url" xorm:"req_url"`
}
func (s Service_request_record) TableName() string {
func (s ServiceRequestRecord) TableName() string {
return "service_request_record"
}
......@@ -14,7 +14,9 @@ import (
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/service"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
......@@ -41,91 +43,145 @@ func Proxy(c *gin.Context) {
c.Error(err)
return
}
if proxyData.Second_level != 1 {
//proxyData.ReqUrl = "https://apaas3.wodcloud.com/iam/login/#/login"
if proxyData.SecondLevel != 1 {
res := model.WebRes{}
res.ErrMsg = `服务申请未通过审批,请联系组织管理员或超管审批`
c.JSON(200, res)
return
}
roundTripper := http.DefaultTransport.(*http.Transport)
roundTripper.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
f, _ := forward.New(forward.PassHostHeader(true), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error {
//Resp = resp
//判断是否超过调用次数是否可以调用
callflag, sensitiveflag, err := service.QueryCallsCount(proxyData)
if err != nil {
fmt.Println("err......", err.Error())
return nil
}
if callflag == false {
err = errors.New(`调用达到当日限定次数`)
Return(nil, err, resp)
if proxyData.ServiceEndTime.Before(appT) {
res := model.WebRes{}
res.ErrMsg = `申请的服务已过期`
c.JSON(200, res)
return
}
//判断是否超过调用次数是否可以调用
callflag, _, err := service.QueryCallsCount(proxyData)
if err != nil {
fmt.Println("err......", err.Error(), "无法处理调用次数判断")
res := model.WebRes{}
res.ErrMsg = `无法处理您的请求`
c.JSON(200, res)
return
}
if callflag {
res := model.WebRes{}
res.ErrMsg = `已达到最大调用量`
c.JSON(200, res)
return
}
//如果是静态文件
if CheckStaticFile(c.Request.URL.Path) {
host := getHost(proxyData.ReqUrl)
fwd, _ := forward.New(forward.PassHostHeader(false))
c.Request.URL = testutils.ParseURI(fmt.Sprintf("%s%s", host, c.Request.URL.Path))
c.Request.RequestURI = getRequestURI(c.Request)
c.Request.Host = host
fwd.ServeHTTP(c.Writer, c.Request)
} else {
roundTripper := http.DefaultTransport.(*http.Transport)
//roundTripper.ForceAttemptHTTP2 = false
roundTripper.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
f, err := forward.New(forward.PassHostHeader(false), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error {
respbody, err1 := ioutil.ReadAll(resp.Body)
if err1 != nil {
log.Println(err1)
return errors.New(`数据读取失败`)
}
var bytesNewBody []byte
//是否需要过滤字段
if proxyData.DataServiceType1 == 5 && proxyData.ResFields != "" && proxyData.ResFields != "[]" {
//读取数据
bytesNewBody = service.FiledFilter(proxyData, respbody)
} else {
bytesNewBody = respbody
}
//放回
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bytesNewBody))
go func() {
defer func() {
err := recover()
if err != nil {
fmt.Println(err)
}
}()
//调用次数统计
if err := service.CallCounts(proxyData); err != nil {
fmt.Println(err)
}
if resp.StatusCode < 400 {
service.RecordCall(proxyData, resp.StatusCode, nil)
} else {
service.RecordCall(proxyData, resp.StatusCode, nil)
}
}()
//是否需要屏蔽敏感字段 仅对数据服务生效 其它服务部进行处理
//if sensitiveflag && proxyData.DataServiceType1 == 5 {
// res = service.SensitiveFilter(proxyData, res)
//}
//计次
//bgn := time.Now()
//fmt.Println(`开始携程:`, bgn)
//go service.CallCounts(proxyData)
//end := time.Since(bgn)
//fmt.Println("结束携程: ", end)
//err = service.CallCounts(proxyData)
//if err != nil {
// Return(nil, err, resp)
// return nil
//}
////个人申请调用计次
//err = service.ApplyCallCounts(proxyData)
//if err != nil {
// Return(nil, err, resp)
// return nil
//}
//Return(res, nil, resp)
//appeT := time.Since(appT)
//fmt.Println("结束: ", appeT)
return nil
}))
if err != nil {
}
bT := time.Now()
fmt.Println(`开始获取数据:`, bT)
respbody, _ := ioutil.ReadAll(resp.Body)
var res interface{}
res = respbody
eT := time.Since(bT)
fmt.Println("获取数据: ", eT)
//是否需要过滤字段
if proxyData.ResFields != "" {
bT := time.Now()
fmt.Println(`过滤:`, bT)
res = service.FiledFilter(proxyData, respbody)
eT := time.Since(bT)
fmt.Println("过滤: ", eT)
}
//是否需要屏蔽敏感字段
if sensitiveflag == false {
res = service.SensitiveFilter(proxyData, res)
}
//计次
//bgn := time.Now()
//fmt.Println(`开始携程:`, bgn)
go service.CallCounts(proxyData)
//end := time.Since(bgn)
//fmt.Println("结束携程: ", end)
//err = service.CallCounts(proxyData)
//if err != nil {
// Return(nil, err, resp)
// return nil
//}
////个人申请调用计次
//err = service.ApplyCallCounts(proxyData)
//if err != nil {
// Return(nil, err, resp)
// return nil
//}
Return(res, nil, resp)
//appeT := time.Since(appT)
//fmt.Println("结束: ", appeT)
return nil
}))
c.Request.URL = testutils.ParseURI(getProxyURL(proxyData, c.Request))
fmt.Println("c.Request.URL-------", c.Request.URL)
c.Request.RequestURI = getRequestURI(c.Request)
fmt.Println("c.Request-------", c.Request)
c.Request.Host = getHost(proxyData.RealUrl)
fmt.Println("proxyData.RealUrl-------", proxyData.RealUrl)
f.ServeHTTP(c.Writer, c.Request)
c.Request.URL = testutils.ParseURI(proxyData.ReqUrl)
fmt.Println("c.Request.URL-------", c.Request.URL)
c.Request.RequestURI = getRequestURI(c.Request)
fmt.Println("c.Request-------", c.Request)
c.Request.Host = getHost(proxyData.ReqUrl)
fmt.Println("proxyData.RealUrl-------", proxyData.ReqUrl)
f.ServeHTTP(c.Writer, c.Request)
}
}
func CheckStaticFile(path string) bool {
if strings.HasSuffix(path, ".css") || strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".woff2") ||
strings.HasSuffix(path, ".jpg") || strings.HasSuffix(path, ".svg") || strings.HasSuffix(path, ".woff") || strings.HasSuffix(path, ".html") || strings.HasSuffix(path, ".ttf") {
return true
}
return false
}
func getProxyURL(proxyData model.ProxyData, req *http.Request) string {
realPath := proxyData.RealUrl
realPath := proxyData.ReqUrl
rawQuery := req.URL.RawQuery
var result = ""
if rawQuery == "" {
if proxyData.Name == "时空服务" {
if proxyData.DataServiceType1 == 6 {
result = realPath + "?f=json"
} else {
result = realPath
}
} else {
result = realPath + "?" + rawQuery
if proxyData.Name == "时空服务" && !strings.Contains(rawQuery, "f=json") {
if proxyData.DataServiceType1 == 6 && !strings.Contains(rawQuery, "f=json") {
result = result + "&f=json"
}
}
......@@ -176,20 +232,19 @@ func gzipCompress(content *[]byte) []byte {
return compressData.Bytes()
}
// 获取域名
func getHost(url string) (path string) {
if strings.Contains(url, "//") {
path = strings.Split(url, "//")[1]
if strings.Contains(path, "/") {
path = strings.Split(path, "/")[0]
}
// 获取域名 protocol :// hostname[:port] / path / [;parameters][?query]#fragment
func getHost(httpurl string) (path string) {
parse, err := url.Parse(httpurl)
if err != nil {
return ""
}
return
return fmt.Sprintf("%s://%s", parse.Scheme, parse.Host)
}
func HealthCheck(c *gin.Context) {
res := model.WebRes{}
proxyData, err := service.GetReqPath()
apiId := c.Param("apiid")
proxyData, err := service.GetReqPath(apiId)
if err != nil {
res.Data = err.Error()
c.JSON(500, res)
......
......@@ -18,11 +18,11 @@ import (
var (
argPort = pflag.Int("port", 8011, "")
argPrefix = pflag.String("prefix", "/bgmesh/fiddler", "")
meshId = pflag.String("meshId", "149", "")
redisUrl = pflag.String("redisUrl", "redis://redis.apaas-v3:6379", "")
meshId = pflag.String("meshId", "133", "")
redisUrl = pflag.String("redisUrl", "redis://apaas-redis:6379", "")
redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "")
confPath = pflag.String("confPath", "/app/config/proxy.json", "")
dbURL = pflag.String("dbURL", "host=stolon-proxy.devops port=5432 user=postgres password=spaceIN511 dbname=apaas sslmode=disable", "")
dbURL = pflag.String("dbURL", "host=apaas-postgis.apaas-v3 port=54321 user=postgres password=passwd123 dbname=apaas sslmode=disable", "")
dbDriverName = pflag.String("dbDriverName", "postgres", "")
)
......
package model
import "time"
type ProxyData struct {
RealUrl string `json:"req_url" xorm:"req_url"`
ResFields string `json:"res_fields" xorm:"res_fields text"`
Count int `json:"count" xorm:"count"`
Sensituve_word string `json:"sensituve_word" xorm:"sensituve_word"`
Sensitive_count int `json:"sensitive_count" xorm:"sensitive_count"`
Apply_id int `json:"apply_id" xorm:"apply_id"`
Service_id int `json:"service_id" xorm:"service_id"`
User_id string `json:"user_id" xorm:"user_id"`
Data_service_type1 int `json:"data_service_type1" xorm:"data_service_type1"`
Name string `json:"name"`
Second_level int `json:"second_level"xorm:"second_level"`
ApplyId int64 `json:"apply_id"`
ReqUrl string `json:"req_url"`
RequestCount int64 `json:"request_count"`
Duration int64 `json:"duration"`
DurationUnit int64 `json:"duration_unit"`
SpcsType int `json:"spcs_type"`
SpcsCount int64 `json:"spcs_count"`
ResFields string `json:"res_fields"`
DataServiceType1 int64 `json:"data_service_type1"`
DataServiceType2 int64 `json:"data_service_type2"`
DataServiceType3 int64 `json:"data_service_type3"`
SecondLevel int64 `json:"second_level"`
ServiceEndTime time.Time `json:"service_end_time"`
ReqName string `json:"req_name"`
ServiceState int64 `json:"service_state"`
PayStatus int64 `json:"pay_status"`
ApplyUserId string `json:"apply_user_id"`
ServiceUserId string `json:"service_user_id"`
ServiceOid string `json:"service_oid"`
ApplyOid string `json:"apply_oid"`
SensituveWord string `json:"sensituve_word"`
SensitiveCount int64 `json:"sensitive_count"`
ServiceId int64 `json:"service_id"`
ApiId int64 `json:"api_id"` //接口ID
}
type WebRes struct {
......
......@@ -32,9 +32,12 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.Use(middleware...)
root := e.Group(fmt.Sprintf("%s/%s", config.Prefix, config.MeshId))
{
root.GET("/health", handler.HealthCheck)
root.Any("/service/:applyId", handler.Proxy)
//root.GET("/health", handler.HealthCheck)
root.GET("/health/:apiid/*any", handler.HealthCheck)
//root.Any("/service/:applyId", handler.Proxy)
root.Any("/service/:applyId/:apiid", handler.Proxy)
root.Any("/service/:applyId/:apiid/*any", handler.Proxy)
//root.GET("/static/*any", handler.StaticProxy)
}
return e
}
......@@ -20,7 +20,7 @@ import (
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/model/tables"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools/dataconvertutil"
"strconv"
"log"
"time"
)
......@@ -29,43 +29,50 @@ func CheckSensitiveField() bool {
return true
}
//获取当日调用次数 返回是否到达当日访问量,是否到达敏感字段访问量
func QueryCallsCount(filter model.ProxyData) (call bool, sensitiveCall bool, err error) {
//连接redis
redis, err := client.GetRedisClient()
if err != nil {
return
}
date := time.Now().Format(config.LocalDateFormat)
key := fmt.Sprintf("%d-%s", filter.Apply_id, date)
countStr, err := redis.Get(key)
if err != nil && err.Error() != `redis: nil` {
return
}
var count int
call = true
sensitiveCall = true
if countStr == "" {
expire := GetExpire()
err = redis.Set(key, 1, expire)
count = 1
} else {
count, err = strconv.Atoi(countStr)
if err != nil {
return
}
if count+1 > filter.Count {
call = false
return
//检测是否达到了访问量
func QueryCallsCount(filter model.ProxyData) (bool, bool, error) {
// 计次的
if filter.SpcsType == 1 {
if filter.SpcsCount < filter.RequestCount {
return true, false, nil
}
//超过允许访问敏感字段次数
if count+1 > filter.Sensitive_count && filter.Sensituve_word != "" {
sensitiveCall = false
}
expire := GetExpire()
err = redis.Set(key, count+1, expire)
}
return
return false, false, nil
////连接redis
//redis, err := client.GetRedisClient()
//if err != nil {
// return
//}
//date := time.Now().Format(config.LocalDateFormat)
//key := fmt.Sprintf("%d-%s", filter.ApplyId, date)
//countStr, err := redis.Get(key)
//if err != nil && err.Error() != `redis: nil` {
// return
//}
//var count int64
//call = true
//sensitiveCall = true
//if countStr == "" {
// expire := GetExpire()
// err = redis.Set(key, 1, expire)
// count = 1
//} else {
// count, err = strconv.ParseInt(countStr, 10, 64)
// if err != nil {
// return
// }
// if count+1 > filter.RequestCount {
// call = false
// return
// }
// //超过允许访问敏感字段次数
// if count+1 > filter.SensitiveCount && filter.SensituveWord != "" {
// sensitiveCall = false
// }
// expire := GetExpire()
// err = redis.Set(key, count+1, expire)
//}
//return
}
//获取过期时间
......@@ -83,27 +90,25 @@ func GetExpire() time.Duration {
//记录调用痕迹
func RecordCall(proxyData model.ProxyData, status int, res []byte) {
acc := dao.Service_request_record{}
acc := dao.ServiceRequestRecord{}
redis, err := client.GetRedisClient()
if err != nil {
fmt.Println(err.Error())
return
}
if status != 200 {
acc.Error = string(res[:])
//mjson,_ :=json.Marshal(res)
//mString :=string(mjson)
//acc.Error = mString
acc.Error = string(res)
}
acc.Request_time = tools.JsonTime(time.Now())
acc.Apply_id = proxyData.Apply_id
acc.User_id = proxyData.User_id
acc.Service_id = proxyData.Service_id
acc.Date_day = time.Now().Day()
acc.RequestTime = tools.JsonTime(time.Now())
acc.ApplyId = cast.ToInt(proxyData.ApplyId)
acc.UserId = proxyData.ApplyUserId
acc.ServiceId = cast.ToInt(proxyData.ServiceId)
acc.ReqUrl = proxyData.ReqUrl
acc.DateDay = time.Now().Day()
fmt.Println(time.Now().Month().String())
acc.Date_month = int(time.Now().Month())
acc.DateMonth = int(time.Now().Month())
acc.Date_year = time.Now().Year()
acc.DateYear = time.Now().Year()
acc.Status = status
b, _ := json.Marshal(acc)
if errs := redis.Conn.RPush(config.CallRecord, b).Err(); errs != nil {
......@@ -189,24 +194,39 @@ func CallCounts(proxyData model.ProxyData) (err error) {
date := time.Now().Format(config.LocalDateFormat)
session := db.NewSession()
session.Begin()
sql1 := fmt.Sprintf(`
if proxyData.ApiId == 0 {
sql1 := fmt.Sprintf(`
LOCK TABLE service IN SHARE ROW EXCLUSIVE MODE;
update service set request_count =request_count+1,req_count =req_count+1 where id = %d;
update service_apply set request_count =request_count+1 where id = %d;
`, proxyData.ServiceId, proxyData.ApplyId)
_, err = session.SQL(sql1).Execute()
if err != nil {
fmt.Println("error******service、service_apply更新失败:" + err.Error())
return
}
} else {
sql1 := fmt.Sprintf(`
LOCK TABLE service IN SHARE ROW EXCLUSIVE MODE;
update service set request_count =request_count+1 where id = %d;
update service_req_extend set req_count =req_count+1 where id = %d;
update service_apply set request_count =request_count+1 where id = %d;
`, proxyData.Service_id, proxyData.Apply_id)
_, err = session.SQL(sql1).Execute()
if err != nil {
fmt.Println("error******service、service_apply更新失败:" + err.Error())
return
`, proxyData.ServiceId, proxyData.ApiId, proxyData.ApplyId)
_, err = session.SQL(sql1).Execute()
if err != nil {
fmt.Println("error******service、service_apply更新失败:" + err.Error())
return
}
}
session.Commit()
session.Begin()
sql := fmt.Sprintf(`
LOCK TABLE service_daliy_count IN SHARE ROW EXCLUSIVE MODE;
INSERT INTO service_daliy_count ( service_id,date,count) VALUES
(%d,'%s',%d) ON conflict (service_id,date) DO
UPDATE SET count=(service_daliy_count.count+1);
`, proxyData.Service_id, date, 1)
`, proxyData.ServiceId, date, 1)
_, err = session.SQL(sql).Execute()
if err != nil {
fmt.Println("error******service_daliy_count更新失败:" + err.Error())
......@@ -220,7 +240,7 @@ LOCK TABLE service_count_person IN SHARE ROW EXCLUSIVE MODE;
INSERT INTO service_count_person ( user_id,date,count) VALUES
('%s','%s',%d) ON conflict (user_id,date) DO
UPDATE SET count=(service_count_person.count+1);
`, proxyData.User_id, date, 1)
`, proxyData.ApplyUserId, date, 1)
_, err = session.SQL(sql3).Execute()
if err != nil {
fmt.Println("error****** service_count_person 更新失败:" + err.Error())
......@@ -243,20 +263,21 @@ func ApplyCallCounts(proxyData model.ProxyData) (err error) {
return
}
//date := time.Now().Format(config.LocalDateTimeFormat)
err = redis.RPush(fmt.Sprintf("%s", config.ApplyTag), proxyData.Apply_id)
err = redis.RPush(fmt.Sprintf("%s", config.ApplyTag), proxyData.ApplyId)
fmt.Println("rpush data to list failed:", err)
return
}
//过滤返回字段
func FiledFilter(proxyData model.ProxyData, respbody []byte) interface{} {
fields := []request.ServiceField{}
func FiledFilter(proxyData model.ProxyData, respbody []byte) []byte {
var fields []request.ServiceField
fields = dataconvertutil.GetResponseField(proxyData.ResFields)
_, arrmodel := dataconvertutil.ConvertJson(fields)
realData := make(map[string]interface{})
json.Unmarshal(respbody, &realData)
if arrmodel != nil && len(arrmodel) != 0 && len(realData) != 0 {
return Change(arrmodel[0], realData)
bytes, _ := json.Marshal(Change(arrmodel[0], realData))
return bytes
}
return respbody
}
......@@ -266,7 +287,7 @@ func Change(model interface{}, returnData interface{}) interface{} {
//switch t := model.(type) {
switch model.(type) {
case map[string]interface{}:
// TODO 判定值是否相等
// 判定值是否相等
for k, v := range returnData.(map[string]interface{}) {
flag := false
for k1, v1 := range model.(map[string]interface{}) {
......@@ -313,7 +334,7 @@ func Change(model interface{}, returnData interface{}) interface{} {
}
break
default:
// TODO 判断old是否有这个值
// 判断old是否有这个值
//fmt.Println("数组类型 |||||||||||||||||||||||||||||||| old=====new=====>: ", model, returnData)
}
}
......@@ -339,16 +360,16 @@ func UpdateCallsCount(applyId string) (err error) {
//返回过滤后的敏感字段
func SensitiveFilter(proxyData model.ProxyData, res interface{}) interface{} {
fields := []request.ServiceField{}
fields = dataconvertutil.GetResponseField(proxyData.Sensituve_word)
_, arrSensituve_word := dataconvertutil.ConvertJson(fields)
var fields []request.ServiceField
fields = dataconvertutil.GetResponseField(proxyData.SensituveWord)
_, arrsensituveWord := dataconvertutil.ConvertJson(fields)
//if Sensituve_word != nil && len(Sensituve_word) != 0 {
// return FilterSensituveField(Sensituve_word, res)
//} else if arrSensituve_word != nil && len(arrSensituve_word) != 0 {
// return FilterSensituveField(arrSensituve_word, res)
//}
if arrSensituve_word != nil && len(arrSensituve_word) != 0 {
return Change(arrSensituve_word[0], res)
if arrsensituveWord != nil && len(arrsensituveWord) != 0 {
return Change(arrsensituveWord[0], res)
}
return res
}
......@@ -358,7 +379,7 @@ func FilterSensituveField(model interface{}, returnData interface{}) (body inter
//switch t := model.(type) {
switch model.(type) {
case map[string]interface{}:
// TODO 判定值是否相等
// 判定值是否相等
for k, v := range returnData.(map[string]interface{}) {
//flag := false
for k1, v1 := range model.(map[string]interface{}) {
......@@ -432,37 +453,55 @@ func FilterSensituveField(model interface{}, returnData interface{}) (body inter
}
// 获取真实地址和返回结构、调用限定次数
func GetRealPath(applyId, apiId string) (res model.ProxyData, err error) {
func GetRealPath(applyId, apiId string) (model.ProxyData, error) {
db, err := client.GetConnect()
if err != nil {
return
return model.ProxyData{}, err
}
var res model.ProxyData
apiid := cast.ToInt64(apiId)
if apiid == 0 {
}
has, err := db.
Select(`service_apply.res_fields as res_fields,service.req_url as req_url,service_request_spcs.count as count,service_safe_config.sensituve_word as sensituve_word
,service_request_spcs.sensitive_count as sensitive_count,service_apply.service_id as service_id,service_apply.id as apply_id,service_apply.user_id as user_id,service.data_service_type1,domains.name,service_apply.second_level`).
Table(`service`).
Join(`inner`, `service_apply`, `service_apply.service_id = service.id`).
Join(`left`, `service_request_spcs`, `service_request_spcs.id = service_apply.request_spcs_id`).
Join(`left`, `service_safe_config`, `service_apply.service_id = service_safe_config.service_id`).
Join("inner", "domains", "domains.id = service.data_service_type1").
Where(`service_apply.uuid=?`, applyId).Get(&res)
if !has {
err = errors.New(`未找到发布的服务!`)
has, err := db.NewSession().Select("ssc.sensituve_word,sa.service_id,sa.id as apply_id, s.req_url,sa.request_count,sa.duration,sa.duration_unit,sa.spcs_type,sa.spcs_count,sa.res_fields,s.data_service_type1,s.data_service_type2 ,s.data_service_type3,sa.second_level,sa.service_end_time,s.req_name,s.state as service_state,sa.pay_status,sa.user_id as apply_user_id,s.user_id as service_user_id,s.organization as service_oid,sa.apply_oid").Table("service_apply").Alias("sa").Join("inner", []string{"service", "s"}, "sa.service_id=s.id").Join("left", []string{"service_safe_config", "ssc"}, "ssc.service_id=sa.service_id").Where("s.is_deleted =0 and sa.is_deleted =0 and s.state in(1,3) and sa.uuid=?", applyId).Get(&res)
if err != nil {
log.Println(err)
return model.ProxyData{}, errors.New(`未找到发布的服务!`)
} else if !has {
return model.ProxyData{}, errors.New(`未找到发布的服务!`)
}
} else {
has, err := db.NewSession().Select("ssc.sensituve_word,sre.id as api_id,sa.service_id,sa.id as apply_id,sre.req_url,sa.request_count,sa.duration,sa.duration_unit,sa.spcs_type,sa.spcs_count,sa.res_fields,s.data_service_type1,s.data_service_type2 ,s.data_service_type3,sa.second_level,sa.service_end_time,sre.req_name,s.state as service_state,sa.pay_status,sa.user_id as apply_user_id,s.user_id as service_user_id,s.organization as service_oid,sa.apply_oid").Table("service_apply").Alias("sa").Join("inner", []string{"service", "s"}, "sa.service_id=s.id").Join("inner", []string{"service_req_extend", "sre"}, "sre.service_id = sa.service_id and sre.id =?", apiid).Join("left", []string{"service_safe_config", "ssc"}, "ssc.service_id=sa.service_id").Where("s.is_deleted =0 and sa.is_deleted =0 and s.state in(1,3) and sa.uuid=?", applyId).Get(&res)
if err != nil {
log.Println(err)
return model.ProxyData{}, errors.New(`未找到发布的服务!`)
} else if !has {
return model.ProxyData{}, errors.New(`未找到发布的服务!`)
}
}
return
return res, nil
}
// 获取真实地址
func GetReqPath() (res tables.Service, err error) {
func GetReqPath(apiId string) (tables.Service, error) {
db, err := client.GetConnect()
if err != nil {
return
return tables.Service{}, err
}
res = tables.Service{}
_, err = db.ID(config.MeshId).Get(&res)
return
res := tables.Service{}
apiid := cast.ToInt64(apiId)
if apiid == 0 {
if has, err := db.ID(config.MeshId).Get(&res); err != nil {
return tables.Service{}, err
} else if !has {
return tables.Service{}, errors.New("服务不存在")
}
} else {
if has, err := db.NewSession().Where("service_id=? and id=?", config.MeshId, apiid).Table("service_req_extend").Get(&res); err != nil {
return tables.Service{}, err
} else if !has {
return tables.Service{}, errors.New("服务不存在")
}
}
return res, nil
}
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