《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)思想 ...
随机推荐
- Nginx模块及配置虚拟主机
1.Nginx的2组主要的模块 (1)core modules (必需,核心模块) 包括:Main.Events (2)Standard HTTP modules(虽然不是必需,但是缺省都会安装,不建 ...
- Power BI 概念及 国内版Pro 试用账户注册流程
视频内容: Power BI 基本概念:https://v.qq.com/x/page/s3026nn69eu.html Power BI Pro 世纪互联版本试用账号注册:https://v.qq. ...
- (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法
原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...
- Cookie插件的使用
jQuery-cookie插件的使用 什么是插件? 基于jQuery的语法,按照一定规范书写,具有特定功能的脚本文件,称为插件. 插件除了js文件之外,有的还包含css文件,图片和字体等资源文件. 在 ...
- Mac Electron App 签名后打开闪退
背景 昨天在测试 Mac Electron App 打包,发现不签名的应用能够正常打开,签了名的打开反而会崩溃. 寻因 首先我怀疑是不是自己代码导致闪退,但是在一番查找后,发现还根本没到执行我的代码就 ...
- HTTP Error 500.19 - Internal Server Error 无法读取配置文件
将Code移动文件夹就报以下错误,http error 500.19 - internal server error. 项目文件下.vs=>config=>applicationhost. ...
- zipalign的使用
zipalign 是一个存档对齐工具,可为Android应用程序(.apk)文件提供重要的优化.目的是确保所有未压缩数据以相对于文件开头的特定对齐开始.具体来说,它会导致.apk中的所有未压缩数据(如 ...
- [20191125]探究等待事件的本源.txt
[20191125]探究等待事件的本源.txt --//当工作中遇到oracle的性能问题时,查看awr报表提供很好的解决问题途径.但是有时候很容易想当然.--//比如以前我一看到 log file ...
- Spring注解--实现applicationContext.xml效果
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件. Configuration注解: Spring解 ...
- CodeForces - 460C(二分+差分)
题意 https://vjudge.net/problem/CodeForces-460C 一个长度为 n 的序列 a ,你有 m 次操作的机会,每次操作是将其中连续的 w 个元素增加 1 .最大化最 ...