/
service_detail_model.go
53 lines (47 loc) · 1.29 KB
/
service_detail_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package model
import (
"errors"
"regexp"
"strings"
"github.com/bitly/go-simplejson"
"github.com/didi/gatekeeper/public"
"github.com/gin-gonic/gin"
)
type ServiceDetail struct {
Info *ServiceInfo `json:"info" description:"基本信息"`
PluginConf *simplejson.Json `json:"plugin_conf" description:"plugin_conf"`
}
type UpstreamConfig struct {
Schema string
IpList []string
IpWeight map[string]string
}
func GetUpstreamConfigFromString(upstreamList string) (*UpstreamConfig, error) {
config := &UpstreamConfig{}
if upstreamList == "" {
return config, nil
}
tmpLine := strings.Split(upstreamList, "\n")
ipList := []string{}
ipConf := map[string]string{}
for _, tmp := range tmpLine {
r, _ := regexp.Compile("^(.*://)(.*?)\\s(.*?)$")
submatch := r.FindStringSubmatch(tmp)
if len(submatch) != 4 {
return nil, errors.New("upstream_list format error")
}
config.Schema = submatch[1]
ipList = append(ipList, submatch[2])
ipConf[submatch[2]] = submatch[3]
}
config.IpList = ipList
config.IpWeight = ipConf
return config, nil
}
func GetServiceDetailFromGinContext(c *gin.Context) (*ServiceDetail, error) {
serverInterface, ok := c.Get(public.ServiceDetailContextKey)
if !ok {
return nil, errors.New("service not found")
}
return serverInterface.(*ServiceDetail), nil
}