fix #37 support number of notifications
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
91099ff74e
commit
32507de908
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue