From 2e1e2c6930e313f2c299216d71460baf2ad64d0f Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Tue, 25 Apr 2017 22:40:53 +1000 Subject: [PATCH] readConfig have error check --- config.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 5078701..c5114ba 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "encoding/base64" "encoding/json" + "errors" "io/ioutil" "log" ) @@ -20,6 +21,9 @@ type WechatAPIConfig struct { //AppSecret is how we identify ourselves. AppSecret string `json:"AppSecret"` + + //AuthTokenSaveTo + AuthTokenSaveTo string `json:"AuthTokenSaveTo"` } //APIConfig contains secrets that cannot store in source file @@ -30,9 +34,26 @@ func readConfig() error { log.Printf("read config from %s\r\n", "server_config.json") body, err := ioutil.ReadFile("server_config.json") if err != nil { + log.Fatal("Cannot read config from server_config.json") return err } - return json.Unmarshal(body, &APIConfig) + err = json.Unmarshal(body, &APIConfig) + if APIConfig.AppSecret == "" { + return errors.New("AppSecret not available") + } + if APIConfig.Appid == "" { + return errors.New("AppId not available") + } + if APIConfig.AuthTokenSaveTo == "" { + return errors.New("where to save accesstoken is unknown") + } + if APIConfig.EncodingAESKey == "" { + return errors.New("unknown AesEncryptionKey") + } + if APIConfig.Token == "" { + return errors.New("hidden shared secret token is empty") + } + return err } //convert AesEncryptKey into Binary 32bytes