module.go 3.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package service

import (
	"fmt"
Y
Your Name 已提交
5 6

	dao_service2 "github.com/eolinker/goku-api-gateway/server/dao/console-sqlite3/dao-service"
黄孟柱 已提交
7 8
	driver2 "github.com/eolinker/goku-api-gateway/server/driver"
	entity "github.com/eolinker/goku-api-gateway/server/entity/console-entity"
E
eoLinker API Management 已提交
9 10
)

Y
Your Name 已提交
11 12
const _TableName = "goku_service_config"

Y
Your Name 已提交
13
//Add 新增服务发现
E
eoLinker API Management 已提交
14
func Add(param *AddParam) error {
Y
Your Name 已提交
15
	err := dao_service2.Add(param.Name, param.Driver, param.Desc, param.Config, param.ClusterConfig, false, param.HealthCheck, param.HealthCheckPath, param.HealthCheckCode, param.HealthCheckPeriod, param.HealthCheckTimeOut)
E
eoLinker API Management 已提交
16 17 18 19

	return err
}

Y
Your Name 已提交
20
//Save 保存服务发现
E
eoLinker API Management 已提交
21 22
func Save(param *AddParam) error {

Y
Your Name 已提交
23
	v, e := dao_service2.Get(param.Name)
E
eoLinker API Management 已提交
24 25 26 27 28 29 30 31
	if e != nil {
		return e
	}

	if v.Driver != param.Driver {
		return fmt.Errorf("not allowed change dirver from %s to %s for service", v.Driver, param.Driver)
	}

Y
Your Name 已提交
32 33
	err := dao_service2.Save(param.Name, param.Desc, param.Config, param.ClusterConfig, param.HealthCheck, param.HealthCheckPath, param.HealthCheckCode, param.HealthCheckPeriod, param.HealthCheckTimeOut)

E
eoLinker API Management 已提交
34 35
	return err
}
Y
Your Name 已提交
36

Y
Your Name 已提交
37
//Get 通过名称获取服务发现信息
E
eoLinker API Management 已提交
38
func Get(name string) (*Info, error) {
Y
Your Name 已提交
39
	v, err := dao_service2.Get(name)
E
eoLinker API Management 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	if err != nil {
		return nil, err
	}

	return &Info{
		Service:            tran(v),
		Config:             v.Config,
		ClusterConfig:      v.ClusterConfig,
		HealthCheckPath:    v.HealthCheckPath,
		HealthCheckPeriod:  v.HealthCheckPeriod,
		HealthCheckCode:    v.HealthCheckCode,
		HealthCheckTimeOut: v.HealthCheckTimeOut,
	}, nil
}

Y
Your Name 已提交
55
//Delete 批量删除服务发现
E
eoLinker API Management 已提交
56 57 58 59 60 61 62 63
func Delete(names []string) error {

	for _, n := range names {
		if !ValidateName(n) {
			return fmt.Errorf("invalid name:%s", n)
		}
	}

Y
Your Name 已提交
64
	return dao_service2.Delete(names)
E
eoLinker API Management 已提交
65 66
}

Y
Your Name 已提交
67
//SetDefaut 设置默认服务发现
E
eoLinker API Management 已提交
68
func SetDefaut(name string) error {
Y
Your Name 已提交
69
	return dao_service2.SetDefault(name)
E
eoLinker API Management 已提交
70 71 72 73 74 75 76 77 78 79 80
}
func tran(v *entity.Service) *Service {
	s := &Service{
		Simple: Simple{
			Name:   v.Name,
			Driver: v.Driver,
		},
		Desc:        v.Desc,
		IsDefault:   v.IsDefault,
		HealthCheck: v.HealthCheck,
		UpdateTime:  v.UpdateTime,
Y
Your Name 已提交
81
		CreateTime:  v.CreateTime,
E
eoLinker API Management 已提交
82 83 84 85 86 87 88 89 90 91 92 93
	}

	d, has := driver2.Get(v.Driver)
	if has {
		s.DriverTitle = d.Title
		s.Type = d.Type
	} else {
		s.DriverTitle = "unknown"
		s.Type = "unknown"
	}
	return s
}
Y
Your Name 已提交
94

Y
Your Name 已提交
95
//List 获取服务发现列表
E
eoLinker API Management 已提交
96
func List(keyword string) ([]*Service, error) {
Y
Your Name 已提交
97
	vs, e := dao_service2.List(keyword)
E
eoLinker API Management 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
	if e != nil {
		return nil, e
	}
	list := make([]*Service, 0, len(vs))

	for _, v := range vs {

		list = append(list, tran(v))

	}
	return list, nil
}

Y
Your Name 已提交
111
//SimpleList 获取简易服务发现列表
E
eoLinker API Management 已提交
112
func SimpleList() ([]*Simple, string, error) {
Y
Your Name 已提交
113
	vs, e := dao_service2.List("")
E
eoLinker API Management 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if e != nil {
		return nil, "", e
	}
	list := make([]*Simple, 0, len(vs))
	defaultName := ""
	for _, v := range vs {

		if v.IsDefault {
			defaultName = v.Name
		}
		s := &Simple{
			Name:   v.Name,
			Driver: v.Driver,
		}

		d, has := driver2.Get(v.Driver)
		if has {
			s.DriverTitle = d.Title
			s.Type = d.Type
		} else {
			s.DriverTitle = "unknown"
			s.Type = "unknown"
		}

		list = append(list, s)
	}
	return list, defaultName, nil
}