Merge pull request #38 from appleboy/feature/37

fix #37 support number of notifications
This commit is contained in:
Bo-Yi Wu 2016-04-10 00:12:16 -05:00
commit f75a6112c7
5 changed files with 48 additions and 6 deletions

View File

@ -15,7 +15,7 @@ See the [YAML config eample](config/config.yaml):
```yaml ```yaml
core: core:
port: "8088" port: "8088"
notification_max: 100 max_notification: 100
mode: "release" mode: "release"
ssl: false ssl: false
cert_path: "cert.pem" cert_path: "cert.pem"

View File

@ -1,6 +1,6 @@
core: core:
port: "8088" port: "8088"
notification_max: 100 max_notification: 100
mode: "release" mode: "release"
ssl: false ssl: false
cert_path: "cert.pem" cert_path: "cert.pem"

View File

@ -16,7 +16,7 @@ type ConfYaml struct {
type SectionCore struct { type SectionCore struct {
Port string `yaml:"port"` Port string `yaml:"port"`
NotificationMax int `yaml:"notification_max"` MaxNotification int `yaml:"max_notification"`
Mode string `yaml:"mode"` Mode string `yaml:"mode"`
SSL bool `yaml:"ssl"` SSL bool `yaml:"ssl"`
CertPath string `yaml:"cert_path"` CertPath string `yaml:"cert_path"`
@ -53,11 +53,11 @@ func BuildDefaultPushConf() ConfYaml {
// Core // Core
conf.Core.Port = "8088" conf.Core.Port = "8088"
conf.Core.NotificationMax = 100
conf.Core.Mode = "release" conf.Core.Mode = "release"
conf.Core.SSL = false conf.Core.SSL = false
conf.Core.CertPath = "cert.pem" conf.Core.CertPath = "cert.pem"
conf.Core.KeyPath = "key.pem" conf.Core.KeyPath = "key.pem"
conf.Core.MaxNotification = 100
// Api // Api
conf.Api.PushUri = "/api/push" conf.Api.PushUri = "/api/push"

View File

@ -4,6 +4,7 @@ import (
api "github.com/appleboy/gin-status-api" api "github.com/appleboy/gin-status-api"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"fmt"
) )
func AbortWithError(c *gin.Context, code int, message string) { func AbortWithError(c *gin.Context, code int, message string) {
@ -22,14 +23,26 @@ func rootHandler(c *gin.Context) {
func pushHandler(c *gin.Context) { func pushHandler(c *gin.Context) {
var form RequestPush var form RequestPush
var msg string
if err := c.BindJSON(&form); err != nil { if err := c.BindJSON(&form); err != nil {
AbortWithError(c, http.StatusBadRequest, "Missing nitifications field.") msg = "Missing nitifications field."
LogAccess.Debug(msg)
AbortWithError(c, http.StatusBadRequest, msg)
return return
} }
if len(form.Notifications) == 0 { if len(form.Notifications) == 0 {
AbortWithError(c, http.StatusBadRequest, "Notification field is empty.") msg = "Notification field is empty."
LogAccess.Debug(msg)
AbortWithError(c, http.StatusBadRequest, msg)
return
}
if len(form.Notifications) > PushConf.Core.MaxNotification {
msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), PushConf.Core.MaxNotification)
LogAccess.Debug(msg)
AbortWithError(c, http.StatusBadRequest, msg)
return return
} }

View File

@ -121,6 +121,35 @@ func TestEmptyNotifications(t *testing.T) {
}) })
} }
func TestOutOfRangeMaxNotifications(t *testing.T) {
initTest()
PushConf.Core.MaxNotification = 1
r := gofight.New()
// notifications is empty.
r.POST("/api/push").
SetJSON(gofight.D{
"notifications": []gofight.D{
gofight.D{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
},
gofight.D{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
},
},
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestSuccessPushHandler(t *testing.T) { func TestSuccessPushHandler(t *testing.T) {
initTest() initTest()