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

26
dtmcli/types.go

@ -82,16 +82,24 @@ func SetPassthroughHeaders(headers []string) {
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
func Result2HttpCode(result interface{}) int {
func Result2HttpJSON(result interface{}) (code int, res interface{}) {
err, _ := result.(error)
if errors.Is(err, ErrFailure) {
return http.StatusConflict
} else if errors.Is(err, ErrOngoing) {
return http.StatusTooEarly
} else if err != nil {
return http.StatusInternalServerError
if err == nil {
code = http.StatusOK
res = result
} else {
res = map[string]string{
"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) {
began := time.Now()
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 {
logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), status, c.Request.Method, c.Request.RequestURI, string(b))
} else {
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