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-26 15:42:22 +00:00
|
|
|
apns "github.com/sideshow/apns2"
|
2016-03-26 06:26:07 +00:00
|
|
|
"github.com/sideshow/apns2/certificate"
|
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)
|
2016-03-25 09:56:09 +00:00
|
|
|
AbortWithError(c, http.StatusBadRequest, "Bad input request, please refer to README guide.")
|
2016-03-24 16:46:37 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-03-26 06:26:07 +00:00
|
|
|
var err error
|
2016-03-26 05:59:56 +00:00
|
|
|
|
2016-03-26 06:26:07 +00:00
|
|
|
// set default parameters.
|
2016-03-26 05:59:56 +00:00
|
|
|
PushConf = BuildDefaultPushConf()
|
|
|
|
|
2016-03-26 06:26:07 +00:00
|
|
|
// load user define config.
|
|
|
|
PushConf, err = LoadConfYaml("config.yaml")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to load config file: '%v'", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-26 15:42:22 +00:00
|
|
|
if PushConf.Ios.Enabled {
|
|
|
|
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
|
2016-03-26 06:26:07 +00:00
|
|
|
|
2016-03-26 15:42:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Cert Error:", err)
|
2016-03-26 06:26:07 +00:00
|
|
|
|
2016-03-26 15:42:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if PushConf.Ios.Production {
|
|
|
|
ApnsClient = apns.NewClient(CertificatePemIos).Production()
|
|
|
|
} else {
|
|
|
|
ApnsClient = apns.NewClient(CertificatePemIos).Development()
|
|
|
|
}
|
2016-03-26 06:26:07 +00:00
|
|
|
}
|
2016-03-26 05:59:56 +00:00
|
|
|
|
2016-03-26 15:42:22 +00:00
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
|
2016-03-24 09:01:43 +00:00
|
|
|
}
|