Browse Source

readConfig have error check

master
Patrick Peng Sun 8 years ago
parent
commit
2e1e2c6930
1 changed files with 22 additions and 1 deletions
  1. +22
    -1
      config.go

+ 22
- 1
config.go View File

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

Loading…
Cancel
Save