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,61 @@
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",
"memcache_test.go",
"passport_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/identify/conf:go_default_library",
"//app/service/main/identify/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
"passport.go",
],
importpath = "go-common/app/service/main/identify/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/identify/conf:go_default_library",
"//app/service/main/identify/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/stat/prom: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,73 @@
package dao
import (
"context"
"go-common/app/service/main/identify/conf"
"go-common/library/cache/memcache"
bm "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
const (
_tokenURI = "/intranet/auth/tokenInfo"
_cookieURI = "/intranet/auth/cookieInfo"
)
var (
errorsCount = prom.BusinessErrCount
cachedCount = prom.CacheHit
missedCount = prom.CacheMiss
)
// PromError prom error
func PromError(name string) {
errorsCount.Incr(name)
}
// Dao struct info of Dao
type Dao struct {
c *conf.Config
tokenURI string
cookieURI string
mc *memcache.Pool
mcLogin *memcache.Pool
client *bm.Client
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
tokenURI: c.Identify.AuthHost + _tokenURI,
cookieURI: c.Identify.AuthHost + _cookieURI,
mc: memcache.NewPool(c.Memcache),
mcLogin: memcache.NewPool(c.MemcacheLoginLog),
client: bm.NewClient(c.HTTPClient),
}
return
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
d.mc.Close()
d.mcLogin.Close()
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingMC(c); err != nil {
PromError("mc:Ping")
}
return
}
// pingMc ping memcache
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 100}
err = conn.Set(&item)
return
}

View File

@@ -0,0 +1,68 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/service/main/identify/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.identify-service")
flag.Set("conf_appid", "identify-service")
flag.Set("conf_token", "ed882ffb3c5f9ec3f4a28691a2ac3d84")
flag.Set("tree_id", "11172")
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("app_id", "main.account.identify-service")
flag.Set("conf_appid", "identify-service")
flag.Set("conf_token", "15fd302391c22077da13643e11d8bab6")
flag.Set("tree_id", "11172")
flag.Set("conf_version", "server-1")
flag.Set("deploy_env", "dev")
flag.Set("conf_env", "dev")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestDao_Ping(t *testing.T) {
convey.Convey("dao ping", t, func(ctx convey.C) {
err := d.Ping(context.Background())
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDao_pingMC(t *testing.T) {
convey.Convey("dao ping", t, func(ctx convey.C) {
err := d.pingMC(context.Background())
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,104 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/identify/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
var (
loginCacheValue = []byte("1")
)
// SetAccessCache .
func (d *Dao) SetAccessCache(c context.Context, key string, res *model.IdentifyInfo) {
conn := d.mc.Get(c)
defer conn.Close()
key = cacheKey(key)
item := &memcache.Item{Key: key, Object: res, Flags: memcache.FlagProtobuf, Expiration: res.Expires}
if err := conn.Set(item); err != nil {
log.Error("identify set error(%s,%d,%v)", key, res.Expires, err)
}
}
// AccessCache .
func (d *Dao) AccessCache(c context.Context, key string) (res *model.IdentifyInfo, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key = cacheKey(key)
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
missedCount.Incr("access_cache")
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = &model.IdentifyInfo{}
if err = conn.Scan(r, res); err != nil {
PromError("mc:json解析失败")
log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
return
}
cachedCount.Incr("access_cache")
return
}
// DelCache delete access cache.
func (d *Dao) DelCache(c context.Context, key string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key = cacheKey(key)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("dao.DelCache(%s) error(%v)", key, err)
}
return
}
func cacheKey(key string) string {
return fmt.Sprintf("i_%s", key)
}
func loginCacheKey(mid int64, ip string) string {
return fmt.Sprintf("l%d%s", mid, ip)
}
// SetLoginCache set login cache
func (d *Dao) SetLoginCache(c context.Context, mid int64, ip string, expires int32) (err error) {
key := loginCacheKey(mid, ip)
conn := d.mcLogin.Get(c)
defer conn.Close()
item := &memcache.Item{Key: key, Value: loginCacheValue, Flags: memcache.FlagRAW, Expiration: expires}
// use Add instead of Set
if err = conn.Set(item); err != nil {
log.Error("loginCache set error(%s,%v)", key, err)
}
return
}
// ExistMIDAndIP check is exist mid
func (d *Dao) ExistMIDAndIP(c context.Context, mid int64, ip string) (ok bool, err error) {
key := loginCacheKey(mid, ip)
conn := d.mcLogin.Get(c)
defer conn.Close()
_, err = conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
missedCount.Incr("isExistMID")
err = nil
return false, nil
}
log.Error("loginCache conn.Get(%s) error(%v)", key, err)
return
}
return true, nil
}

View File

@@ -0,0 +1,130 @@
package dao
import (
"context"
"testing"
"go-common/app/service/main/identify/model"
"go-common/library/cache/memcache"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSetAccessCache(t *testing.T) {
var (
c = context.Background()
key = "123"
res = &model.IdentifyInfo{Mid: 1, Csrf: "csrf", Expires: 1577811661}
)
convey.Convey("SetAccessCache", t, func(ctx convey.C) {
d.SetAccessCache(c, key, res)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestDaoAccessCache(t *testing.T) {
var (
c = context.Background()
hitKey = "123"
missKey = "miss"
)
convey.Convey("AccessCache", t, func(ctx convey.C) {
res, err := d.AccessCache(c, hitKey)
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)
ctx.So(res.Mid, convey.ShouldEqual, 1)
})
})
convey.Convey("AccessCache miss", t, func(ctx convey.C) {
res, err := d.AccessCache(c, missKey)
ctx.Convey("Then err and res should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
}
func TestDaoDelCache(t *testing.T) {
var (
c = context.Background()
hitKey = "123"
missKey = "miss"
)
convey.Convey("DelCache", t, func(ctx convey.C) {
err := d.DelCache(c, hitKey)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
convey.Convey("DelCache miss", t, func(ctx convey.C) {
err := d.DelCache(c, missKey)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaocacheKey(t *testing.T) {
var (
key = "1"
)
convey.Convey("cacheKey", t, func(ctx convey.C) {
p1 := cacheKey(key)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaologinCacheKey(t *testing.T) {
var (
mid = int64(1)
ip = "127.0.0.1"
)
convey.Convey("loginCacheKey", t, func(ctx convey.C) {
p1 := loginCacheKey(mid, ip)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldEqual, "l1127.0.0.1")
})
})
}
func TestDao_SetLoginCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
ip = "127.0.0.1"
expires = int32(1577811661)
)
convey.Convey("SetLoginCache", t, func(ctx convey.C) {
err := d.SetLoginCache(c, mid, ip, expires)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeIn, nil, memcache.ErrNotStored)
})
})
}
func TestDao_ExistMIDAndIP(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
hitIP = "127.0.0.1"
missIP = "127.0.0.2"
)
convey.Convey("IsExistMID", t, func(ctx convey.C) {
ok, err := d.ExistMIDAndIP(c, mid, hitIP)
ctx.Convey("Then err should be nil.ok should be true.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldEqual, true)
})
})
convey.Convey("IsExistMID miss", t, func(ctx convey.C) {
ok, err := d.ExistMIDAndIP(c, mid, missIP)
ctx.Convey("Then err should be nil and ok should be false.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldEqual, false)
})
})
}

View File

@@ -0,0 +1,66 @@
package dao
import (
"context"
"net/http"
"net/url"
"go-common/app/service/main/identify/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// AccessCookie .
func (d *Dao) AccessCookie(c context.Context, cookie string) (res *model.IdentifyInfo, err error) {
params := url.Values{}
// new request
req, err := d.client.NewRequest(http.MethodGet, d.cookieURI, metadata.String(c, metadata.RemoteIP), params)
if err != nil {
log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
return
}
req.Header.Set("Cookie", cookie)
var response struct {
Code int `json:"code"`
Data model.IdentifyInfo `json:"data"`
}
if err = d.client.Do(c, req, &response); err != nil {
log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
return
}
if response.Code != ecode.OK.Code() {
log.Warn("identify auth url(%s) code(%d)", req.URL.String(), response.Code)
err = ecode.Int(response.Code)
return
}
res = &response.Data
return
}
// AccessToken .
func (d *Dao) AccessToken(c context.Context, accesskey string) (res *model.IdentifyInfo, err error) {
params := url.Values{}
params.Set("access_key", accesskey)
// new request
req, err := d.client.NewRequest(http.MethodGet, d.tokenURI, metadata.String(c, metadata.RemoteIP), params)
if err != nil {
log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
return
}
var response struct {
Code int `json:"code"`
Data model.IdentifyInfo `json:"data"`
}
if err = d.client.Do(c, req, &response); err != nil {
log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
return
}
if response.Code != 0 {
log.Warn("identify auth url(%s) code(%d)", req.URL.String(), response.Code)
err = ecode.Int(response.Code)
return
}
res = &response.Data
return
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAccessCookie(t *testing.T) {
var (
c = context.Background()
cookie = "cookie=cookie"
)
convey.Convey("AccessCookie", t, func(ctx convey.C) {
res, err := d.AccessCookie(c, cookie)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
}
func TestDaoAccessToken(t *testing.T) {
var (
c = context.Background()
accesskey = "123456"
)
convey.Convey("AccessToken", t, func(ctx convey.C) {
res, err := d.AccessToken(c, accesskey)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
}