2016-03-24 09:01:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
api "github.com/appleboy/gin-status-api"
|
2016-03-24 16:46:37 +00:00
|
|
|
"github.com/fvbock/endless"
|
2016-03-24 16:53:45 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-03-24 16:46:37 +00:00
|
|
|
"log"
|
2016-03-24 16:53:45 +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{
|
|
|
|
"text": "Welcome to golang push server.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushHandler(c *gin.Context) {
|
|
|
|
var form RequestPushNotification
|
|
|
|
|
|
|
|
if err := c.BindJSON(&form); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
AbortWithError(c, http.StatusBadRequest, "Missing some parameters like token or platform or message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// process notification.
|
|
|
|
pushNotification(form)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"text": "Welcome to golang push server.",
|
2016-03-24 09:01:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMainEngine() *gin.Engine {
|
|
|
|
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-03-24 09:01:43 +00:00
|
|
|
|
|
|
|
r.GET("/api/status", api.StatusHandler)
|
2016-03-24 16:46:37 +00:00
|
|
|
r.POST("/api/push", pushHandler)
|
2016-03-24 09:01:43 +00:00
|
|
|
r.GET("/", rootHandler)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-03-24 16:46:37 +00:00
|
|
|
endless.ListenAndServe(":8088", GetMainEngine())
|
2016-03-24 09:01:43 +00:00
|
|
|
}
|