2016-03-27 02:58:57 +00:00
|
|
|
package gopush
|
2016-03-24 09:01:43 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-10 08:00:17 +00:00
|
|
|
"fmt"
|
2016-03-24 09:01:43 +00:00
|
|
|
api "github.com/appleboy/gin-status-api"
|
2016-04-12 07:04:25 +00:00
|
|
|
"github.com/fvbock/endless"
|
2016-03-24 16:53:45 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-03-26 16:03:17 +00:00
|
|
|
"net/http"
|
2016-03-24 09:01:43 +00:00
|
|
|
)
|
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
func AbortWithError(c *gin.Context, code int, message string) {
|
|
|
|
c.JSON(code, gin.H{
|
|
|
|
"code": code,
|
|
|
|
"message": message,
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2016-03-27 16:04:01 +00:00
|
|
|
"text": "Welcome to notification server.",
|
2016-03-24 16:46:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushHandler(c *gin.Context) {
|
2016-04-10 04:02:44 +00:00
|
|
|
var form RequestPush
|
2016-04-10 05:04:32 +00:00
|
|
|
var msg string
|
2016-03-24 16:46:37 +00:00
|
|
|
|
|
|
|
if err := c.BindJSON(&form); err != nil {
|
2016-04-10 07:15:54 +00:00
|
|
|
msg = "Missing notifications field."
|
2016-04-10 05:04:32 +00:00
|
|
|
LogAccess.Debug(msg)
|
|
|
|
AbortWithError(c, http.StatusBadRequest, msg)
|
2016-04-10 04:02:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.Notifications) == 0 {
|
2016-04-13 03:43:07 +00:00
|
|
|
msg = "Notifications field is empty."
|
2016-04-10 05:04:32 +00:00
|
|
|
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)
|
2016-03-24 16:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// process notification.
|
2016-04-10 04:02:44 +00:00
|
|
|
go SendNotification(form)
|
2016-03-24 16:46:37 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2016-04-13 03:43:07 +00:00
|
|
|
"success": "ok",
|
2016-03-24 09:01:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMainEngine() *gin.Engine {
|
2016-04-01 01:53:10 +00:00
|
|
|
// set server mode
|
|
|
|
gin.SetMode(PushConf.Core.Mode)
|
2016-03-27 13:12:11 +00:00
|
|
|
|
2016-03-24 09:01:43 +00:00
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
// Global middleware
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
r.Use(gin.Recovery())
|
2016-03-25 01:23:46 +00:00
|
|
|
r.Use(VersionMiddleware())
|
2016-04-07 06:16:59 +00:00
|
|
|
r.Use(LogMiddleware())
|
2016-03-24 09:01:43 +00:00
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
r.GET(PushConf.Api.StatGoUri, api.StatusHandler)
|
|
|
|
r.POST(PushConf.Api.PushUri, pushHandler)
|
2016-03-24 09:01:43 +00:00
|
|
|
r.GET("/", rootHandler)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-04-01 16:37:58 +00:00
|
|
|
func RunHTTPServer() error {
|
|
|
|
var err error
|
2016-03-28 06:40:48 +00:00
|
|
|
if PushConf.Core.SSL && PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {
|
2016-04-12 07:04:25 +00:00
|
|
|
err = endless.ListenAndServeTLS(":"+PushConf.Core.Port, PushConf.Core.CertPath, PushConf.Core.KeyPath, GetMainEngine())
|
2016-03-28 06:40:48 +00:00
|
|
|
} else {
|
2016-04-12 07:04:25 +00:00
|
|
|
err = endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
|
2016-03-28 06:40:48 +00:00
|
|
|
}
|
2016-04-01 16:37:58 +00:00
|
|
|
|
|
|
|
return err
|
2016-03-24 09:01:43 +00:00
|
|
|
}
|