conf.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package common
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "runtime"
  12. "time"
  13. "github.com/go-yaml/yaml"
  14. "github.com/sirupsen/logrus"
  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. fmt.Println("not found dir : ", this.CommandDir)
  77. return false
  78. }
  79. if this.LogPath, err = filepath.Abs(this.LogPath); nil != err {
  80. fmt.Println("log dir err : ", err)
  81. return false
  82. }
  83. if _, err = os.Stat(this.LogPath); os.IsNotExist(err) {
  84. if err = os.MkdirAll(path.Dir(this.LogPath), 0755); nil != err {
  85. fmt.Println("mak logdir err : ", err)
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. var (
  92. Log *logrus.Logger
  93. g_client http.Client
  94. )
  95. func (this *config) initTimeout() {
  96. g_client.Timeout = time.Duration(this.Timeout) * time.Millisecond
  97. }
  98. func (this *config) initLog() bool {
  99. Log = logrus.New()
  100. Log.SetReportCaller(true)
  101. Log.SetFormatter(&logrus.TextFormatter{
  102. CallerPrettyfier: func(f *runtime.Frame) (string, string) {
  103. filename := path.Base(f.File)
  104. return "", fmt.Sprintf("%s:%d", filename, f.Line)
  105. },
  106. DisableColors: true,
  107. FullTimestamp: true,
  108. })
  109. logFile, err := os.OpenFile(this.LogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  110. if err == nil {
  111. Log.SetOutput(logFile)
  112. return true
  113. } else {
  114. Log.Infof("Failed to log to file, using default stderr.")
  115. return false
  116. }
  117. return false
  118. }
  119. func (this *config) Init() bool {
  120. if !this.initConfig() {
  121. return false
  122. }
  123. if !this.initLog() {
  124. return false
  125. }
  126. this.initTimeout()
  127. return true
  128. }
  129. func fetchContents(request *http.Request) (data []byte, err error) {
  130. respon, err := g_client.Do(request)
  131. if nil != err {
  132. return nil, err
  133. }
  134. defer respon.Body.Close()
  135. data, err = ioutil.ReadAll(respon.Body)
  136. if nil != err {
  137. return nil, err
  138. }
  139. /*
  140. if respon.StatusCode < 200 || respon.StatusCode > 299 {
  141. return data, fmt.Errorf("http return code: %d and error message %s.", respon.StatusCode, string(data))
  142. }
  143. */
  144. return data, err
  145. }
  146. func Get(inUrl string) (data []byte, err error) {
  147. request, err := http.NewRequest(http.MethodGet, inUrl, nil)
  148. if nil != err {
  149. return nil, err
  150. }
  151. return fetchContents(request)
  152. }
  153. func Post(inHead, inBody string) (data []byte, err error) {
  154. request, err := http.NewRequest(http.MethodPost, inHead, bytes.NewBuffer([]byte(inBody)))
  155. if nil != err {
  156. return nil, err
  157. }
  158. request.Header.Set("Content-Type", "application/json")
  159. return fetchContents(request)
  160. }
  161. func Delete(inUrl string) (data []byte, err error) {
  162. request, err := http.NewRequest(http.MethodDelete, inUrl, nil)
  163. if nil != err {
  164. return nil, err
  165. }
  166. return fetchContents(request)
  167. }
  168. func LoadFileUnmarshal(path string, ret interface{}) error {
  169. sliByte, err := ioutil.ReadFile(path)
  170. if nil != err {
  171. return err
  172. }
  173. err = json.Unmarshal(sliByte, ret)
  174. if nil != err {
  175. return err
  176. }
  177. return nil
  178. }
  179. func SaveFileMarshal(path string, content interface{}) error {
  180. data, err := json.Marshal(content)
  181. if nil != err {
  182. return err
  183. }
  184. return ioutil.WriteFile(path, data, 0666)
  185. }