conf.go 3.8 KB

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