Browse Source

Merge pull request #285 from dtm-labs/alpha

update dtmcli for rockscache
pull/286/head v1.13.5
yedf2 4 years ago
committed by GitHub
parent
commit
481448c3ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      dtmcli/logger/log.go
  2. 26
      dtmcli/types.go
  3. 6
      dtmutil/utils.go

2
dtmcli/logger/log.go

@ -6,6 +6,7 @@ import (
"log" "log"
"net/url" "net/url"
"os" "os"
"runtime/debug"
"strings" "strings"
"github.com/natefinch/lumberjack" "github.com/natefinch/lumberjack"
@ -124,6 +125,7 @@ func FatalfIf(cond bool, fmt string, args ...interface{}) {
if !cond { if !cond {
return return
} }
debug.PrintStack()
log.Fatalf(fmt, args...) log.Fatalf(fmt, args...)
} }

26
dtmcli/types.go

@ -82,16 +82,24 @@ func SetPassthroughHeaders(headers []string) {
dtmimp.PassthroughHeaders = headers dtmimp.PassthroughHeaders = headers
} }
// Result2HttpCode return the http code for the result // Result2HttpJSON return the http code and json result
// if result is error, the return proper code, else return StatusOK // if result is error, the return proper code, else return StatusOK
func Result2HttpCode(result interface{}) int { func Result2HttpJSON(result interface{}) (code int, res interface{}) {
err, _ := result.(error) err, _ := result.(error)
if errors.Is(err, ErrFailure) { if err == nil {
return http.StatusConflict code = http.StatusOK
} else if errors.Is(err, ErrOngoing) { res = result
return http.StatusTooEarly } else {
} else if err != nil { res = map[string]string{
return http.StatusInternalServerError "error": err.Error(),
}
if errors.Is(err, ErrFailure) {
code = http.StatusConflict
} else if errors.Is(err, ErrOngoing) {
code = http.StatusTooEarly
} else if err != nil {
code = http.StatusInternalServerError
}
} }
return http.StatusOK return
} }

6
dtmutil/utils.go

@ -52,15 +52,15 @@ func WrapHandler(fn func(*gin.Context) interface{}) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
began := time.Now() began := time.Now()
ret := fn(c) ret := fn(c)
status := dtmcli.Result2HttpCode(ret) status, res := dtmcli.Result2HttpJSON(ret)
b, _ := json.Marshal(ret) b, _ := json.Marshal(res)
if status == http.StatusOK || status == http.StatusTooEarly { if status == http.StatusOK || status == http.StatusTooEarly {
logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), status, c.Request.Method, c.Request.RequestURI, string(b)) logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), status, c.Request.Method, c.Request.RequestURI, string(b))
} else { } else {
logger.Errorf("%2dms %d %s %s %s", time.Since(began).Milliseconds(), status, c.Request.Method, c.Request.RequestURI, string(b)) logger.Errorf("%2dms %d %s %s %s", time.Since(began).Milliseconds(), status, c.Request.Method, c.Request.RequestURI, string(b))
} }
c.JSON(status, ret) c.JSON(status, res)
} }
} }

Loading…
Cancel
Save