commit
21a31fd161
|
@ -25,3 +25,4 @@ _testmain.go
|
|||
|
||||
gin-bin
|
||||
key.pem
|
||||
config.yaml
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type ConfYaml struct {
|
||||
Core SectionCore `yaml:"core"`
|
||||
Api SectionApi `yaml:"api"`
|
||||
Android SectionAndroid `yaml:"android"`
|
||||
Ios SectionIos `yaml:"ios"`
|
||||
}
|
||||
|
||||
type SectionCore struct {
|
||||
Port string `yaml:"port"`
|
||||
NotificationMax int `yaml:"notification_max"`
|
||||
}
|
||||
|
||||
type SectionApi struct {
|
||||
PushUri string `yaml:"push_uri"`
|
||||
StatGoUri string `yaml:"stat_go_uri"`
|
||||
}
|
||||
|
||||
type SectionAndroid struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
ApiKey string `yaml:"apikey"`
|
||||
}
|
||||
|
||||
type SectionIos struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
PemCertPath string `yaml:"pem_cert_path"`
|
||||
PemKeyPath string `yaml:"pem_key_path"`
|
||||
Production bool `yaml:"production"`
|
||||
}
|
||||
|
||||
func BuildDefaultPushConf() ConfYaml {
|
||||
var conf ConfYaml
|
||||
|
||||
// Core
|
||||
conf.Core.Port = "8088"
|
||||
conf.Core.NotificationMax = 100
|
||||
|
||||
// Api
|
||||
conf.Api.PushUri = "/api/push"
|
||||
conf.Api.StatGoUri = "/api/status"
|
||||
|
||||
// Android
|
||||
conf.Android.ApiKey = ""
|
||||
conf.Android.Enabled = true
|
||||
|
||||
// iOS
|
||||
conf.Ios.Enabled = true
|
||||
conf.Ios.PemCertPath = ""
|
||||
conf.Ios.PemKeyPath = ""
|
||||
conf.Ios.Production = false
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadConfYaml(confPath string) (ConfYaml, error) {
|
||||
var config ConfYaml
|
||||
|
||||
configFile, err := ioutil.ReadFile(confPath)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to read config file '%s'", confPath)
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal([]byte(configFile), &config)
|
||||
if err != nil {
|
||||
log.Printf("Unable to read config file '%v'", err)
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
apns "github.com/sideshow/apns2"
|
||||
)
|
||||
|
||||
var (
|
||||
PushConf ConfYaml
|
||||
CertificatePemIos tls.Certificate
|
||||
ApnsClient *apns.Client
|
||||
)
|
39
main.go
39
main.go
|
@ -6,6 +6,8 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
apns "github.com/sideshow/apns2"
|
||||
"github.com/sideshow/apns2/certificate"
|
||||
)
|
||||
|
||||
func AbortWithError(c *gin.Context, code int, message string) {
|
||||
|
@ -47,13 +49,44 @@ func GetMainEngine() *gin.Engine {
|
|||
r.Use(gin.Recovery())
|
||||
r.Use(VersionMiddleware())
|
||||
|
||||
r.GET("/api/status", api.StatusHandler)
|
||||
r.POST("/api/push", pushHandler)
|
||||
r.GET(PushConf.Api.StatGoUri, api.StatusHandler)
|
||||
r.POST(PushConf.Api.PushUri, pushHandler)
|
||||
r.GET("/", rootHandler)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
endless.ListenAndServe(":8088", GetMainEngine())
|
||||
var err error
|
||||
|
||||
// set default parameters.
|
||||
PushConf = BuildDefaultPushConf()
|
||||
|
||||
// load user define config.
|
||||
PushConf, err = LoadConfYaml("config.yaml")
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to load config file: '%v'", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if PushConf.Ios.Enabled {
|
||||
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
|
||||
|
||||
if err != nil {
|
||||
log.Println("Cert Error:", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if PushConf.Ios.Production {
|
||||
ApnsClient = apns.NewClient(CertificatePemIos).Production()
|
||||
} else {
|
||||
ApnsClient = apns.NewClient(CertificatePemIos).Development()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"github.com/google/go-gcm"
|
||||
apns "github.com/sideshow/apns2"
|
||||
"github.com/sideshow/apns2/certificate"
|
||||
"github.com/sideshow/apns2/payload"
|
||||
"log"
|
||||
)
|
||||
|
@ -64,19 +63,9 @@ func pushNotification(notification RequestPushNotification) bool {
|
|||
success bool
|
||||
)
|
||||
|
||||
cert, err := certificate.FromPemFile("./key.pem", "")
|
||||
if err != nil {
|
||||
log.Println("Cert Error:", err)
|
||||
}
|
||||
|
||||
apnsClient := apns.NewClient(cert).Development()
|
||||
|
||||
switch notification.Platform {
|
||||
case PlatFormIos:
|
||||
success = pushNotificationIos(notification, apnsClient)
|
||||
if !success {
|
||||
apnsClient = nil
|
||||
}
|
||||
success = pushNotificationIos(notification)
|
||||
case PlatFormAndroid:
|
||||
success = pushNotificationAndroid(notification)
|
||||
}
|
||||
|
@ -84,7 +73,7 @@ func pushNotification(notification RequestPushNotification) bool {
|
|||
return success
|
||||
}
|
||||
|
||||
func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool {
|
||||
func pushNotificationIos(req RequestPushNotification) bool {
|
||||
|
||||
for _, token := range req.Tokens {
|
||||
notification := &apns.Notification{}
|
||||
|
@ -169,20 +158,20 @@ func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool
|
|||
notification.Payload = payload
|
||||
|
||||
// send ios notification
|
||||
res, err := client.Push(notification)
|
||||
res, err := ApnsClient.Push(notification)
|
||||
|
||||
if err != nil {
|
||||
log.Println("There was an error", err)
|
||||
log.Println("There was an error: ", err)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if res.Sent() {
|
||||
log.Println("APNs ID:", res.ApnsID)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
client = nil
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -237,7 +226,7 @@ func pushNotificationAndroid(req RequestPushNotification) bool {
|
|||
notification.Notification.Body = req.Message
|
||||
}
|
||||
|
||||
res, err := gcm.SendHttp("api key", notification)
|
||||
res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
|
Loading…
Reference in New Issue