《Web Development with Go》两个Middleware执行顺序
也加了如果有认证时的执行流程:
一个错误,
一个正确。
package main
import (
"fmt"
"log"
"net/http"
//"time"
)
func middlewareFirst(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("MiddlewareFirst - Before Handler")
next.ServeHTTP(w, r)
log.Println("MiddlewareFirst - After Handler")
})
}
func middlewareSecond(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("MiddlewareSecond - Before Handler")
if r.URL.Path == "/message" {
if r.URL.Query().Get("password") == "password" {
log.Println("Authorized to the system")
next.ServeHTTP(w, r)
} else {
log.Println("Failed to authorize to the system")
return
}
} else {
next.ServeHTTP(w, r)
}
log.Println("MiddlewareSecond - After Handler")
})
}
func index(w http.ResponseWriter, r *http.Request) {
log.Println("Executing index handler")
fmt.Fprintf(w, "welcome!")
}
func message(w http.ResponseWriter, r *http.Request) {
log.Println("Executing message handler")
fmt.Fprintf(w, "HTTP Middleware is awesome!")
}
func iconHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
http.HandleFunc("/favicon.ico", iconHandler)
http.Handle("/", middlewareFirst(middlewareSecond(http.HandlerFunc(index))))
http.Handle("/message", middlewareFirst(middlewareSecond(http.HandlerFunc(message))))
server := &http.Server{
Addr: ":8080",
}
log.Println("Listening...")
server.ListenAndServe()
}

《Web Development with Go》两个Middleware执行顺序的更多相关文章
- promise.then, setTimeout,await执行顺序问题
promise.then VS setTimeout 在chrome和node环境环境中均输出2, 3, 1, 先输出2没什么好说的,3和1顺序让人有些意外 原因: 有一个事件循环,但是任务队列可以有 ...
- Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation
The Original link : http://zeroturnaround.com/rebellabs/rjc301/ Copyright reserved by Rebel Inc In t ...
- 【外文阅读】Web Development in 2020: What Coding Tools You Should Learn---Quincy Larson
原文链接:https://mail.qq.com/cgi-bin/readtemplate?t=safety&check=false&gourl=https%3A%2F%2Fwww.f ...
- WEB API 系列(二) Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- WEB API Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- 《Agile Web Development With Rails》读后感--rails基于web设计的best Practices
最近看完<Agile Web Development with Rails>一书,受益匪浅.书中先是用一个简单的web应用带你进入Rails的世界,然后在你大致熟悉之后,再带你了解Rail ...
- php课程 1-3 web项目中php、html、js代码的执行顺序是怎样的(详解)
php课程 1-3 web项目中php.html.js代码的执行顺序是怎样的(详解) 一.总结 一句话总结:b/s结构 总是先执行服务器端的先.js是客户端脚本 ,是最后执行的.所以肯定是php先执行 ...
- 详解Java Web项目启动执行顺序
一. web.xml加载过程(步骤): 启动web项目,容器(如Tomcat.Apache)会去读取它的配置文件web.xml 中的两个节点,context-param和listener. 紧接着,容 ...
- Web API系列(二) Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
随机推荐
- 解密国内BAT等大厂前端技术体系-腾讯篇(长文建议收藏)
1 引言 为了了解当前前端的发展趋势,让我们从国内各大互联网大厂开始,了解他们的最新动态和未来规划.这是解密大厂前端技术体系的第三篇,前两篇已经讲述了阿里和百度在前端技术这几年的技术发展.这一篇从腾讯 ...
- Maven发布封装到中央仓库时候报错:no default secret key: No secret key
今天因为发布swagger-spring-boot-starter做一个问题的修复,然后碰到了下面这个问题,记录一下解决过程,帮助后续碰到类似问题的童鞋: *gpg: WARNING: "- ...
- .Net Core 项目发布到Linux - CentOS 7(二)用Supervisor守护netcore进程
简介 supervisor可以保证程序崩溃后,可以重新把程序启动起来等相关功能. 安装 yum install -y supervisor 安装好后在/etc/会生成一个supervisord.con ...
- [转]BEC Vantage
https://www.examenglish.com/BEC/BEC_Vantage.html https://www.cambridgeenglish.org/exams-and-tests/bu ...
- Kali Linux configuration "Ettercap"
Xx_Instroduction Ettercap is a man-in-the-middle attack(MITM) tool,kali take this tool,so,use front ...
- WordPress 文件下载漏洞
Google dork:inurl:"/wp-content/themes/liberator/inc/php/download.php" exploit:https://www. ...
- 45-管理 Machine
用 docker-machine 创建 machine 的过程很简洁,非常适合多主机环境.除此之外,Docker Machine 也提供了一些子命令方便对 machine 进行管理.其中最常用的就是无 ...
- Troubleshooting: High Version Count Issues
--查询版本高的原因 select * from v$sql_shared_cursor where sql_id=''; Configuring Download the script in the ...
- SQL Server Agent作业执行CmdExec(bat)命令报权限问题
写了一个bat命令,定期去清理一些SQL Server的Dump文件,然后配置成SQL Server作业,作业执行时报权限错误,具体错误信息如下所示: Message Executed as user ...
- 字符串 string方法
字符串 name = 'ab c dd' i = name.find('a', 1, 3) # 找到返回对应下标 找不到返回-1 print(i) j = name.rfind('d') # 寻找对应 ...