diff --git a/gorush/config.go b/gorush/config.go index 1a85c62..d011673 100644 --- a/gorush/config.go +++ b/gorush/config.go @@ -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 diff --git a/gorush/const.go b/gorush/const.go index 237f852..55e4ecd 100644 --- a/gorush/const.go +++ b/gorush/const.go @@ -1,4 +1,4 @@ -package main +package gopush const ( Version = "0.0.1" diff --git a/gorush/global.go b/gorush/global.go index 9be2bbd..35974a6 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -1,4 +1,4 @@ -package main +package gopush import ( "crypto/tls" diff --git a/gorush/notification.go b/gorush/notification.go index 69b8dc1..2671ae0 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -1,4 +1,4 @@ -package main +package gopush import ( "github.com/google/go-gcm" diff --git a/gorush/server.go b/gorush/server.go index 1d6dc14..0231bd3 100644 --- a/gorush/server.go +++ b/gorush/server.go @@ -1,11 +1,9 @@ -package main +package gopush import ( api "github.com/appleboy/gin-status-api" "github.com/fvbock/endless" "github.com/gin-gonic/gin" - apns "github.com/sideshow/apns2" - "github.com/sideshow/apns2/certificate" "log" "net/http" ) @@ -56,36 +54,6 @@ func GetMainEngine() *gin.Engine { return r } -func main() { - var err error - - // set default parameters. - PushConf = BuildDefaultPushConf() - - // load user define config. - PushConf, err = LoadConfYaml("config.yaml") - - if err != nil { - log.Printf("Unable to load config file: '%v'", err) - - return - } - - if PushConf.Ios.Enabled { - CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "") - - if err != nil { - log.Println("Cert Error:", err) - - return - } - - if PushConf.Ios.Production { - ApnsClient = apns.NewClient(CertificatePemIos).Production() - } else { - ApnsClient = apns.NewClient(CertificatePemIos).Development() - } - } - +func RunHTTPServer() { endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine()) } diff --git a/gorush/version.go b/gorush/version.go index d45b710..ba6a240 100644 --- a/gorush/version.go +++ b/gorush/version.go @@ -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()) diff --git a/main.go b/main.go new file mode 100644 index 0000000..bc702aa --- /dev/null +++ b/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "flag" + "github.com/appleboy/gopush/gopush" + "github.com/sideshow/apns2/certificate" + apns "github.com/sideshow/apns2" + "log" +) + +func main() { + version := flag.Bool("v", false, "gopush version") + confPath := flag.String("c", "", "yaml configuration file path for gopush") + port := flag.String("p", "", "port number for gopush") + + flag.Parse() + + if *version { + gopush.PrintGoPushVersion() + return + } + + var err error + + // set default parameters. + gopush.PushConf = gopush.BuildDefaultPushConf() + + // load user define config. + if *confPath != "" { + gopush.PushConf, err = gopush.LoadConfYaml(*confPath) + + if err != nil { + log.Printf("Unable to load yaml config file: '%v'", err) + + return + } + } + + if gopush.PushConf.Ios.Enabled { + gopush.CertificatePemIos, err = certificate.FromPemFile(gopush.PushConf.Ios.PemKeyPath, "") + + if err != nil { + log.Println("Cert Error:", err) + + return + } + + if gopush.PushConf.Ios.Production { + gopush.ApnsClient = apns.NewClient(gopush.CertificatePemIos).Production() + } else { + gopush.ApnsClient = apns.NewClient(gopush.CertificatePemIos).Development() + } + } + + // overwrite server port + if *port != "" { + gopush.PushConf.Core.Port = *port + } + + gopush.RunHTTPServer() +}