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

import (
	"fmt"
黄孟柱 已提交
5 6 7 8
	"github.com/eolinker/goku-api-gateway/server/dao"
	dao_service "github.com/eolinker/goku-api-gateway/server/dao/console-mysql/dao-service"
	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 add
E
eoLinker API Management 已提交
14 15 16 17 18 19 20 21 22 23
func Add(param *AddParam) error {

	err := dao_service.Add(param.Name, param.Driver, param.Desc, param.Config, param.ClusterConfig, false, param.HealthCheck, param.HealthCheckPath, param.HealthCheckCode, param.HealthCheckPeriod, param.HealthCheckTimeOut)

	if err == nil {
		dao.UpdateTable(_TableName)
	}
	return err
}

Y
Your Name 已提交
24
//Save save
E
eoLinker API Management 已提交
25 26 27 28 29 30 31 32 33 34 35
func Save(param *AddParam) error {

	v, e := dao_service.Get(param.Name)
	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 已提交
36
	err := dao_service.Save(param.Name, param.Desc, param.Config, param.ClusterConfig, param.HealthCheck, param.HealthCheckPath, param.HealthCheckCode, param.HealthCheckPeriod, param.HealthCheckTimeOut)
E
eoLinker API Management 已提交
37 38 39 40 41
	if err == nil {
		dao.UpdateTable(_TableName)
	}
	return err
}
Y
Your Name 已提交
42 43

//Get get
E
eoLinker API Management 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
func Get(name string) (*Info, error) {
	v, err := dao_service.Get(name)
	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 已提交
61
//Delete delete
E
eoLinker API Management 已提交
62 63 64 65 66 67 68 69 70 71 72
func Delete(names []string) error {

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

	return dao_service.Delete(names)
}

Y
Your Name 已提交
73
//SetDefaut 设置默认值
E
eoLinker API Management 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
func SetDefaut(name string) error {
	return dao_service.SetDefault(name)
}
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 已提交
87
		CreateTime:  v.CreateTime,
E
eoLinker API Management 已提交
88 89 90 91 92 93 94 95 96 97 98 99
	}

	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 已提交
100 101

//List 获取列表
E
eoLinker API Management 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
func List(keyword string) ([]*Service, error) {
	vs, e := dao_service.List(keyword)
	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 已提交
117
//SimpleList 获取简易列表
E
eoLinker API Management 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
func SimpleList() ([]*Simple, string, error) {
	vs, e := dao_service.List("")
	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
}