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,47 @@
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"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/up/conf:go_default_library",
"//app/service/main/up/dao:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/service/main/up/dao/monitor",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/up/conf:go_default_library",
"//app/service/main/up/model:go_default_library",
"//library/log: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,120 @@
package monitor
import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"go-common/app/service/main/up/conf"
"go-common/app/service/main/up/model"
"go-common/library/log"
)
const (
_uri = "/api/v1/message/add"
_method = "POST"
_fileType = "application/json"
)
// Dao is message dao.
type Dao struct {
c *conf.Config
client *http.Client
url string
}
// New new a message dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: &http.Client{
Timeout: time.Duration(time.Second * 1),
},
url: c.Monitor.Host + _uri,
}
return
}
// Send send exception message to owner.
func (d *Dao) Send(c context.Context, username, msg string) (err error) {
params := url.Values{}
now := time.Now().Unix()
params.Set("username", username)
params.Set("content", msg)
params.Set("title", "test")
params.Set("url", "")
params.Set("type", "wechat")
params.Set("token", d.c.Monitor.AppToken)
params.Set("timestamp", strconv.FormatInt(now, 10))
bap := &model.BAP{
UserName: params.Get("username"),
Content: params.Get("content"),
Title: params.Get("title"),
URL: params.Get("url"),
Ty: params.Get("type"),
Token: params.Get("token"),
TimeStamp: now,
Signature: d.getSign(params),
}
jsonStr, err := json.Marshal(bap)
if err != nil {
log.Error("monitor json.Marshal error (%v)", err)
return
}
req, err := http.NewRequest(_method, d.url, bytes.NewBuffer(jsonStr))
if err != nil {
log.Error("monitor http.NewRequest error (%v)", err)
return
}
req.Header.Add("Content-Type", _fileType)
// timeout
ctx, cancel := context.WithTimeout(c, 800*time.Millisecond)
req = req.WithContext(ctx)
defer cancel()
response, err := d.client.Do(req)
if err != nil {
log.Error("monitor d.client.Post error(%v)", err)
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
log.Error("monitor http.StatusCode nq http.StatusOK (%d) | url(%s)", response.StatusCode, d.url)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error("monitor ioutil.ReadAll error(%v)", err)
return
}
var result struct {
Status int `json:"status"`
Msg string `json:"msg"`
}
if err = json.Unmarshal(body, &result); err != nil {
log.Error("monitor json.Unmarshal error(%v)", err)
}
if result.Status != 0 {
log.Error("monitor get status(%d) msg(%s)", result.Status, result.Msg)
}
return
}
func (d *Dao) getSign(params url.Values) (sign string) {
for k, v := range params {
if len(v) == 0 {
params.Del(k)
}
}
h := md5.New()
io.WriteString(h, params.Encode()+d.c.Monitor.AppSecret)
sign = fmt.Sprintf("%x", h.Sum(nil))
return
}

View File

@@ -0,0 +1,69 @@
package monitor
import (
"context"
"flag"
"go-common/app/service/main/up/conf"
"net/url"
"os"
"testing"
"github.com/smartystreets/goconvey/convey"
"go-common/app/service/main/up/dao"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", dao.AppID)
flag.Set("conf_token", dao.UatToken)
flag.Set("tree_id", dao.TreeID)
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/up-service.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/up-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestMonitorSend(t *testing.T) {
var (
c = context.TODO()
username = ""
msg = ""
)
convey.Convey("Send", t, func(ctx convey.C) {
err := d.Send(c, username, msg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestMonitorgetSign(t *testing.T) {
var (
params url.Values
)
convey.Convey("getSign", t, func(ctx convey.C) {
sign := d.getSign(params)
ctx.Convey("Then sign should not be nil.", func(ctx convey.C) {
ctx.So(sign, convey.ShouldNotBeNil)
})
})
}