conf.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package common
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-yaml/yaml"
  7. "github.com/sirupsen/logrus"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "runtime"
  14. "time"
  15. )
  16. type (
  17. config struct {
  18. Port int `yaml:"port"`
  19. Timeout int `yaml:"timeout"`
  20. IntervalTime int `yaml:"intervalTime"`
  21. Ip string `yaml:"ip"`
  22. LogPath string `yaml:"logPath"`
  23. CommandDir string `yaml:"commandDir"`
  24. }
  25. )
  26. var g_conf config
  27. func GetConf() *config {
  28. return &g_conf
  29. }
  30. func (this *config) GetIntervalTime() int {
  31. return this.IntervalTime
  32. }
  33. func (this *config) GetIp() string {
  34. return this.Ip
  35. }
  36. func (this *config) GetPort() int {
  37. return this.Port
  38. }
  39. func (this *config) GetLogPath() string {
  40. return this.LogPath
  41. }
  42. func (this *config) GetCommandDir() string {
  43. return this.CommandDir
  44. }
  45. func processPath(path string) (string, error) {
  46. if abs, err := filepath.Abs(path); err != nil {
  47. return "", nil
  48. } else {
  49. if _, err := os.Stat(abs); os.IsNotExist(err) {
  50. return "", err
  51. }
  52. return abs, nil
  53. }
  54. }
  55. func (this *config) initConfig() bool {
  56. confPath, err := processPath(os.Args[1])
  57. if nil != err {
  58. fmt.Println("conf path err : ", err)
  59. return false
  60. }
  61. sliByte, err := ioutil.ReadFile(confPath)
  62. if nil != err {
  63. fmt.Println("load conf err : ", err)
  64. return false
  65. }
  66. err = yaml.Unmarshal(sliByte, this)
  67. if nil != err {
  68. fmt.Println("unmashal conf err : ", err)
  69. return false
  70. }
  71. if this.CommandDir, err = filepath.Abs(this.CommandDir); err != nil {
  72. fmt.Println("command dir err : ", err)
  73. return false
  74. }
  75. if _, err = os.Stat(this.CommandDir); os.IsNotExist(err) {
  76. return false
  77. }
  78. if this.LogPath, err = filepath.Abs(this.LogPath); nil != err {
  79. fmt.Println("log dir err : ", err)
  80. return false
  81. }
  82. if _, err = os.Stat(this.LogPath); os.IsNotExist(err) {
  83. if err = os.MkdirAll(path.Dir(this.LogPath), 0755); nil != err {
  84. fmt.Println("mak logdir err : ", err)
  85. return false
  86. }
  87. }
  88. return true
  89. }
  90. var (
  91. Log *logrus.Logger
  92. g_client http.Client
  93. )
  94. func (this *config) initTimeout() {
  95. g_client.Timeout = time.Duration(this.Timeout) * time.Millisecond
  96. }
  97. func (this *config) initLog() bool {
  98. Log = logrus.New()
  99. Log.SetReportCaller(true)
  100. Log.SetFormatter(&logrus.TextFormatter{
  101. CallerPrettyfier: func(f *runtime.Frame) (string, string) {
  102. filename := path.Base(f.File)
  103. return "", fmt.Sprintf("%s:%d", filename, f.Line)
  104. },
  105. DisableColors: true,
  106. FullTimestamp: true,
  107. })
  108. logFile, err := os.OpenFile(this.LogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  109. if err == nil {
  110. Log.SetOutput(logFile)
  111. return true
  112. } else {
  113. Log.Infof("Failed to log to file, using default stderr.")
  114. return false
  115. }
  116. return false
  117. }
  118. func (this *config) Init() bool {
  119. if !this.initConfig() {
  120. return false
  121. }
  122. if !this.initLog() {
  123. return false
  124. }
  125. this.initTimeout()
  126. return true
  127. }
  128. func fetchContents(request *http.Request) (data []byte, err error) {
  129. respon, err := g_client.Do(request)
  130. if nil != err {
  131. return nil, err
  132. }
  133. defer respon.Body.Close()
  134. data, err = ioutil.ReadAll(respon.Body)
  135. if nil != err {
  136. return nil, err
  137. }
  138. /*
  139. if respon.StatusCode < 200 || respon.StatusCode > 299 {
  140. return data, fmt.Errorf("http return code: %d and error message %s.", respon.StatusCode, string(data))
  141. }
  142. */
  143. return data, err
  144. }
  145. func Get(inUrl string) (data []byte, err error) {
  146. request, err := http.NewRequest(http.MethodGet, inUrl, nil)
  147. if nil != err {
  148. return nil, err
  149. }
  150. return fetchContents(request)
  151. }
  152. func Post(inHead, inBody string) (data []byte, err error) {
  153. request, err := http.NewRequest(http.MethodPost, inHead, bytes.NewBuffer([]byte(inBody)))
  154. if nil != err {
  155. return nil, err
  156. }
  157. request.Header.Set("Content-Type", "application/json")
  158. return fetchContents(request)
  159. }
  160. func Delete(inUrl string) (data []byte, err error) {
  161. request, err := http.NewRequest(http.MethodDelete, inUrl, nil)
  162. if nil != err {
  163. return nil, err
  164. }
  165. return fetchContents(request)
  166. }
  167. func LoadFileUnmarshal(path string, ret interface{}) error {
  168. sliByte, err := ioutil.ReadFile(path)
  169. if nil != err {
  170. return err
  171. }
  172. err = json.Unmarshal(sliByte, ret)
  173. if nil != err {
  174. return err
  175. }
  176. return nil
  177. }
  178. func SaveFileMarshal(path string, content interface{}) error {
  179. data, err := json.Marshal(content)
  180. if nil != err {
  181. return err
  182. }
  183. return ioutil.WriteFile(path, data, 0666)
  184. }