Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang middleware with just the standard library

Tags:

go

My first stackoverflow question, so please go easy on my naivety about stackoverflow and the question asked, beginner in golang.

I would like to know the difference between the two calls and also simple understanding of the Handle, Handler, HandleFunc, HandlerFunc.

http.Handle("/profile", Logger(profilefunc))
http.HandleFunc("/", HomeFunc)

func main() {            
   fmt.Println("Starting the server.")

   profilefunc := http.HandlerFunc(ProfileFunc)

   http.Handle("/profile", Logger(profilefunc))
   http.HandleFunc("/", HomeFunc)

   http.ListenAndServe("0.0.0.0:8081", nil)
}

func Logger(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
        log.Println("Before serving request")
        h.ServeHTTP(w, r)
        log.Println("After serving request")
    })
}

func ProfileFunc(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "You are on the profile page.")
}

func HomeFunc(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello Imran Pochi")
}
like image 808
Imran Pochi Avatar asked May 22 '17 10:05

Imran Pochi


2 Answers

I would like ... simple understanding of the Handle, Handler, HandleFunc, HandlerFunc.

  • Handler is an interface that can respond to an HTTP request and has a ServeHTTP(ResponseWriter, *Request) method.
  • http.Handle registers a Handler to handle HTTP requests matching a given pattern.
  • http.HandleFunc registers a handler function to handle HTTP requests matching a given pattern. The handler function should be of the form func(ResponseWriter, *Request).
  • HandlerFunc is an explicit function type of the form func(ResponseWriter, *Request). HandlerFunc has a method ServeHTTP that calls itself . This allows you to cast a function to a HandlerFunc and use it as a Handler.

I would to know the difference between the two calls

http.Handle("/profile", Logger(profilefunc))
http.HandleFunc("/", HomeFunc)

Logger is an example of a middleware, which is a function that takes an http.Handler and it returns another http.Handler that wraps the original handler. When called this handler may (or may not) call the nested http.Handler before and/or after performing some operation. So the first line is saying register the profileFunc Handler wrapped in the Logger middleware with the pattern "/profile". The second line is saying register the HomeFunc function with the "/" pattern.

like image 151
Chris Drew Avatar answered Oct 24 '22 01:10

Chris Drew


According to what I've seen from the Go documentation examples:

  • A Handler is a type created to respond to an HTTP request. To make a type a Handler all one has to do is implement a ServeHTTP() method. The ServeHTTP() method does the actual request processing.

  • Handle() takes the route and a Handler, the type that has an instance method named ServeHttp(). Note that it just takes the type, there's no need for one to point at the actual method/function that handles the request explicitly.

  • HandlerFunc is a type that internally implements a ServeHTTP() method. HandlerFunc is used to cast any Go function, with the right signature, to a HandlerFunc. The newly minted HandlerFunc is then passed to the Handle() method together with the route it processes. Note that the HandlerFunc lets one implement request handlers by just writing functions without the need for a dedicated handler type.

  • HandleFunc() takes a route and any function that has the right signature. HandleFunc is shorthand for first of all doing the type casting and then passing the function to the Handle() method.

The signature for a request handling function is:

  func handlerName(wr http.ResponseWriter, req *http.Request)
like image 2
Lym Avatar answered Oct 24 '22 00:10

Lym