简体   繁体   English

在net / http golang中链接中间件

[英]Chaining middleware in net/http golang

I am trying to add context to Authorization middleware. 我正在尝试向授权中间件添加上下文。 The ContextHandler is a handler which will be passed to api handlers to take care of connections and config variables. ContextHandler是一个处理程序,将传递给api处理程序以处理连接和配置变量。 A struct Method ServeHTTP also has been added to the ContextHandler so that it satisfies the net/Http interface for handling requests properly. 结构方法ServeHTTP也已添加到ContextHandler,以便满足用于正确处理请求的net / Http接口。

CheckAuth is the middle ware which takes in the request to check token validation etc, If token is valid, executes the ServeHTTP method and if not, Returns the appropriate error in the response. CheckAuth是接收请求以检查令牌验证等的中间件,如果令牌有效,则执行ServeHTTP方法,如果无效,则在响应中返回适当的错误。

Code compiles, but i am getting error in the ServeHTTP method. 代码可以编译,但是ServeHTTP方法出现错误。

type ContextHandler struct {
    *AppContext
     Handler func(*AppContext, http.ResponseWriter, *http.Request)(int, error)

} }

type AppContext struct {
   Db    *mgo.Session
   Config *simplejson.Json
}


func (ah *ContextedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

status, err := ah.handler(ah.AppContext, w, r)
if err != nil {
     switch status {
    case http.StatusNotFound:
        http.NotFound(w, r)
      case http.StatusInternalServerError:
          http.Error(w, http.StatusText(status), status)
    default:
        http.Error(w, http.StatusText(405), 405)
    }}}





func CheckAuth(h http.Handler) http.Handler {
    log.Println("Entered in CheckAuth")
    f := func( w http.ResponseWriter, r *http.Request) {
    authorizationToken := r.Header.Get("Authorization")
    if authorizationToken != ""{
        secret := []byte("somejunk")
        var credentials authorization
        token, err := jwt.ParseWithClaims(authorizationToken, &credentials, func(t *jwt.Token) (interface{}, error) {
        return []byte(secret), nil
        })

        if err == nil && token.Valid {
            //If everything is fine serve the Http request
            h.ServeHTTP( w, r)
            return 

            } else {
                  //Some response returned
                  json.NewEncoder(w).Encode(response)
                  return 
                  }
        //Check if user exists in the database 
        if dberr != nil {
           //SOmeresponse to be returned
          json.NewEncoder(w).Encode(response)
          return 
        }
      }else{
          response := simplejson.New()
          //response authorization header is missing
          json.NewEncoder(w).Encode(response)
          return 

        }
}
        return http.HandlerFunc(f)

} }

func Initdb(configfile  *simplejson.Json) *mgo.Session {
   //return mongodbsession, copy and close while using it
}

In main.go file in the parent package 
func main() {
      var FileUploadContextHandler *ContextedHandler = &ContextedHandler{&context, filesystem.FileUpload}
     router.Methods("POST").Path("/decentralizefilesystem/fileupload").Name("FileUpload").Handler(CheckAuth(FileUploadContextHandler))
  }

I am getting this error
    2018/07/08 20:45:38 http: panic serving 127.0.0.1:52732: runtime error: invalid memory address or nil pointer dereference
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc4202ce140)
    /usr/local/go/src/net/http/server.go:1726 +0xd0
panic(0x6fe680, 0x92cb10)
    /usr/local/go/src/runtime/panic.go:502 +0x229
gitlab.com/mesha/Gofeynmen/vendor/gopkg.in/mgo%2ev2.(*Session).Copy(0x0, 0x7ff9485fb060)
    /home/feynman/goworkspace/src/gitlab.com/mesha/Gofeynmen/vendor/gopkg.in/mgo.v2/session.go:1589 +0x22
gitlab.com/mesha/Gofeynmen/appsettings.CheckAuth.func1(0x7ff9485fb060, 0xc420276300, 0xc4202e4200)
    /home/feynman/goworkspace/src/gitlab.com/mesha/Gofeynmen/appsettings/appsettings.go:115 +0x361
net/http.HandlerFunc.ServeHTTP(0xc420290180, 0x7ff9485fb060, 0xc420276300, 0xc4202e4200)
    /usr/local/go/src/net/http/server.go:1947 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc42024a310, 0x7ff9485fb060, 0xc420276300, 0xc4202e4200)
    /home/feynman/goworkspace/src/github.com/gorilla/mux/mux.go:162 +0xed
github.com/gorilla/handlers.loggingHandler.ServeHTTP(0x7a8120, 0xc42000e018, 0x7a7b20, 0xc42024a310, 0x7aad60, 0xc4202f0000, 0xc4202e4000)
    /home/feynman/goworkspace/src/github.com/gorilla/handlers/handlers.go:69 +0x123
github.com/gorilla/handlers.(*cors).ServeHTTP(0xc4202c4090, 0x7aad60, 0xc4202f0000, 0xc4202e4000)
    /home/feynman/goworkspace/src/github.com/gorilla/handlers/cors.go:52 +0xa3b
net/http.serverHandler.ServeHTTP(0xc4202da0d0, 0x7aad60, 0xc4202f0000, 0xc4202e4000)
    /usr/local/go/src/net/http/server.go:2694 +0xbc
net/http.(*conn).serve(0xc4202ce140, 0x7ab120, 0xc42025e100)
    /usr/local/go/src/net/http/server.go:1830 +0x651
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2795 +0x27b

It's likely an attempt to dereference ah from (ah *ContextedHandler) , when ah is not a pointer to a ContextedHandler . ah不是ContextedHandler的指针时,很可能尝试从(ah *ContextedHandler)取消引用ah

The types in this assignment don't match up: 此作业中的类型不匹配:

var FileUploadContextHandler *ContextedHandler =
    ContextedHandler{&context, filesystem.FileUpload}

On the left side you have type *ContextedHandler . 在左侧,您输入*ContextedHandler On the right side you have type ContextedHandler . 在右侧,您键入ContextedHandler

Did you mean 你的意思是

var FileUploadContextHandler *ContextedHandler =
    &ContextedHandler{&context, filesystem.FileUpload} 

Or did you mean 还是你的意思

var FileUploadContextHandler ContextedHandler =
    ContextedHandler{&context, filesystem.FileUpload}

?


The argument passed to the CheckAuth function appears to not match the function signature either: 传递给CheckAuth函数的参数似乎与函数签名不匹配:

CheckAuth(FileUploadContextHandler)

FileUploadContextHandler is type *ContextedHandler . FileUploadContextHandler的类型为*ContextedHandler The function signature is: 函数签名为:

func CheckAuth(h contextHandlerFunc) contextHandlerFunc

The type definition of contextHandlerFunc does not appear to be part of the code you shared. contextHandlerFunc的类型定义似乎不属于您共享的代码。


A problem with this line: 这条线有问题:

router.Methods("POST").Path("/decentralizefilesystem/fileupload").Name("FileUpload").Handler(CheckAuth(FileUploadContextHandler))

...would be easier to track down if you broke it up into variable assignments on several lines and then figured out which line the panic pointed to. ...如果您将其分解为几行变量分配,然后弄清楚恐慌所指向的那一行,将更容易追踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM