add ApiHandler
This commit is contained in:
		
							parent
							
								
									136178a1c1
								
							
						
					
					
						commit
						92b821a12d
					
				
							
								
								
									
										19
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								api.go
									
									
									
									
									
								
							@ -77,6 +77,8 @@ func middlewareMyApiEngine() gin.HandlerFunc {
 | 
				
			|||||||
		defer log.Cleanup()
 | 
							defer log.Cleanup()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Step 2. init *api.Context
 | 
							// Step 2. init *api.Context
 | 
				
			||||||
 | 
							req := c.Request
 | 
				
			||||||
 | 
							log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
 | 
				
			||||||
		cc := New(c)
 | 
							cc := New(c)
 | 
				
			||||||
		c.Set("cc", cc)
 | 
							c.Set("cc", cc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -96,6 +98,23 @@ func defaultLogidGetter(c *gin.Context) (logid string) {
 | 
				
			|||||||
	panic("unreachable code")
 | 
						panic("unreachable code")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CC(c *gin.Context) *Context {
 | 
				
			||||||
 | 
						if icc, ok := c.Get("cc"); !ok {
 | 
				
			||||||
 | 
							log.Errorf("Unreachable Code: can not get cc(*api.Context) from *gin.Context")
 | 
				
			||||||
 | 
						} else if cc, ok := icc.(*Context); !ok {
 | 
				
			||||||
 | 
							log.Debugf("Unreachable Code: cc from *gin.Context is type of:[%s]", reflect.TypeOf(icc).String())
 | 
				
			||||||
 | 
							log.Errorf("Unreachable Code: cc from *gin.Context is not type of *api.Context")
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							return cc
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ApiHandler(c *gin.Context) Handler {
 | 
				
			||||||
 | 
						cc := CC(c)
 | 
				
			||||||
 | 
						return cc.ApiHandler()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func DumpHandler(c *Context) error {
 | 
					func DumpHandler(c *Context) error {
 | 
				
			||||||
	errmsg := "api not implemented"
 | 
						errmsg := "api not implemented"
 | 
				
			||||||
	errcode := errCodeUnimplementApi
 | 
						errcode := errCodeUnimplementApi
 | 
				
			||||||
 | 
				
			|||||||
@ -57,9 +57,6 @@ func (c *Context) TryRecover() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Context) BindInput(i interface{}) (err error) {
 | 
					func (c *Context) BindInput(i interface{}) (err error) {
 | 
				
			||||||
	req := c.Request
 | 
					 | 
				
			||||||
	log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logstr := fmt.Sprintf("input:[have not initialized]")
 | 
						logstr := fmt.Sprintf("input:[have not initialized]")
 | 
				
			||||||
	defer func(plogstr *string) {
 | 
						defer func(plogstr *string) {
 | 
				
			||||||
		log.DebugfWithDepth(2, "%s", *plogstr)
 | 
							log.DebugfWithDepth(2, "%s", *plogstr)
 | 
				
			||||||
@ -186,7 +183,7 @@ func (c *Context) ShapeOutput(o interface{}) {
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Context) Handler() Handler {
 | 
					func (c *Context) ApiHandler() Handler {
 | 
				
			||||||
	h := c.Context.Handler()
 | 
						h := c.Context.Handler()
 | 
				
			||||||
	if handler, ok := handlerMapper[fmt.Sprintf("%v", h)]; ok {
 | 
						if handler, ok := handlerMapper[fmt.Sprintf("%v", h)]; ok {
 | 
				
			||||||
		return handler
 | 
							return handler
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user