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,59 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"app_auth_test.go",
"dao_test.go",
"mysql_test.go",
"tree_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/msm/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"app_auth.go",
"dao.go",
"mysql.go",
"tree.go",
],
importpath = "go-common/app/service/main/msm/dao",
tags = ["automanaged"],
deps = [
"//app/service/main/msm/conf:go_default_library",
"//app/service/main/msm/model:go_default_library",
"//library/conf/env:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/time:go_default_library",
],
)

View File

@@ -0,0 +1,59 @@
package dao
import (
"context"
"go-common/app/service/main/msm/model"
"go-common/library/log"
)
const (
_allAppInfoSQL = "SELECT app_tree_id,app_id,`limit` FROM app"
_allAppAuthSQL = "SELECT service_tree_id,app_tree_id,rpc_method,http_method,quota,mtime FROM app_auth"
)
// AllAppsInfo AllAppsInfo.
func (d *Dao) AllAppsInfo(c context.Context) (res map[int64]*model.AppInfo, err error) {
rows, err := d.db.Query(c, _allAppInfoSQL)
if err != nil {
log.Error("d.apmDB.Query(app) error(%v)", err)
return
}
defer rows.Close()
res = make(map[int64]*model.AppInfo)
for rows.Next() {
app := &model.AppInfo{}
if err = rows.Scan(&app.AppTreeID, &app.AppID, &app.Limit); err != nil {
log.Error("rows.Scan(app) error(%v)", err)
return
}
res[app.AppTreeID] = app
}
return
}
// AllAppsAuth get all app auth info.
func (d *Dao) AllAppsAuth(c context.Context) (res map[int64]map[int64]*model.AppAuth, err error) {
rows, err := d.db.Query(c, _allAppAuthSQL)
if err != nil {
log.Error("d.apmDB.Query(app_auth) error(%v)", err)
return
}
defer rows.Close()
res = make(map[int64]map[int64]*model.AppAuth)
for rows.Next() {
appAuth := &model.AppAuth{}
if err = rows.Scan(&appAuth.ServiceTreeID, &appAuth.AppTreeID, &appAuth.RPCMethod, &appAuth.HTTPMethod, &appAuth.Quota, &appAuth.MTime); err != nil {
log.Error("rows.Scan(appAuth) error(%v)", err)
return
}
if _, b := res[appAuth.ServiceTreeID]; b {
res[appAuth.ServiceTreeID][appAuth.AppTreeID] = appAuth
} else {
authMap := make(map[int64]*model.AppAuth)
authMap[appAuth.AppTreeID] = appAuth
res[appAuth.ServiceTreeID] = authMap
}
}
return
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAllAppsInfo(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("AllAppsInfo", t, func(ctx convey.C) {
res, err := d.AllAppsInfo(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDaoAllAppsAuth(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("AllAppsAuth", t, func(ctx convey.C) {
res, err := d.AllAppsAuth(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"go-common/app/service/main/msm/conf"
"go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
)
// Dao dao.
type Dao struct {
client *bm.Client
db *sql.DB
treeHost string
platformID string
}
// New new dao.
func New(c *conf.Config) *Dao {
d := &Dao{
db: sql.NewMySQL(c.Mysql),
client: bm.NewClient(c.HTTPClient),
treeHost: c.Tree.Host,
platformID: c.Tree.PlatformID,
}
return d
}
// Close close mysql resource.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/service/main/msm/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "msm-service")
flag.Set("conf_token", "b1ece1c6fa8a11e788920a0b41d970e6")
flag.Set("tree_id", "2888")
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/msm-service-example.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,114 @@
package dao
import (
"container/list"
"context"
"go-common/app/service/main/msm/model"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_codesSQL = "SELECT code, message, mtime FROM codes"
_diffCodesSQL = "SELECT code, message, mtime FROM codes WHERE mtime > ? ORDER BY mtime LIMIT 100"
_codesLangsSQL = "select a.code,a.message,a.mtime,IFNULL(b.locale,''),IFNULL(b.msg,''),IFNULL(b.mtime,'') as bmtime from codes as a left join code_msg as b on a.id=b.code_id"
)
// Codes get all codes.
func (d *Dao) Codes(c context.Context) (codes map[int]string, lcode *model.Code, err error) {
var (
code int
msg string
tmp int64
mtime xtime.Time
)
rows, err := d.db.Query(c, _codesSQL)
if err != nil {
log.Error("d.db.Query(%v) error(%v)", _codesSQL, err)
return
}
defer rows.Close()
lcode = &model.Code{}
codes = make(map[int]string)
for rows.Next() {
if err = rows.Scan(&code, &msg, &mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
codes[code] = msg
if int64(mtime) > tmp {
lcode.Code = code
lcode.Ver = int64(mtime)
lcode.Msg = msg
tmp = int64(mtime)
}
}
return
}
// Diff get change codes.
func (d *Dao) Diff(c context.Context, ver int64) (vers *list.List, err error) {
var (
code int
msg string
mtime xtime.Time
)
rows, err := d.db.Query(c, _diffCodesSQL, xtime.Time(ver))
if err != nil {
log.Error("d.db.Query(%v) error(%v)", _diffCodesSQL, err)
return
}
defer rows.Close()
vers = list.New()
for rows.Next() {
if err = rows.Scan(&code, &msg, &mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
vers.PushBack(&model.Code{Ver: int64(mtime), Code: code, Msg: msg})
}
return
}
// CodesLang get all codes.
func (d *Dao) CodesLang(c context.Context) (codes map[int]map[string]string, lcode *model.CodeLangs, err error) {
var (
code int
tmp int64
mtime xtime.Time
message string
bl string
bmsg string
bmtime xtime.Time
)
rows, err := d.db.Query(c, _codesLangsSQL)
if err != nil {
log.Error("d.db.Query(%v) error(%v)", _codesLangsSQL, err)
return
}
defer rows.Close()
lcode = &model.CodeLangs{}
codes = make(map[int]map[string]string)
for rows.Next() {
t := make(map[string]string)
bl = ""
if err = rows.Scan(&code, &message, &mtime, &bl, &bmsg, &bmtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
t["default"] = message
if len(bl) > 0 {
t[bl] = bmsg
}
codes[code] = t
if int64(mtime) > tmp {
lcode.Code = code
lcode.Ver = int64(mtime)
lcode.Msg = t
tmp = int64(mtime)
}
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCodes(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Codes", t, func(ctx convey.C) {
codes, lcode, err := d.Codes(c)
ctx.Convey("Then err should be nil.codes,lcode should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(lcode, convey.ShouldNotBeNil)
ctx.So(codes, convey.ShouldNotBeNil)
})
})
}
func TestDaoDiff(t *testing.T) {
var (
c = context.Background()
ver = int64(0)
)
convey.Convey("Diff", t, func(ctx convey.C) {
vers, err := d.Diff(c, ver)
ctx.Convey("Then err should be nil.vers should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(vers, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,101 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"go-common/app/service/main/msm/model"
"go-common/library/conf/env"
"go-common/library/log"
)
const (
treeUsername = "msm"
treeAuthURL = "/v1/token"
allAppAuthURL = "%s/v1/node/app/secretinfo/%s"
)
func (d *Dao) treeToken(c context.Context) (token string, err error) {
var (
jsonBytes []byte
url = d.treeHost + treeAuthURL
)
body := &struct {
Username string `json:"user_name"`
PlatformID string `json:"platform_id"`
}{
Username: treeUsername,
PlatformID: d.platformID,
}
if jsonBytes, err = json.Marshal(body); err != nil {
log.Error("json.Marshal(body) error(%v)", err)
return
}
req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBytes)))
if err != nil {
log.Error("http.NewRequest failed", err)
return
}
req.Header.Set("Content-Type", "application/json")
res := &struct {
Code int64 `json:"code"`
Data struct {
Token string `json:"token"`
Username string `json:"user_name"`
Secret string `json:"secret"`
Expired int64 `json:"expired"`
} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}{}
if err = d.client.Do(c, req, res); err != nil {
log.Error("service-tree client Do failed", err)
return
}
if res.Code != 90000 {
log.Error("service-tree client Do failed", err)
return
}
token = res.Data.Token
return
}
// TreeAppInfo TreeAppInfo.
func (d *Dao) TreeAppInfo(c context.Context) (appInfo map[int64]*model.AppToken, err error) {
var (
token string
url = fmt.Sprintf(allAppAuthURL, d.treeHost, env.DeployEnv)
)
appInfo = make(map[int64]*model.AppToken)
if token, err = d.treeToken(c); err != nil {
return
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error("http.NewRequest failed", err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Authorization-Token", token)
res := &struct {
Code int `json:"code"`
Data []*model.AppToken `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}{}
if err = d.client.Do(c, req, res); err != nil {
log.Error("service-tree client Do failed", err)
return
}
if res.Code != 90000 {
log.Error("service-tree client Do failed", err)
return
}
for _, auth := range res.Data {
appInfo[auth.AppTreeID] = auth
}
return
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaotreeToken(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("treeToken", t, func(ctx convey.C) {
token, err := d.treeToken(c)
ctx.Convey("Then err should be nil.token should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(token, convey.ShouldNotBeNil)
})
})
}
func TestDaoTreeAppInfo(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("TreeAppInfo", t, func(ctx convey.C) {
appInfo, err := d.TreeAppInfo(c)
ctx.Convey("Then err should be nil.appInfo should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(appInfo, convey.ShouldNotBeNil)
})
})
}