Add check android and ios conf.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-02 23:51:42 +08:00
parent 17b9dc6ae8
commit de78a189fd
4 changed files with 79 additions and 30 deletions

View File

@ -36,44 +36,25 @@ func main() {
} }
} }
if !gopush.PushConf.Ios.Enabled && !gopush.PushConf.Android.Enabled {
log.Printf("Note: Please enable iOS or Android config in yaml config")
return
}
if gopush.PushConf.Ios.Enabled {
if *certificateKeyPath != "" { if *certificateKeyPath != "" {
gopush.PushConf.Ios.PemKeyPath = *certificateKeyPath gopush.PushConf.Ios.PemKeyPath = *certificateKeyPath
} }
if gopush.PushConf.Ios.PemKeyPath == "" {
log.Println("iOS certificate path not define")
return
}
}
// check andorid api key exist
if gopush.PushConf.Android.Enabled {
if *apiKey != "" { if *apiKey != "" {
gopush.PushConf.Android.ApiKey = *apiKey gopush.PushConf.Android.ApiKey = *apiKey
} }
if gopush.PushConf.Android.ApiKey == "" {
log.Println("Android API Key not define")
return
}
}
// overwrite server port // overwrite server port
if *port != "" { if *port != "" {
gopush.PushConf.Core.Port = *port gopush.PushConf.Core.Port = *port
} }
if err = gopush.CheckPushConf(); err != nil {
log.Printf("Check Conf Error: '%v'", err)
return
}
gopush.InitAPNSClient() gopush.InitAPNSClient()
gopush.RunHTTPServer() gopush.RunHTTPServer()
} }

View File

@ -55,8 +55,8 @@ func BuildDefaultPushConf() ConfYaml {
conf.Api.StatGoUri = "/api/status" conf.Api.StatGoUri = "/api/status"
// Android // Android
conf.Android.ApiKey = ""
conf.Android.Enabled = false conf.Android.Enabled = false
conf.Android.ApiKey = ""
// iOS // iOS
conf.Ios.Enabled = false conf.Ios.Enabled = false

View File

@ -6,6 +6,7 @@ import (
"github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload" "github.com/sideshow/apns2/payload"
"log" "log"
"errors"
) )
type ExtendJSON struct { type ExtendJSON struct {
@ -71,6 +72,26 @@ type RequestPushNotification struct {
IDs []uint64 `json:"seq_id,omitempty"` IDs []uint64 `json:"seq_id,omitempty"`
} }
func CheckPushConf() error {
if !PushConf.Ios.Enabled && !PushConf.Android.Enabled {
return errors.New("Please enable iOS or Android config in yaml config")
}
if PushConf.Ios.Enabled {
if PushConf.Ios.PemKeyPath == "" {
return errors.New("Missing iOS certificate path")
}
}
if PushConf.Android.Enabled {
if PushConf.Android.ApiKey == "" {
return errors.New("Missing Android API Key")
}
}
return nil
}
func InitAPNSClient() error { func InitAPNSClient() error {
if PushConf.Ios.Enabled { if PushConf.Ios.Enabled {
var err error var err error

View File

@ -9,6 +9,53 @@ import (
"testing" "testing"
) )
func TestDisabledAndroidIosConf(t *testing.T) {
PushConf = BuildDefaultPushConf()
err := CheckPushConf()
assert.Error(t, err)
assert.Equal(t, "Please enable iOS or Android config in yaml config", err.Error())
}
func TestMissingIOSCertificate(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = ""
err := CheckPushConf()
assert.Error(t, err)
assert.Equal(t, "Missing iOS certificate path", err.Error())
}
func TestMissingAndroidAPIKey(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = ""
err := CheckPushConf()
assert.Error(t, err)
assert.Equal(t, "Missing Android API Key", err.Error())
}
func TestCorrectConf(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = "xxxxx"
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "xxxxx"
err := CheckPushConf()
assert.NoError(t, err)
}
func TestIOSNotificationStructure(t *testing.T) { func TestIOSNotificationStructure(t *testing.T) {
var dat map[string]interface{} var dat map[string]interface{}