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,35 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/service/bbq/push/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/push/conf:go_default_library",
"//app/service/bbq/push/dao/jpush:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/bbq/push/dao/jpush:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,28 @@
package dao
import (
"time"
"go-common/app/service/bbq/push/conf"
"go-common/app/service/bbq/push/dao/jpush"
)
// Dao dao
type Dao struct {
c *conf.Config
JPush *jpush.Client
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
client := jpush.New(c.JPush.AppKey, c.JPush.SecretKey, time.Duration(c.JPush.Timeout))
dao = &Dao{
c: c,
JPush: client,
}
return
}
// Close close the resource.
func (d *Dao) Close() {
}

View File

@@ -0,0 +1,39 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"audience.go",
"client.go",
"message.go",
"notification.go",
"option.go",
"payload.go",
],
importpath = "go-common/app/service/bbq/push/dao/jpush",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/stat: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,12 @@
package jpush
// Audience .
type Audience struct {
Tag interface{} `json:"tag,omitempty"`
TagAnd interface{} `json:"tag_and,omitempty"`
TagNot interface{} `json:"tag_not,omitempty"`
Alias interface{} `json:"alias,omitempty"`
RegID interface{} `json:"registration_id,omitempty"`
Segment interface{} `json:"segment,omitempty"`
AbTest interface{} `json:"abtest,omitempty"`
}

View File

@@ -0,0 +1,67 @@
package jpush
import (
"bytes"
"encoding/base64"
"io/ioutil"
"net/http"
"time"
"go-common/library/stat"
"go-common/library/stat/prom"
)
const (
pushURL = "https://api.jpush.cn/v3/push"
// 如果创建的极光应用分配的北京机房,并且 API 调用方的服务器也位于北京,则比较适合调用极光北京机房的 API可以提升一定的响应速度。
// PUSH_URL = "https://bjapi.push.jiguang.cn/v3/push"
// VALIDATE_URL = "https://api.jpush.cn/v3/push/validate"
// GROUP_PUSH_URL = "https://api.jpush.cn/v3/grouppush"
)
// Client for JPush
type Client struct {
Auth string
Stats stat.Stat
Timeout time.Duration
}
// New .
func New(appKey string, secretKey string, timeout time.Duration) *Client {
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(appKey+":"+secretKey))
return &Client{
Auth: auth,
Stats: prom.HTTPClient,
Timeout: timeout,
}
}
// Push .
func (clt *Client) Push(b []byte) (resp []byte, err error) {
if clt.Stats != nil {
now := time.Now()
defer func() {
clt.Stats.Timing(pushURL, int64(time.Since(now)/time.Millisecond))
// log.Info("jpush stats timing: %v", int64(time.Since(now)/time.Millisecond))
if err != nil {
clt.Stats.Incr(pushURL, "failed")
}
}()
}
req, err := http.NewRequest("POST", pushURL, bytes.NewBuffer(b))
req.Header.Add("Charset", "UTF-8")
req.Header.Add("Authorization", clt.Auth)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: clt.Timeout}
httpResp, err := client.Do(req)
if err != nil {
return
}
defer httpResp.Body.Close()
return ioutil.ReadAll(httpResp.Body)
}
// GetTimeout .
func (clt *Client) GetTimeout() time.Duration {
return clt.Timeout
}

View File

@@ -0,0 +1,9 @@
package jpush
// Message .
type Message struct {
Title string `json:"title"`
ContentType string `json:"content_type"`
MsgContent string `json:"msg_content"`
Extras interface{} `json:"extras"`
}

View File

@@ -0,0 +1,35 @@
package jpush
// Notification .
type Notification struct {
Android *AndroidNotification `json:"android,omitempty"`
IOS *IOSNotification `json:"ios,omitempty"`
}
// AndroidNotification .
type AndroidNotification struct {
Alert string `json:"alert"`
Title string `json:"title,omitempty"`
AlertType int `json:"alert_type,omitempty"`
BuilderID int `json:"builder_id,omitempty"`
Style int `json:"style,omitempty"`
BigPicPath string `json:"big_pic_path,omitempty"`
Extras interface{} `json:"extras,omitempty"`
}
// IOSAlert .
type IOSAlert struct {
Title string `json:"title"`
Body string `json:"body"`
}
// IOSNotification .
type IOSNotification struct {
Alert interface{} `json:"alert"`
Sound string `json:"sound,omitempty"`
Badge int32 `json:"badge,omitempty"`
ContentAvailable bool `json:"content-available,omitempty"`
MutableContent bool `json:"mutable-content,omitempty"`
Category string `json:"category,omitempty"`
Extras interface{} `json:"extras,omitempty"`
}

View File

@@ -0,0 +1,10 @@
package jpush
// Option .
type Option struct {
SendNo int `json:"sendno,omitempty"`
TimeLive int `json:"time_to_live,omitempty"`
ApnsProduction bool `json:"apns_production"`
OverrideMsgID int64 `json:"override_msg_id,omitempty"`
BigPushDuration int `json:"big_push_duration,omitempty"`
}

View File

@@ -0,0 +1,11 @@
package jpush
// Payload .
type Payload struct {
CID string `json:"cid,omitempty"`
Platform interface{} `json:"platform"`
Audience interface{} `json:"audience"`
Notification interface{} `json:"notification,omitempty"`
Message interface{} `json:"message,omitempty"`
Options *Option `json:"options,omitempty"`
}