Created
October 14, 2013 04:33
-
-
Save tenntenn/6970823 to your computer and use it in GitHub Desktop.
[Go言語] httpハンドラの共通部分を取り出していい感じにする ref: http://qiita.com/tenntenn/items/b7bd54c7ba0ff90f1707
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" | |
"log" | |
"net/http" | |
) | |
func baseHandlerFunc(handler func(w http.ResponseWriter, r *http.Request)) http.Handler { | |
return baseHandler(http.HandlerFunc(handler)) | |
} | |
func baseHandler(handler http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// common | |
log.Println(r.URL, r.Method) | |
handler.ServeHTTP(w, r) | |
}) | |
} | |
// handler | |
func index(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "hello") | |
} | |
func main() { | |
http.Handle("/", baseHandlerFunc(index)) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment