Created
October 25, 2016 07:30
-
-
Save AndrewJakubowicz/007808958c325cca04911907c63c0ad2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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