net/http/server.go


// Serve a new connection.
serve(){
  serverHandler{c.server}.ServeHTTP(w, w.req)
}


func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request){
   handler := sh.srv.Handler
  if handler == nil {
   handler = DefaultServeMux
}
  if req.RequestURI == "*" && req.Method == "OPTIONS" {
   handler = globalOptionsHandler{}
  }
  handler.ServeHTTP(rw, req)

}


astaxie/beego/router.go


// Implement http.Handler interface.
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")
   }

   //call the controller init function
   execController.Init(context, runRouter.Name(), runMethod, vc.Interface())

   //call prepare function
   execController.Prepare()

   //if XSRF is Enable then check cookie where there has any cookie in the  request's cookie _csrf
   if BConfig.WebConfig.EnableXSRF {
      execController.XSRFToken()
      if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
         (r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) {
         execController.CheckXSRFCookie()
      }
   }

   execController.URLMapping()

   if !context.ResponseWriter.Started {
      //exec main logic
      switch runMethod {
      case "GET":
         execController.Get()
      case "POST":
         execController.Post()
      case "DELETE":
         execController.Delete()
      case "PUT":
         execController.Put()
      case "HEAD":
         execController.Head()
      case "PATCH":
         execController.Patch()
      case "OPTIONS":
         execController.Options()
      default:
         if !execController.HandlerFunc(runMethod) {
            var in []reflect.Value
            method := vc.MethodByName(runMethod)
            method.Call(in)
         }
      }

      //render template
      if !context.ResponseWriter.Started && context.Output.Status == 0 {
         if BConfig.WebConfig.AutoRender {
            if err := execController.Render(); err != nil {
               logs.Error(err)
            }
         }
      }
   }

   // finish all runRouter. release resource
   execController.Finish()
}


}


src/Controllers/loginByLDAP.go


LoginByLDAPController{

  func (c *LoginByLDAPController) Post() {
}

}


src/main.go > beego.Run() >  BeeApp.Run() > go fun(){  app.Server.Serve(ln) } > Server.Serve{
c := srv.newConn(rw)
c.setState(c.rwc, StateNew) // before Serve can return
go c.serve(ctx)  //每个请求起一个goroutine
}   >   // Serve a new connection.
        serve(){
                serverHandler{c.server}.ServeHTTP(w, w.req)
               }


BeeApp  *App 
type App struct {
   Handlers *ControllerRegister
   Server   *http.Server
}


src/routers/router.go

init(){

  beego.Router("/login", &controllers.LoginByLDAPController{})

} > app.Router() {

BeeApp.Handlers.Add(rootpath, c, mappingMethods...)

}

Handlers *ControllerRegister

//ControllerRegister.serveHTTP(rw,r)


beego

beego

2019-05-03 03:06:56 0 162
这里是评论

相关推荐