当前位置:网站首页>Go语言web中间件的使用
Go语言web中间件的使用
2022-04-23 02:26:00 【田土豆】
前提概要
承接上一回使用Go语言构建Web服务器,我们实现了一个简易的应用HelloWorld,通过监听本地8080端口,提供web服务。客户端通过访问http://127.0.0.1:8080/可以获取返回值 “Hello World!”
故事背景
现在服务端想要统计每次响应请求消耗的时间(这里只涉及app处理请求消耗的时间,不包含链路通信等其他时间),我们可以对HelloWorld作如下简单修改
func HelloWorld(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
_, err := fmt.Fprintf(w, "Hello World!")
if err != nil {
log.Panic(err)
}
timeElapse := time.Since(timeStart)
log.Println(timeElapse)
}
现在如果有第二个,第三个应用,我们也可以进行简单的代码添加。但如果应用很多,30个、100个,这种方法显然就不行了。问题的关键就在于业务代码和非业务代码(时间统计)夹杂在一起了
中间件的引入
思路很简单,就是服务器将获取到的request请求先传递给时间统计的中间件timeMiddleware,然后再由中间件传递给应用HelloWorld,下面是timeMiddleware简单的实现
func timeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
//传递参数给下一个http.Handler
next.ServeHTTP(w, r)
timeElapse := time.Since(timeStart)
log.Println(timeElapse)
})
}
在main函数中开启监听
func main() {
http.Handle("/", timeMiddleware(http.HandlerFunc(HelloWorld)))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Panic(err)
}
}
这里需要补充说明http.Handler、http.HandlerFunc、ServeHttp之间的关系
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
type HandlerFunc func(ResponseWriter, *Request)
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
Handler是一个接口,只有一个方法ServeHttp,HandlerFunc是一个函数类型,实现了Handler接口,所以在timeMiddleware中通过调用next.ServeHTTP(w, r) 就是在传递参数执行app
现在拓展一下,如果要继续添加一个log功能,我们可以这么实现
//新增一个中间件用于log的生成
func logMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
file, _ := os.OpenFile("test.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
logger := log.New(file, "", log.Llongfile|log.LstdFlags)
//将w,r传递给下一个handler
h.ServeHTTP(w, r)
logger.Println("请求处理完成")
})
}
func main() {
http.Handle("/", logMiddleware(timeMiddleware(http.HandlerFunc(HelloWorld))))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Panic(err)
}
}
通过浏览器访问后,后台可以获取如下的log信息:
2022/04/16 19:32:07 E:/goland-workspace/goRestful/httptest/main.go:38: 请求处理完成
2022/04/16 19:32:07 E:/goland-workspace/goRestful/httptest/main.go:38: 请求处理完成
版权声明
本文为[田土豆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42216109/article/details/124218325
边栏推荐
- The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
- 双亲委派模型【理解】
- Develop a chrome plug-in from 0 (2)
- MySQL C language connection
- Open3d point cloud processing
- Talk about current limiting
- Latin goat (20204-2022) - daily question 1
- Halo open source project learning (I): project launch
- 007_ Redis_ Jedis connection pool
- Wechat public platform test number application, authorized login function and single sign on using hbuilder X and wechat developer tools
猜你喜欢

Talk about current limiting

89 logistic回归用户画像用户响应度预测

Arduino esp8266 network upgrade OTA

After idea is successfully connected to H2 database, there are no sub files

Halo open source project learning (I): project launch

Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储

002_ Redis_ Common operation commands of string type
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售

都是做全屋智能的,Aqara和HomeKit到底有什么不同?

每日一题冲刺大厂第十六天 NOIP普及组 三国游戏
随机推荐
Global, exclusive and local routing guard
Summary of I / O knowledge points
Open3d point cloud processing
Is CICC fortune a company with CICC? Is it safe
Redis memory recycling strategy
C # import details
简洁开源的一款导航网站源码
World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
Talk about biology live broadcast: Dr. Wang Ziyuan, a lake view biology, exploring hepatitis B with gene therapy
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
Easyswool environment configuration
RT_Thread自问自答
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
Talk about current limiting
002_Redis_String类型常见的操作命令
The usage and difference of * and & in C language and the meaning of keywords static and volatile
Flink real-time data warehouse project - Design and implementation of DWS layer
Leetcode46 Full Permutation
[nk]牛客月赛48 D
89 logistic回歸用戶畫像用戶響應度預測