-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.go
91 lines (80 loc) · 1.84 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"log"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/payload"
)
var (
redisPool *redis.Pool
)
func initRedisPool() {
redisPool = &redis.Pool{
MaxIdle: *maxIdle,
MaxActive: *maxActive,
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL(*redisURI)
if err != nil {
log.Panicf("connect to redis(%s) got error: %s", *redisURI, err)
}
return c, nil
},
}
}
func genUUIDKey(uuid string) string {
return fmt.Sprintf("obito:uuid:%s", uuid)
}
func genTagKey(tagName string) string {
return fmt.Sprintf("obito:tag:%s", tagName)
}
func genTagListKey(uuid string) string {
return fmt.Sprintf("obito:taglist:%s", uuid)
}
func genBadgeKey(uuid string) string {
return fmt.Sprintf("obito:badge:%s", uuid)
}
// Success 返回成功的json但是要自己return
func Success(c *gin.Context, code int, result map[string]interface{}, message string) {
if result == nil {
result = gin.H{}
}
c.JSON(
code,
gin.H{
"result": result,
"message": message,
"code": code,
},
)
}
// Fail 返回失败的json, 但是要自己return
func Fail(c *gin.Context, code int, result map[string]interface{}, message string) {
if result == nil {
result = gin.H{}
}
c.JSON(
code,
gin.H{
"result": result,
"message": message,
"code": code,
},
)
}
// false means failed, true in another way
func push(conn redis.Conn, deviceToken string, uuid string, content string) {
badge, _ := redis.Int(conn.Do("INCR", genBadgeKey(uuid)))
notification := &apns2.Notification{}
notification.DeviceToken = deviceToken
notification.Payload = payload.NewPayload().Alert(
content,
).Badge(
badge,
).Sound("default")
log.Printf("gonna push to %s", deviceToken)
go func() {
daemon <- &Notification{apns: notification}
}()
}