Merge pull request #11 from appleboy/cli

fix #9 support command line flag.
This commit is contained in:
Bo-Yi Wu 2016-03-27 12:59:20 +08:00
commit 8623a92d50
7 changed files with 122 additions and 66 deletions

View File

@ -1,4 +1,4 @@
package main package gopush
import ( import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -52,8 +52,8 @@ func BuildDefaultPushConf() ConfYaml {
// iOS // iOS
conf.Ios.Enabled = true conf.Ios.Enabled = true
conf.Ios.PemCertPath = "" conf.Ios.PemCertPath = "cert.pem"
conf.Ios.PemKeyPath = "" conf.Ios.PemKeyPath = "key.pem"
conf.Ios.Production = false conf.Ios.Production = false
return conf return conf

View File

@ -1,4 +1,4 @@
package main package gopush
const ( const (
Version = "0.0.1" Version = "0.0.1"

View File

@ -1,4 +1,4 @@
package main package gopush
import ( import (
"crypto/tls" "crypto/tls"

View File

@ -1,4 +1,4 @@
package main package gopush
import ( import (
"github.com/google/go-gcm" "github.com/google/go-gcm"

59
gorush/server.go Normal file
View File

@ -0,0 +1,59 @@
package gopush
import (
api "github.com/appleboy/gin-status-api"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
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, "Bad input request, please refer to README guide.")
return
}
// process notification.
pushNotification(form)
c.JSON(http.StatusOK, gin.H{
"text": "Welcome to golang push server.",
})
}
func GetMainEngine() *gin.Engine {
r := gin.New()
// Global middleware
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.Use(VersionMiddleware())
r.GET(PushConf.Api.StatGoUri, api.StatusHandler)
r.POST(PushConf.Api.PushUri, pushHandler)
r.GET("/", rootHandler)
return r
}
func RunHTTPServer() {
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
}

View File

@ -1,4 +1,4 @@
package main package gopush
import ( import (
"fmt" "fmt"
@ -7,7 +7,7 @@ import (
) )
func PrintGoPushVersion() { func PrintGoPushVersion() {
fmt.Printf(`GoPush %s Compiler: %s %s Copyright (C) 2016 Bo-Yi Wu, Inc.`, fmt.Printf(`GoPush %s, Compiler: %s %s, Copyright (C) 2016 Bo-Yi Wu, Inc.`,
Version, Version,
runtime.Compiler, runtime.Compiler,
runtime.Version()) runtime.Version())

109
main.go
View File

@ -1,78 +1,56 @@
package main package main
import ( import (
api "github.com/appleboy/gin-status-api" "flag"
"github.com/fvbock/endless" "github.com/appleboy/gopush/gopush"
"github.com/gin-gonic/gin"
apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/certificate"
apns "github.com/sideshow/apns2"
"log" "log"
"net/http"
) )
func AbortWithError(c *gin.Context, code int, message string) { func main() {
c.JSON(code, gin.H{ version := flag.Bool("v", false, "gopush version")
"code": code, confPath := flag.String("c", "", "yaml configuration file path for gopush")
"message": message, certificateKeyPath := flag.String("i", "", "iOS certificate key file path for gopush")
}) apiKey := flag.String("k", "", "Android api key configuration for gopush")
c.Abort() port := flag.String("p", "", "port number for gopush")
}
func rootHandler(c *gin.Context) { flag.Parse()
c.JSON(http.StatusOK, gin.H{
"text": "Welcome to golang push server.",
})
}
func pushHandler(c *gin.Context) { if *version {
var form RequestPushNotification gopush.PrintGoPushVersion()
if err := c.BindJSON(&form); err != nil {
log.Println(err)
AbortWithError(c, http.StatusBadRequest, "Bad input request, please refer to README guide.")
return return
} }
// process notification.
pushNotification(form)
c.JSON(http.StatusOK, gin.H{
"text": "Welcome to golang push server.",
})
}
func GetMainEngine() *gin.Engine {
r := gin.New()
// Global middleware
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.Use(VersionMiddleware())
r.GET(PushConf.Api.StatGoUri, api.StatusHandler)
r.POST(PushConf.Api.PushUri, pushHandler)
r.GET("/", rootHandler)
return r
}
func main() {
var err error var err error
// set default parameters. // set default parameters.
PushConf = BuildDefaultPushConf() gopush.PushConf = gopush.BuildDefaultPushConf()
// load user define config. // load user define config.
PushConf, err = LoadConfYaml("config.yaml") if *confPath != "" {
gopush.PushConf, err = gopush.LoadConfYaml(*confPath)
if err != nil { if err != nil {
log.Printf("Unable to load config file: '%v'", err) log.Printf("Unable to load yaml config file: '%v'", err)
return
}
}
if gopush.PushConf.Ios.Enabled {
if *certificateKeyPath != "" {
gopush.PushConf.Ios.PemKeyPath = *certificateKeyPath
}
if gopush.PushConf.Ios.PemKeyPath == "" {
log.Println("iOS certificate path not define")
return return
} }
if PushConf.Ios.Enabled { gopush.CertificatePemIos, err = certificate.FromPemFile(gopush.PushConf.Ios.PemKeyPath, "")
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
if err != nil { if err != nil {
log.Println("Cert Error:", err) log.Println("Cert Error:", err)
@ -80,12 +58,31 @@ func main() {
return return
} }
if PushConf.Ios.Production { if gopush.PushConf.Ios.Production {
ApnsClient = apns.NewClient(CertificatePemIos).Production() gopush.ApnsClient = apns.NewClient(gopush.CertificatePemIos).Production()
} else { } else {
ApnsClient = apns.NewClient(CertificatePemIos).Development() gopush.ApnsClient = apns.NewClient(gopush.CertificatePemIos).Development()
} }
} }
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine()) // check andorid api key exist
if gopush.PushConf.Android.Enabled {
if *apiKey != "" {
gopush.PushConf.Android.ApiKey = *apiKey
}
if gopush.PushConf.Android.ApiKey == "" {
log.Println("Android API Key not define")
return
}
}
// overwrite server port
if *port != "" {
gopush.PushConf.Core.Port = *port
}
gopush.RunHTTPServer()
} }