util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coreos/etcd/web"
  6. "io"
  7. "log"
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "strconv"
  13. "time"
  14. )
  15. //--------------------------------------
  16. // etcd http Helper
  17. //--------------------------------------
  18. // Convert string duration to time format
  19. func durationToExpireTime(strDuration string) (time.Time, error) {
  20. if strDuration != "" {
  21. duration, err := strconv.Atoi(strDuration)
  22. if err != nil {
  23. return time.Unix(0, 0), err
  24. }
  25. return time.Now().Add(time.Second * (time.Duration)(duration)), nil
  26. } else {
  27. return time.Unix(0, 0), nil
  28. }
  29. }
  30. //--------------------------------------
  31. // Web Helper
  32. //--------------------------------------
  33. var storeMsg chan string
  34. // Help to send msg from store to webHub
  35. func webHelper() {
  36. storeMsg = make(chan string)
  37. etcdStore.SetMessager(storeMsg)
  38. for {
  39. // transfer the new msg to webHub
  40. web.Hub().Send(<-storeMsg)
  41. }
  42. }
  43. //--------------------------------------
  44. // HTTP Utilities
  45. //--------------------------------------
  46. func decodeJsonRequest(req *http.Request, data interface{}) error {
  47. decoder := json.NewDecoder(req.Body)
  48. if err := decoder.Decode(&data); err != nil && err != io.EOF {
  49. warnf("Malformed json request: %v", err)
  50. return fmt.Errorf("Malformed json request: %v", err)
  51. }
  52. return nil
  53. }
  54. func encodeJsonResponse(w http.ResponseWriter, status int, data interface{}) {
  55. w.Header().Set("Content-Type", "application/json")
  56. w.WriteHeader(status)
  57. if data != nil {
  58. encoder := json.NewEncoder(w)
  59. encoder.Encode(data)
  60. }
  61. }
  62. // sanitizeURL will cleanup a host string in the format hostname:port and
  63. // attach a schema.
  64. func sanitizeURL(host string, defaultScheme string) string {
  65. // Blank URLs are fine input, just return it
  66. if len(host) == 0 {
  67. return host
  68. }
  69. p, err := url.Parse(host)
  70. if err != nil {
  71. fatal(err)
  72. }
  73. // Make sure the host is in Host:Port format
  74. _, _, err = net.SplitHostPort(host)
  75. if err != nil {
  76. fatal(err)
  77. }
  78. p = &url.URL{Host: host, Scheme: defaultScheme}
  79. return p.String()
  80. }
  81. //--------------------------------------
  82. // Log
  83. //--------------------------------------
  84. var logger *log.Logger
  85. func init() {
  86. logger = log.New(os.Stdout, "[etcd] ", log.Lmicroseconds)
  87. }
  88. func infof(msg string, v ...interface{}) {
  89. logger.Printf("INFO "+msg+"\n", v...)
  90. }
  91. func debugf(msg string, v ...interface{}) {
  92. if verbose {
  93. logger.Printf("DEBUG "+msg+"\n", v...)
  94. }
  95. }
  96. func debug(v ...interface{}) {
  97. if verbose {
  98. logger.Println("DEBUG " + fmt.Sprint(v...))
  99. }
  100. }
  101. func warnf(msg string, v ...interface{}) {
  102. logger.Printf("WARN "+msg+"\n", v...)
  103. }
  104. func warn(v ...interface{}) {
  105. logger.Println("WARN " + fmt.Sprint(v...))
  106. }
  107. func fatalf(msg string, v ...interface{}) {
  108. logger.Printf("FATAL "+msg+"\n", v...)
  109. os.Exit(1)
  110. }
  111. func fatal(v ...interface{}) {
  112. logger.Println("FATAL " + fmt.Sprint(v...))
  113. os.Exit(1)
  114. }