| 
				
			 | 
			
			
				@@ -0,0 +1,60 @@ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				1
			 | 
			
			
				+package request 
			 | 
		
	
		
			
			| 
				
			 | 
			
				2
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				3
			 | 
			
			
				+import ( 
			 | 
		
	
		
			
			| 
				
			 | 
			
				4
			 | 
			
			
				+	"net/http" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				5
			 | 
			
			
				+	"runtime" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				6
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				7
			 | 
			
			
				+	"github.com/gin-gonic/gin" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				8
			 | 
			
			
				+	"github.com/sirupsen/logrus" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				9
			 | 
			
			
				+) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				10
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				11
			 | 
			
			
				+type Checker interface { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				12
			 | 
			
			
				+	Check() error 
			 | 
		
	
		
			
			| 
				
			 | 
			
				13
			 | 
			
			
				+} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				14
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				15
			 | 
			
			
				+func ParseSuccess(ctx *gin.Context, r interface{}, fs ...func() error) bool { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				16
			 | 
			
			
				+	if r != nil { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				17
			 | 
			
			
				+		if Error(ctx, http.StatusBadRequest, ctx.ShouldBind(r)) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				18
			 | 
			
			
				+			return false 
			 | 
		
	
		
			
			| 
				
			 | 
			
				19
			 | 
			
			
				+		} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				20
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				21
			 | 
			
			
				+		if checker, ok := r.(Checker); ok { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				22
			 | 
			
			
				+			return !Error(ctx, http.StatusBadRequest, checker.Check()) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				23
			 | 
			
			
				+		} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				24
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				25
			 | 
			
			
				+	for _, f := range fs { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				26
			 | 
			
			
				+		if Error(ctx, http.StatusBadRequest, f()) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				27
			 | 
			
			
				+			return false 
			 | 
		
	
		
			
			| 
				
			 | 
			
				28
			 | 
			
			
				+		} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				29
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				30
			 | 
			
			
				+	return true 
			 | 
		
	
		
			
			| 
				
			 | 
			
				31
			 | 
			
			
				+} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				32
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				33
			 | 
			
			
				+func Error(ctx *gin.Context, status int, err error) bool { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				34
			 | 
			
			
				+	if err == nil { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				35
			 | 
			
			
				+		return false 
			 | 
		
	
		
			
			| 
				
			 | 
			
				36
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				37
			 | 
			
			
				+	ctx.JSON(status, gin.H{"msg": err.Error()}) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				38
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				39
			 | 
			
			
				+	if status == http.StatusInternalServerError { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				40
			 | 
			
			
				+		_, file, line, _ := runtime.Caller(1) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				41
			 | 
			
			
				+		logrus.WithField("err", err).Errorf("file:%s line:%d", file, line) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				43
			 | 
			
			
				+	return true 
			 | 
		
	
		
			
			| 
				
			 | 
			
				44
			 | 
			
			
				+} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				45
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				46
			 | 
			
			
				+func Success(ctx *gin.Context, data interface{}) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+	if data == nil { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				48
			 | 
			
			
				+		ctx.Status(http.StatusNoContent) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				49
			 | 
			
			
				+		return 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				51
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+	switch ctx.Request.Method { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				53
			 | 
			
			
				+	case "GET", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS": 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+		ctx.JSON(http.StatusOK, data) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				55
			 | 
			
			
				+	case "POST": 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+		ctx.JSON(http.StatusCreated, data) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				57
			 | 
			
			
				+	default: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				58
			 | 
			
			
				+		ctx.JSON(http.StatusBadRequest, gin.H{"msg": "unsupported request method" + ctx.Request.Method}) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				59
			 | 
			
			
				+	} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				60
			 | 
			
			
				+} 
			 |