From 77138c40de03c4a999236a4361d9d34989ae69da Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Fri, 19 May 2017 22:33:38 +1000 Subject: [PATCH] read CRM wechat API from different config file --- config.go | 18 +----------------- configCRM.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ crm_config.json | 5 +++++ main.go | 12 ++++++++++-- 4 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 configCRM.go create mode 100644 crm_config.json diff --git a/config.go b/config.go index ac7d9d6..4f7e65f 100644 --- a/config.go +++ b/config.go @@ -32,25 +32,9 @@ type WechatAPIConfig struct { //APIConfig contains secrets that cannot store in source file var APIConfig WechatAPIConfig -type EspoCRMAPIConfig struct { - - //where the EspoCRM is installed - BaseURL string `json:"BaseURL"` - - //access UserName - UserName string `json:"UserName"` - - //access Password - UserPass string `json:"password"` -} - -//CRMConfig contains secrets that cannot store in source file -// -var CRMConfig EspoCRMAPIConfig - func readConfig() error { - log.Printf("read config from %s\r\n", "server_config.json") + log.Printf("read Wechat API 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") diff --git a/configCRM.go b/configCRM.go new file mode 100644 index 0000000..fced4e0 --- /dev/null +++ b/configCRM.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/json" + "errors" + "io/ioutil" + "log" +) + +//EspoCRMAPIConfig CRM configuration for API accessing +type EspoCRMAPIConfig struct { + + //where the EspoCRM is installed + BaseURL string `json:"BaseURL"` + + //access UserName + UserName string `json:"UserName"` + + //access Password + UserPass string `json:"UserPass"` +} + +//CRMConfig contains secrets that cannot store in source file +// +var CRMConfig EspoCRMAPIConfig + +func readCRMConfig() error { + log.Printf("read CRM config from %s\r\n", "crm_config.json") + body, err := ioutil.ReadFile("crm_config.json") + if err != nil { + log.Fatal("Cannot read config from crm_config.json") + return err + } + err = json.Unmarshal(body, &CRMConfig) + if CRMConfig.BaseURL == "" { + return errors.New("CRM BaseURL not available not available") + } + + if CRMConfig.UserName == "" { + return errors.New("CRM UserName not available not available") + } + + if CRMConfig.UserPass == "" { + return errors.New("CRM UserPass not available not available") + } + + return err +} diff --git a/crm_config.json b/crm_config.json new file mode 100644 index 0000000..e9638c3 --- /dev/null +++ b/crm_config.json @@ -0,0 +1,5 @@ +{ + "BaseURL": "https://crm.hitxy.org.au/", + "UserName": "wechat", + "UserPass": "crmwechat.api" +} \ No newline at end of file diff --git a/main.go b/main.go index e2eec37..ff0b403 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,19 @@ import ( ) func main() { - if readConfig() != nil { - log.Fatal("unable to read config, program quit") + err := readConfig() //wechat API config + if err != nil { + log.Println(err) + log.Fatal("unable to read server_config.json, program quit") return } + err = readCRMConfig() + if err != nil { + log.Println(err) + log.Fatal("unable to read crm_config.json, program quit") + } + //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist")) fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))