「ゴーによるWeb開発の」シンプルLoggingMiddlewareを書き込みます

main.go

パッケージメイン

インポート(
	"FMT"
	"ログ"
	"ネット/ HTTP"
	"時間"

FUNC loggingHandler(次http.Handler)http.Handler {
	リターンhttp.HandlerFunc(FUNC(http.ResponseWriter W、R * http.Request){
		開始:= time.Now()
		log.Printf( "開始%S%S"、r.Method、r.URL.Path)
		next.ServeHTTP(W、R)
		log.Printf( "完了%sの%Vの"、r.URL.Path、time.Since(スタート))
	})
}

(http.ResponseWriter W、R * http.Request)FUNCインデックス{
	log.Println(「インデックスハンドラの実行」)
	fmt.Fprintf( "歓迎!"、ワット)
}

約FUNC(W http.ResponseWriter、R * http.Request){
	log.Println(「ハンドラについての実行」)
	fmt.Fprintf( "ゴーミドルウェア!"、ワット)
}

iconHandler FUNC(W http.ResponseWriter、R * http.Request){

}

FUNCのmain(){
	http.HandleFunc( "/ favicon.icoを"、iconHandler)
	indexHandler:= http.HandlerFunc(インデックス)
	aboutHandler:= http.HandlerFunc(約)
	http.Handle( "/"、loggingHandler(indexHandler))
	http.Handle( "/約"、loggingHandler(aboutHandler))

	サーバー:=&http.Server {
		ADDR: ":8080"、
	}
	log.Println( "リスニング...")
	server.ListenAndServe()

}

  

おすすめ

転載: www.cnblogs.com/aguncn/p/11965517.html