golang beego router controller 호출 분석

2123 단어 GoLang
beego에서 beego를 호출합니다.Router 메소드 라우팅 추가
router add 방법은 controller Info 구조체에 값을 부여하는 controllerType 속성을 처리합니다
그 중 처리 코드는 다음과 같다beego/router.go
func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
	reflectVal := reflect.ValueOf(c) 
	t := reflect.Indirect(reflectVal).Type()
	route := &controllerInfo{}
	route.controllerType = t
}

저는 루터 컨트롤러만 보고 다른 코드는 다 지웠어요.
controller 처리
router ServeHTTP에서
go net/http Handler interface 다시 쓰기
type Handler interface {  
    ServeHTTP(ResponseWriter, *Request)  
}  

그 중 처리 코드는 다음과 같다beego/router.go
func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) {  
  
    // also defined runRouter & runMethod from filter  
    if !isRunnable {  
        //Invoke the request handler  
        vc := reflect.New(runRouter)  
        //      
        execController, ok := vc.Interface().(ControllerInterface)  
        if !ok {  
            panic("controller is not ControllerInterface")  
        }  
  
        if !context.ResponseWriter.Started {  
            switch runMethod {
                default:
                    //  beego ControllerInterface    
                    //     Controller     HandlerFunc    
                   if !execController.HandlerFunc(runMethod) { 
                        var in []reflect.Value  
                        //         
                        method := vc.MethodByName(runMethod)  
                        //            
                        method.Call(in)  
                    }
            }
 
        }  
   
    }  
  
}  

전체 반사 방법 테스트 코드 호출
package main

import (
	"fmt"
	"reflect"
)

type Xiao struct {
}

func (this Xiao) Xiaochuan() {
	fmt.Println("this is xiao test")
}

func main() {

	reflectVal := reflect.ValueOf(Xiao{})
	t := reflect.Indirect(reflectVal).Type()
	//    
	var in []reflect.Value
	vc := reflect.New(t)
	method := vc.MethodByName("Xiaochuan")
	method.Call(in)
}

좋은 웹페이지 즐겨찾기