可以方便的使用日志和压缩等功能。

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/handlers"
)

func index(w http.ResponseWriter, r *http.Request) {
	log.Println("Executing index handler")
	fmt.Fprintf(w, "welcome!")
}

func about(w http.ResponseWriter, r *http.Request) {
	log.Println("Executing about handler")
	fmt.Fprintf(w, "Go Middleware!")
}

func iconHandler(w http.ResponseWriter, r *http.Request) {

}

func main() {
	http.HandleFunc("/favicon.ico", iconHandler)
	indexHandler := http.HandlerFunc(index)
	aboutHandler := http.HandlerFunc(about)
	logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	http.Handle("/", handlers.LoggingHandler(logFile, handlers.CompressHandler(indexHandler)))
	http.Handle("/about", handlers.LoggingHandler(logFile, handlers.CompressHandler(aboutHandler)))
	server := &http.Server{
		Addr: ":8080",
	}
	log.Println("Listening...")
	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 /about HTTP/1.1" 200 38

最新文章

  1. LUA 学习笔记
  2. Android 双卡双待识别
  3. HBase自动分区
  4. UI坐标变换/转换
  5. spark reduce类操作
  6. C语言strlen函数和sizeof操作符
  7. git常见操作--忽略文件以及常用命令【转】
  8. JSP-tag文件使用介绍
  9. VS2010 Web项目需要缺少的Web组件才能加载
  10. JS遍历对象或者数组
  11. VS2010使用附加进程的方式调试IIS中的页面介绍
  12. Java中finally你知多少
  13. VS代码生成工具ReSharper发布8.1版本
  14. [转] iOS ABI Function Call Guide
  15. Linux显示历史记录
  16. 安装mysql8.0出现服务无法启动,服务没报告任何错误
  17. mysql [索引优化] -- in or替换为union all
  18. websocket连接的后台反向代理问题
  19. #CSS margin-top父元素下落
  20. 【Unity/Kinect】Kinect一些常用的API

热门文章

  1. tensorflow add_to_collection用法
  2. Python如何实现微信群万人同步直播?
  3. checkbox多选框取值
  4. 京东云携手HashiCorp,宣布推出Terraform Provider
  5. ObjectPool 对象池设计模式
  6. AI: Web: 2 Vulnhub Walkthrough
  7. mongodb 简单的增删改查
  8. 2019年Java面试题基础系列228道(1)
  9. luoguP4449 于神之怒加强版
  10. deepin镜像 mxlinux镜像 ubuntu镜像桌面版