Merge pull request #11 from appleboy/cli
fix #9 support command line flag.
This commit is contained in:
		
						commit
						8623a92d50
					
				|  | @ -1,4 +1,4 @@ | |||
| package main | ||||
| package gopush | ||||
| 
 | ||||
| import ( | ||||
| 	"gopkg.in/yaml.v2" | ||||
|  | @ -52,8 +52,8 @@ func BuildDefaultPushConf() ConfYaml { | |||
| 
 | ||||
| 	// iOS
 | ||||
| 	conf.Ios.Enabled = true | ||||
| 	conf.Ios.PemCertPath = "" | ||||
| 	conf.Ios.PemKeyPath = "" | ||||
| 	conf.Ios.PemCertPath = "cert.pem" | ||||
| 	conf.Ios.PemKeyPath = "key.pem" | ||||
| 	conf.Ios.Production = false | ||||
| 
 | ||||
| 	return conf | ||||
|  | @ -1,4 +1,4 @@ | |||
| package main | ||||
| package gopush | ||||
| 
 | ||||
| const ( | ||||
| 	Version = "0.0.1" | ||||
|  | @ -1,4 +1,4 @@ | |||
| package main | ||||
| package gopush | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
|  | @ -1,4 +1,4 @@ | |||
| package main | ||||
| package gopush | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/google/go-gcm" | ||||
|  | @ -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()) | ||||
| } | ||||
|  | @ -1,4 +1,4 @@ | |||
| package main | ||||
| package gopush | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | @ -7,7 +7,7 @@ import ( | |||
| ) | ||||
| 
 | ||||
| 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, | ||||
| 		runtime.Compiler, | ||||
| 		runtime.Version()) | ||||
							
								
								
									
										113
									
								
								main.go
								
								
								
								
							
							
						
						
									
										113
									
								
								main.go
								
								
								
								
							|  | @ -1,78 +1,56 @@ | |||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	api "github.com/appleboy/gin-status-api" | ||||
| 	"github.com/fvbock/endless" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	apns "github.com/sideshow/apns2" | ||||
| 	"flag" | ||||
| 	"github.com/appleboy/gopush/gopush" | ||||
| 	"github.com/sideshow/apns2/certificate" | ||||
| 	apns "github.com/sideshow/apns2" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| ) | ||||
| 
 | ||||
| func AbortWithError(c *gin.Context, code int, message string) { | ||||
| 	c.JSON(code, gin.H{ | ||||
| 		"code":    code, | ||||
| 		"message": message, | ||||
| 	}) | ||||
| 	c.Abort() | ||||
| } | ||||
| func main() { | ||||
| 	version := flag.Bool("v", false, "gopush version") | ||||
| 	confPath := flag.String("c", "", "yaml configuration file path for gopush") | ||||
| 	certificateKeyPath := flag.String("i", "", "iOS certificate key file path for gopush") | ||||
| 	apiKey := flag.String("k", "", "Android api key configuration for gopush") | ||||
| 	port := flag.String("p", "", "port number for gopush") | ||||
| 
 | ||||
| func rootHandler(c *gin.Context) { | ||||
| 	c.JSON(http.StatusOK, gin.H{ | ||||
| 		"text": "Welcome to golang push server.", | ||||
| 	}) | ||||
| } | ||||
| 	flag.Parse() | ||||
| 
 | ||||
| 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.") | ||||
| 	if *version { | ||||
| 		gopush.PrintGoPushVersion() | ||||
| 		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 | ||||
| 
 | ||||
| 	// set default parameters.
 | ||||
| 	PushConf = BuildDefaultPushConf() | ||||
| 	gopush.PushConf = gopush.BuildDefaultPushConf() | ||||
| 
 | ||||
| 	// load user define config.
 | ||||
| 	PushConf, err = LoadConfYaml("config.yaml") | ||||
| 	if *confPath != "" { | ||||
| 		gopush.PushConf, err = gopush.LoadConfYaml(*confPath) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		log.Printf("Unable to load config file: '%v'", err) | ||||
| 		if err != nil { | ||||
| 			log.Printf("Unable to load yaml config file: '%v'", err) | ||||
| 
 | ||||
| 		return | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if PushConf.Ios.Enabled { | ||||
| 		CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "") | ||||
| 	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 | ||||
| 		} | ||||
| 
 | ||||
| 		gopush.CertificatePemIos, err = certificate.FromPemFile(gopush.PushConf.Ios.PemKeyPath, "") | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			log.Println("Cert Error:", err) | ||||
|  | @ -80,12 +58,31 @@ func main() { | |||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		if PushConf.Ios.Production { | ||||
| 			ApnsClient = apns.NewClient(CertificatePemIos).Production() | ||||
| 		if gopush.PushConf.Ios.Production { | ||||
| 			gopush.ApnsClient = apns.NewClient(gopush.CertificatePemIos).Production() | ||||
| 		} 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() | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue