diff --git a/.gitignore b/.gitignore index cfb5aef..f460ba3 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ _testmain.go gin-bin key.pem +config.yaml diff --git a/config.go b/config.go new file mode 100644 index 0000000..79ab421 --- /dev/null +++ b/config.go @@ -0,0 +1,81 @@ +package main + +import ( + "log" + "gopkg.in/yaml.v2" + "io/ioutil" +) + +type ConfYaml struct { + Core SectionCore `yaml:"core"` + Api SectionApi `yaml:"api"` + Android SectionAndroid `yaml:"android"` + Ios SectionIos `yaml:"ios"` +} + +type SectionCore struct { + Port string `yaml:"port"` + NotificationMax int `yaml:"notification_max"` +} + +type SectionApi struct { + PushUri string `yaml:"push_uri"` + StatGoUri string `yaml:"stat_go_uri"` +} + +type SectionAndroid struct { + Enabled bool `yaml:"enabled"` + ApiKey string `yaml:"apikey"` +} + +type SectionIos struct { + Enabled bool `yaml:"enabled"` + PemCertPath string `yaml:"pem_cert_path"` + PemKeyPath string `yaml:"pem_key_path"` + Production bool `yaml:"production"` +} + +func BuildDefaultPushConf() ConfYaml { + var conf ConfYaml + + // Core + conf.Core.Port = "8088" + conf.Core.NotificationMax = 100 + + // Api + conf.Api.PushUri = "/api/push" + conf.Api.StatGoUri = "/api/status" + + // Android + conf.Android.ApiKey = "" + conf.Android.Enabled = true + + // iOS + conf.Ios.Enabled = true + conf.Ios.PemCertPath = "" + conf.Ios.PemKeyPath = "" + conf.Ios.Production = false + + return conf +} + +func LoadConfYaml(confPath string) (ConfYaml, error) { + var config ConfYaml + + configFile, err := ioutil.ReadFile(confPath) + + if err != nil { + log.Printf("Unable to read config file '%s'", confPath) + + return config, err + } + + err = yaml.Unmarshal([]byte(configFile), &config) + if err != nil { + log.Printf("Unable to read config file '%v'", err) + + return config, err + } + + return config, nil +} diff --git a/global.go b/global.go new file mode 100644 index 0000000..ebd82c4 --- /dev/null +++ b/global.go @@ -0,0 +1,5 @@ +package main + +var ( + PushConf ConfYaml +) diff --git a/main.go b/main.go index 2c6740a..c994716 100644 --- a/main.go +++ b/main.go @@ -47,13 +47,27 @@ func GetMainEngine() *gin.Engine { r.Use(gin.Recovery()) r.Use(VersionMiddleware()) - r.GET("/api/status", api.StatusHandler) - r.POST("/api/push", pushHandler) + r.GET(PushConf.Api.StatGoUri, api.StatusHandler) + r.POST(PushConf.Api.PushUri, pushHandler) r.GET("/", rootHandler) return r } func main() { - endless.ListenAndServe(":8088", GetMainEngine()) + + // set default parameters + PushConf = BuildDefaultPushConf() + + config, err := LoadConfYaml("config.yaml") + + if err != nil { + log.Printf("Unable to load config file: '%v'", err) + + return + } + + PushConf = config + + endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine()) } diff --git a/notification.go b/notification.go index 8a82e39..413b15f 100644 --- a/notification.go +++ b/notification.go @@ -237,7 +237,7 @@ func pushNotificationAndroid(req RequestPushNotification) bool { notification.Notification.Body = req.Message } - res, err := gcm.SendHttp("api key", notification) + res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification) if err != nil { log.Println(err)