support command line.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-03-27 10:58:57 +08:00
parent 81826ac10c
commit bf703d3e20
7 changed files with 71 additions and 42 deletions

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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())
}

View File

@ -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())

61
main.go Normal file
View File

@ -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()
}