「ゴーによるWeb開発の「ミドルウェア之使用gorilla.handlers

あなたは簡単にログと圧縮機能を使用することができます。

パッケージメイン

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

	"github.com/gorilla/handlers" 

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

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

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

} 

メイン(){FUNC 
	http.HandleFunc(" / favicon.icoを」、iconHandler)
	indexHandler:= HTTP。HandlerFunc(インデックス) 
	aboutHandler:= http.HandlerFunc(約)
	ログファイル、ERR:= os.OpenFile( "のserver.log"、os.O_WRONLY | os.O_CREATE | os.O_APPEND、0666)
	ERR = nilの{もし!
		パニック(ERR)
	} 
	http.Handle( "/"、ハンドラ。 LoggingHandler(ログファイル、handlers.CompressHandler(indexHandler)))
	http.Handle( "/約"、handlers.LoggingHandler(ログファイル、handlers.CompressHandler(aboutHandler)))
	サーバ:=&http.Server { 
		ADDR: ":8080" 
	} 
	log.Println( "リスニング...")
	server.ListenAndServe()

}

  

127.0.0.1 - - [01 / DEC / 2019:13:31:40 0800] "GET / HTTP / 1.1" 200 32 
127.0.0.1 - - [01 / DEC / 2019:13:32:10 0800] " GET / 38 200 HTTP / 1.1"について

 

おすすめ

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