Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"tree_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/config/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"tree.go",
],
importpath = "go-common/app/admin/main/config/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/config/conf:go_default_library",
"//app/admin/main/config/model:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,54 @@
package dao
import (
"context"
"go-common/app/admin/main/config/conf"
"go-common/library/database/orm"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
// Dao dao.
type Dao struct {
c *conf.Config
DB *gorm.DB
DBApm *gorm.DB
client *bm.Client
}
// New new a dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
DB: orm.NewMySQL(c.ORM),
DBApm: orm.NewMySQL(c.ORMApm),
client: bm.NewClient(c.HTTPClient),
}
d.initORM()
return
}
func (d *Dao) initORM() {
d.DB.LogMode(true)
d.DBApm.LogMode(true)
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
if d.DB != nil {
err = d.DB.DB().PingContext(c)
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
if d.DBApm != nil {
d.DBApm.Close()
}
}

View File

@@ -0,0 +1,45 @@
package dao
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/admin/main/config/conf"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.common-arch.config-admin")
flag.Set("conf_token", "0OuIaZNZU2Yfa4lXM6e4p2XeGtiQuwyF")
flag.Set("tree_id", "9366")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/config-admin-example.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,201 @@
package dao
import (
"bytes"
"context"
"fmt"
"net/http"
"strings"
"time"
"go-common/app/admin/main/config/model"
"go-common/library/ecode"
"go-common/library/log"
)
var (
tokenURI = "http://easyst.bilibili.co/v1/token"
dataURI = "http://easyst.bilibili.co/v1/node/apptree"
authURI = "http://easyst.bilibili.co/v1/auth"
nodeURI = "http://easyst.bilibili.co/v1/node/bilibili%s"
appsURI = "http://easyst.bilibili.co/v1/node/role/app"
prefix = []byte("bilibili.")
)
// Token get Token.
func (d *Dao) Token(c context.Context, body string) (msg map[string]interface{}, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("POST", tokenURI, strings.NewReader(body)); err != nil {
log.Error("Token url(%s) error(%v)", tokenURI, err)
return
}
var res struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Token url(%s) res(%+v) err(%v)", tokenURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", tokenURI, res)
return
}
msg = res.Data
return
}
// Auth get Token.
func (d *Dao) Auth(c context.Context, cookie string) (msg map[string]interface{}, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("GET", authURI, nil); err != nil {
log.Error("Token url(%s) error(%v)", tokenURI, err)
return
}
req.Header.Set("Cookie", cookie)
var res struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Token url(%s) res($s) err(%v)", tokenURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", tokenURI, res)
return
}
msg = res.Data
return
}
// Tree get service tree.
func (d *Dao) Tree(c context.Context, token string) (data interface{}, err error) {
var (
req *http.Request
tmp map[string]interface{}
ok bool
)
if req, err = http.NewRequest("GET", dataURI, nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data map[string]map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", dataURI, res)
return
}
if tmp, ok = res.Data["bilibili"]; ok {
data, ok = tmp["children"]
}
if !ok {
err = ecode.NothingFound
}
return
}
// Role get service tree.
func (d *Dao) Role(c context.Context, user, token string) (nodes *model.CacheData, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("GET", appsURI, nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data []*model.RoleNode `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", dataURI, res)
return
}
nodes = &model.CacheData{Data: make(map[int64]*model.RoleNode)}
nodes.CTime = time.Now()
for _, node := range res.Data {
if bytes.Equal(prefix, []byte(node.Path)[0:9]) {
node.Path = string([]byte(node.Path)[9:])
}
nodes.Data[node.ID] = node
}
return
}
// NodeTree get service tree.
func (d *Dao) NodeTree(c context.Context, token, bu, team string) (nodes []*model.Node, err error) {
var (
req *http.Request
node string
)
if len(bu) != 0 {
node = "." + bu
}
if len(team) != 0 {
node = "." + team
}
if len(node) == 0 {
nodes = append(nodes, &model.Node{Name: "main", Path: "main"})
nodes = append(nodes, &model.Node{Name: "ai", Path: "ai"})
return
}
if req, err = http.NewRequest("GET", fmt.Sprintf(nodeURI, node), nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data *model.Res `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
for _, tnode := range res.Data.Data {
if bytes.Equal(prefix, []byte(tnode.Path)[0:9]) {
tnode.Path = string([]byte(tnode.Path)[9:])
}
nodes = append(nodes, &model.Node{Name: tnode.Name, Path: tnode.Path})
}
return
}

View File

@@ -0,0 +1,137 @@
package dao
import (
"context"
"net/http"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig"
func TestToken(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("get token", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("POST", "http://easyst.bilibili.co/v1/token").Reply(200).JSON(`{
"code": 90000,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig",
"user_name": "main",
"secret": "",
"expired": 1540641724
},
"message": "success",
"status": 200
}`)
data, err := d.Token(context.Background(), "")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestAuth(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("get auth", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/auth").Reply(200).JSON(`{"code":90000,"message":"success"}`)
_, err := d.Auth(context.Background(), "sven-apm=afab110001a20fa3a1c9b5bd67564ccd71c6253406725184bfa5d88c7ece9d17; Path=/; Domain=bilibili.co; Expires=Tue, 23 Oct 2018 07:25:02 GMT; Max-Age=1800; HttpOnly")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestRole(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("TestRole", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/role/app").Reply(200).JSON(`{"code":90000,"message":"success"}`)
data, err := d.Role(context.Background(), "linli", token)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestNodeTree(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("NodeTree", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/bilibili.main.common-arch").Reply(200).JSON(`{
"status": 200,
"message": "success",
"code": 90000,
"data": {
"count": 0,
"results": 20,
"page": 1,
"data": null
}
}`)
_, err := d.NodeTree(context.Background(), token, "main.common-arch", "")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestTree(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("TestTree", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/apptree").Reply(200).JSON(`{
"code": 90000,
"data": {
"bilibili": {
"id": 0,
"name": "bilibili",
"alias": "哔哩哔哩",
"type": 1,
"path": "bilibili",
"tags": {},
"children": null
}
},
"message": "success",
"status": 200
}`)
_, err := d.Tree(context.Background(), token)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}