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",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"redis_test.go",
"up_nas_test.go",
"upbfs_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/admin/main/appstatic/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",
"redis.go",
"up_nas.go",
"upbfs.go",
],
importpath = "go-common/app/admin/main/appstatic/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/appstatic/conf:go_default_library",
"//app/admin/main/appstatic/model:go_default_library",
"//library/cache/redis: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,38 @@
package dao
import (
"go-common/app/admin/main/appstatic/conf"
"go-common/library/cache/redis"
"go-common/library/database/orm"
httpx "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
// Dao .
type Dao struct {
DB *gorm.DB
c *conf.Config
client *httpx.Client
redis *redis.Pool
}
// New new a instance
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// db
DB: orm.NewMySQL(c.ORM),
c: c,
client: httpx.NewClient(c.HTTPClient),
redis: redis.NewPool(c.Redis.Config),
}
d.DB.LogMode(true)
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
}

View File

@ -0,0 +1,49 @@
package dao
import (
"flag"
"os"
"strings"
"go-common/app/admin/main/appstatic/conf"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
var d *Dao
func init() {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.appstatic-admin")
flag.Set("conf_token", "26eec0d4137fac469c03e5ae147ed101")
flag.Set("tree_id", "22976")
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/appstatic-admin-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
d.client.SetTransport(gock.DefaultTransport)
return r
}
func WithDao(f func(d *Dao)) func() {
return func() {
Reset(func() {})
f(d)
}
}

View File

@ -0,0 +1,32 @@
package dao
import (
"context"
"time"
"go-common/library/log"
)
const _pushKey = "appstatic-admin-topush"
// ZAddPush adds one to push data into the redis sorted set
func (d *Dao) ZAddPush(c context.Context, resID int) (err error) {
var (
conn = d.redis.Get(c)
ctime = time.Now().Unix()
)
defer conn.Close()
if err = conn.Send("ZADD", _pushKey, ctime, resID); err != nil {
log.Error("conn.Send(ZADD %s - %v) error(%v)", _pushKey, resID, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
return
}

View File

@ -0,0 +1,15 @@
package dao
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDao_ZAddPush(t *testing.T) {
Convey("TestDao_ZAddPush", t, WithDao(func(d *Dao) {
err := d.ZAddPush(context.Background(), 381)
So(err, ShouldBeNil)
}))
}

View File

@ -0,0 +1,129 @@
package dao
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"crypto/md5"
"encoding/hex"
"go-common/app/admin/main/appstatic/conf"
"go-common/app/admin/main/appstatic/model"
"go-common/library/log"
)
// Sign fn
func Sign(params url.Values) (query string, err error) {
if len(params) == 0 {
return
}
if params.Get("appkey") == "" {
err = fmt.Errorf("utils http get must have parameter appkey")
return
}
if params.Get("appsecret") == "" {
err = fmt.Errorf("utils http get must have parameter appsecret")
return
}
if params.Get("sign") != "" {
err = fmt.Errorf("utils http get must have not parameter sign")
return
}
// sign
secret := params.Get("appsecret")
params.Del("appsecret")
tmp := params.Encode()
if strings.IndexByte(tmp, '+') > -1 {
tmp = strings.Replace(tmp, "+", "%20", -1)
}
mh := md5.Sum([]byte(tmp + secret))
params.Set("sign", hex.EncodeToString(mh[:]))
query = params.Encode()
return
}
// get sign for NAS storage
func getSign(nas *conf.Bfs) (uri string, err error) {
var (
params = url.Values{}
query string
)
params.Set("appkey", nas.Key)
params.Set("appsecret", nas.Secret)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
if query, err = Sign(params); err != nil {
log.Error("UpNAS getSign Error (%s)-(%v)-(%v)", nas, err)
return
}
uri = nas.Host + "?" + query
return
}
// UploadNas can upload the file into Nas Storage
func (d *Dao) UploadNas(c context.Context, fileName string, data []byte, nas *conf.Bfs) (location string, err error) {
var (
req *http.Request
resp *http.Response
client = &http.Client{Timeout: time.Duration(nas.Timeout) * time.Millisecond}
url string
res = model.ResponseNas{}
)
// get sign
if url, err = getSign(nas); err != nil {
log.Error("UpNAS getSign Error (%s)-(%v)-(%v)", nas, err)
return
}
// prepare the data of the file and init the request
buf := new(bytes.Buffer)
bodyWriter := multipart.NewWriter(buf)
fileWriter, err := bodyWriter.CreateFormFile("file", fileName)
if err != nil {
log.Error("UpNAS fileWriter Error (%v)-(%v)", nas, err)
return
}
if _, err = io.Copy(fileWriter, bytes.NewReader(data)); err != nil {
log.Error("UpNAS fileWriter Copy Error (%v)-(%v)", nas, err)
return
}
bodyWriter.Close()
// request setting
if req, err = http.NewRequest(_methodNas, url, buf); err != nil {
log.Error("http.NewRequest() Upload(%v) error(%v)", url, err)
return
}
req.Header.Set("Content-Type", bodyWriter.FormDataContentType())
resp, err = client.Do(req)
// response treatment
if err != nil {
log.Error("Nas client.Do(%s) error(%v)", url, err)
return
}
defer resp.Body.Close()
log.Info("NasAPI returns (%v)", resp)
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Nas status code error:%v", resp.StatusCode)
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err = json.Unmarshal(respBody, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(respBody), err)
return
}
log.Info("NasAPI res struct (%v)", res)
location = res.Data
// workaround solution for Macross Upload URL issue
if d.c.Nas.NewURL != "" {
location = strings.Replace(location, d.c.Nas.OldURL, d.c.Nas.NewURL, -1)
log.Error("NasURL replace [%s] to [%s]", res.Data, location)
}
return
}

View File

@ -0,0 +1,55 @@
package dao
import (
"context"
"net/url"
"strconv"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSign(t *testing.T) {
var (
params = url.Values{}
nas = d.c.Nas
)
params.Set("appkey", nas.Key)
params.Set("appsecret", nas.Secret)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
convey.Convey("Sign", t, func(ctx convey.C) {
query, err := Sign(params)
ctx.Convey("Then err should be nil.query should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(query, convey.ShouldNotBeNil)
})
})
}
func TestDaogetSign(t *testing.T) {
var nas = d.c.Nas
convey.Convey("getSign", t, func(ctx convey.C) {
uri, err := getSign(nas)
ctx.Convey("Then err should be nil.uri should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(uri, convey.ShouldNotBeNil)
})
})
}
func TestDaoUploadNas(t *testing.T) {
var (
c = context.Background()
fileName = "test.txt"
data = []byte("test123")
nas = d.c.Nas
)
convey.Convey("UploadNas", t, func(ctx convey.C) {
location, err := d.UploadNas(c, fileName, data, nas)
ctx.Convey("Then err should be nil.location should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(location, convey.ShouldNotBeNil)
})
})
}

View File

@ -0,0 +1,89 @@
package dao
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"hash"
"net/http"
"strconv"
"time"
"go-common/app/admin/main/appstatic/conf"
"go-common/library/ecode"
"go-common/library/log"
)
// bfs info
const (
_uploadURL = "/bfs/%s/%s"
_template = "%s\n%s\n%s\n%d\n"
_method = "PUT"
_methodNas = "POST"
_bucket = "app-static"
)
// Upload upload picture or log file to bfs
func (d *Dao) Upload(c context.Context, fileName string, fileType string, timing int64, data []byte, bfs *conf.Bfs) (location string, err error) {
var (
req *http.Request
resp *http.Response
code int
client = &http.Client{Timeout: time.Duration(bfs.Timeout) * time.Millisecond}
url = fmt.Sprintf(bfs.Host+_uploadURL, _bucket, fileName)
)
// prepare the data of the file and init the request
buf := new(bytes.Buffer)
_, err = buf.Write(data)
if err != nil {
log.Error("Upload.buf.Write.error(%v)", err)
err = ecode.RequestErr
return
}
if req, err = http.NewRequest(_method, url, buf); err != nil {
log.Error("http.NewRequest() Upload(%v) error(%v)", url, err)
return
}
// request setting
authorization := authorize(bfs.Key, bfs.Secret, _method, _bucket, fileName, timing)
req.Header.Set("Date", fmt.Sprint(timing))
req.Header.Set("Authorization", authorization)
req.Header.Set("Content-Type", fileType)
resp, err = client.Do(req)
// response treatment
if err != nil {
log.Error("Bfs client.Do(%s) error(%v)", url, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Bfs status code error:%v", resp.StatusCode)
return
}
code, err = strconv.Atoi(resp.Header.Get("code"))
if err != nil || code != 200 {
err = fmt.Errorf("Bfs response code error:%v", code)
return
}
location = resp.Header.Get("Location")
return
}
// authorize returns authorization for upload file to bfs
func authorize(key, secret, method, bucket, file string, expire int64) (authorization string) {
var (
content string
mac hash.Hash
signature string
)
content = fmt.Sprintf(_template, method, bucket, file, expire)
mac = hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(content))
signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
return
}

View File

@ -0,0 +1,57 @@
package dao
import (
"context"
"fmt"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUpload(t *testing.T) {
var (
c = context.Background()
fileName = "test.txt"
fileType = "txt"
timing = time.Now().Unix()
bfs = d.c.Bfs
data = []byte("123")
url = fmt.Sprintf(bfs.Host+_uploadURL, "app-static", fileName)
)
convey.Convey("Upload", t, func(ctx convey.C) {
ctx.Convey("http code error", func(ctx convey.C) {
httpMock(_method, url).Reply(-400)
_, err := d.Upload(c, fileName, fileType, timing, data, bfs)
ctx.So(err, convey.ShouldNotBeNil)
})
ctx.Convey("business code error", func(ctx convey.C) {
httpMock(_method, url).Reply(200).JSON(`{"code":-400}`)
_, err := d.Upload(c, fileName, fileType, timing, data, bfs)
ctx.So(err, convey.ShouldNotBeNil)
})
ctx.Convey("everything is fine", func(ctx convey.C) {
httpMock(_method, url).Reply(200).SetHeader("Location", "test").SetHeader("code", "200").JSON(`{"code":200}`)
location, err := d.Upload(c, fileName, fileType, timing, data, bfs)
ctx.So(err, convey.ShouldBeNil)
ctx.So(location, convey.ShouldNotBeNil)
})
})
}
func TestDaoauthorize(t *testing.T) {
var (
key = "key"
secret = "secret"
method = "put"
bucket = "tv-cover"
file = "file"
expire = int64(0)
)
convey.Convey("authorize", t, func(ctx convey.C) {
authorization := authorize(key, secret, method, bucket, file, expire)
ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
ctx.So(authorization, convey.ShouldNotBeNil)
})
})
}