Skip to content

Instantly share code, notes, and snippets.

@AndrewJakubowicz
Created October 25, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewJakubowicz/007808958c325cca04911907c63c0ad2 to your computer and use it in GitHub Desktop.
Save AndrewJakubowicz/007808958c325cca04911907c63c0ad2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"reflect"
"runtime"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello!")
}
func log(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
fmt.Println("LOG: Handler function called - " + name)
h(w, r)
}
}
func log2(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
fmt.Println("LOG2: Handler function called - " + name)
h(w, r)
}
}
func main() {
server := http.Server{
Addr: "127.0.0.1:8080",
Handler: nil,
}
http.HandleFunc("/hello", log(log2(hello)))
server.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment