|  | @@ -1,91 +0,0 @@
 | 
	
		
			
			| 1 |  | -package check
 | 
	
		
			
			| 2 |  | -
 | 
	
		
			
			| 3 |  | -import (
 | 
	
		
			
			| 4 |  | -	"encoding/json"
 | 
	
		
			
			| 5 |  | -	"fmt"
 | 
	
		
			
			| 6 |  | -	"github.com/gin-gonic/gin"
 | 
	
		
			
			| 7 |  | -	"net/http"
 | 
	
		
			
			| 8 |  | -)
 | 
	
		
			
			| 9 |  | -
 | 
	
		
			
			| 10 |  | -type (
 | 
	
		
			
			| 11 |  | -	errorWriter struct {
 | 
	
		
			
			| 12 |  | -		gin.ResponseWriter
 | 
	
		
			
			| 13 |  | -		data []byte
 | 
	
		
			
			| 14 |  | -	}
 | 
	
		
			
			| 15 |  | -)
 | 
	
		
			
			| 16 |  | -
 | 
	
		
			
			| 17 |  | -func (ew *errorWriter) WriteString(data string) (int, error) {
 | 
	
		
			
			| 18 |  | -	ew.data = append(ew.data, []byte(data)...)
 | 
	
		
			
			| 19 |  | -	return ew.ResponseWriter.WriteString(data)
 | 
	
		
			
			| 20 |  | -}
 | 
	
		
			
			| 21 |  | -
 | 
	
		
			
			| 22 |  | -func (ew *errorWriter) Write(data []byte) (int, error) {
 | 
	
		
			
			| 23 |  | -	ew.data = append(ew.data, data...)
 | 
	
		
			
			| 24 |  | -
 | 
	
		
			
			| 25 |  | -	return ew.ResponseWriter.Write(data)
 | 
	
		
			
			| 26 |  | -}
 | 
	
		
			
			| 27 |  | -
 | 
	
		
			
			| 28 |  | -func (ew *errorWriter) successful() bool {
 | 
	
		
			
			| 29 |  | -	switch ew.Status() {
 | 
	
		
			
			| 30 |  | -	case 200, 201, 202, 204:
 | 
	
		
			
			| 31 |  | -		return true
 | 
	
		
			
			| 32 |  | -	case 400, 401, 403, 404, 406, 410, 422, 500:
 | 
	
		
			
			| 33 |  | -		return false
 | 
	
		
			
			| 34 |  | -	default:
 | 
	
		
			
			| 35 |  | -		panic(fmt.Sprintf("unsupported status code %d", ew.Status()))
 | 
	
		
			
			| 36 |  | -	}
 | 
	
		
			
			| 37 |  | -}
 | 
	
		
			
			| 38 |  | -
 | 
	
		
			
			| 39 |  | -func CodeAnalysis() gin.HandlerFunc {
 | 
	
		
			
			| 40 |  | -	return func(ctx *gin.Context) {
 | 
	
		
			
			| 41 |  | -		ew := &errorWriter{ResponseWriter: ctx.Writer}
 | 
	
		
			
			| 42 |  | -		ctx.Writer = ew
 | 
	
		
			
			| 43 |  | -
 | 
	
		
			
			| 44 |  | -		ctx.Next()
 | 
	
		
			
			| 45 |  | -
 | 
	
		
			
			| 46 |  | -		switch ew.Header().Get("Content-Type") {
 | 
	
		
			
			| 47 |  | -		case "application/json; charset=utf-8":
 | 
	
		
			
			| 48 |  | -			rps := map[string]string{}
 | 
	
		
			
			| 49 |  | -
 | 
	
		
			
			| 50 |  | -			err := json.Unmarshal(ew.data, &rps)
 | 
	
		
			
			| 51 |  | -			if err != nil {
 | 
	
		
			
			| 52 |  | -				panic("unsupported data format, only json now.")
 | 
	
		
			
			| 53 |  | -			}
 | 
	
		
			
			| 54 |  | -
 | 
	
		
			
			| 55 |  | -			if !ew.successful() {
 | 
	
		
			
			| 56 |  | -				// 对返回的错误信息格式进行代码检测
 | 
	
		
			
			| 57 |  | -				if len(rps) != 1 || rps["msg"] == "" {
 | 
	
		
			
			| 58 |  | -					panic(fmt.Sprintf("you need return %s but, %s", `{"msg":"content can't empty'"}`, string(ew.data)))
 | 
	
		
			
			| 59 |  | -				}
 | 
	
		
			
			| 60 |  | -			} else {
 | 
	
		
			
			| 61 |  | -				// 如果post请求的状态码不是201的话返回错误
 | 
	
		
			
			| 62 |  | -				if ctx.Request.Method == "POST" && ew.Status() != http.StatusCreated {
 | 
	
		
			
			| 63 |  | -					panic(fmt.Sprintf("you need set http status to %d", http.StatusCreated))
 | 
	
		
			
			| 64 |  | -				}
 | 
	
		
			
			| 65 |  | -
 | 
	
		
			
			| 66 |  | -				// 如果状态码是201但是请求方式不是post的话返回错误
 | 
	
		
			
			| 67 |  | -				if ew.Status() == http.StatusCreated && ctx.Request.Method != "POST" {
 | 
	
		
			
			| 68 |  | -					panic(fmt.Sprintf("the http status %d can set when method=POST", http.StatusCreated))
 | 
	
		
			
			| 69 |  | -				}
 | 
	
		
			
			| 70 |  | -
 | 
	
		
			
			| 71 |  | -				// 如果返回的数据为nil则报错
 | 
	
		
			
			| 72 |  | -				if rps == nil {
 | 
	
		
			
			| 73 |  | -					panic(fmt.Sprintf("if response body is null, please only set http status"))
 | 
	
		
			
			| 74 |  | -				}
 | 
	
		
			
			| 75 |  | -
 | 
	
		
			
			| 76 |  | -				// 如果返回状态码为204则body不能有任何数据
 | 
	
		
			
			| 77 |  | -				if ew.Status() == http.StatusNoContent && len(ew.data) != 0 {
 | 
	
		
			
			| 78 |  | -					panic(fmt.Sprintf("if response stauts is %d, can't have reponse body", http.StatusNoContent))
 | 
	
		
			
			| 79 |  | -				}
 | 
	
		
			
			| 80 |  | -			}
 | 
	
		
			
			| 81 |  | -		default:
 | 
	
		
			
			| 82 |  | -			if len(ew.data) == 0 && ew.Status() != http.StatusNoContent {
 | 
	
		
			
			| 83 |  | -				panic(fmt.Sprintf("if response body is null, please only set http status %d", http.StatusNoContent))
 | 
	
		
			
			| 84 |  | -			}
 | 
	
		
			
			| 85 |  | -
 | 
	
		
			
			| 86 |  | -			if len(ew.data) != 0 {
 | 
	
		
			
			| 87 |  | -				panic("unsupported data format, only json now.")
 | 
	
		
			
			| 88 |  | -			}
 | 
	
		
			
			| 89 |  | -		}
 | 
	
		
			
			| 90 |  | -	}
 | 
	
		
			
			| 91 |  | -}
 |