做租号玩网站赚钱吗,网站建设的好处论文,wordpress 增加浮动条,简述app开发流程学习记录 1 使用中间件1.1 测试一下1.2 push代码 2 URI 中的斜杆2.1 StrictSlash2.2 兼容 POST 请求 1 使用中间件
代码中存在重复率很高的代码
w.Header().Set(Content-Type, text/html; charsetutf-8)统一对响应做处理的#xff0c;我们可以使用中… 学习记录 1 使用中间件1.1 测试一下1.2 push代码 2 URI 中的斜杆2.1 StrictSlash2.2 兼容 POST 请求 1 使用中间件
代码中存在重复率很高的代码
w.Header().Set(Content-Type, text/html; charsetutf-8)统一对响应做处理的我们可以使用中间件来做 使用中间件后的代码
package mainimport (fmtnet/httpgithub.com/gorilla/mux
)func homeHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, h1Hello, 欢迎来到 goblog/h1)
}func aboutHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, 此博客是用以记录编程笔记如您有反馈或建议请联系 a href\mailto:summerexample.com\summerexample.com/a)
}func notFoundHandler(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusNotFound)fmt.Fprint(w, h1请求页面未找到 :(/h1p如有疑惑请联系我们。/p)
}func articlesShowHandler(w http.ResponseWriter, r *http.Request) {vars : mux.Vars(r)id : vars[id]fmt.Fprint(w, 文章 IDid)
}func articlesIndexHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, 访问文章列表)
}func articlesStoreHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, 创建新的文章)
}func forceHTMLMiddleware(next http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {// 1. 设置标头w.Header().Set(Content-Type, text/html; charsetutf-8)// 2. 继续处理请求next.ServeHTTP(w, r)})
}func main() {router : mux.NewRouter()router.HandleFunc(/, homeHandler).Methods(GET).Name(home)router.HandleFunc(/about, aboutHandler).Methods(GET).Name(about)router.HandleFunc(/articles/{id:[0-9]}, articlesShowHandler).Methods(GET).Name(articles.show)router.HandleFunc(/articles, articlesIndexHandler).Methods(GET).Name(articles.index)router.HandleFunc(/articles, articlesStoreHandler).Methods(POST).Name(articles.store)// 自定义 404 页面router.NotFoundHandler http.HandlerFunc(notFoundHandler)// 中间件强制内容类型为 HTMLrouter.Use(forceHTMLMiddleware)// 通过命名路由获取 URL 示例homeURL, _ : router.Get(home).URL()fmt.Println(homeURL: , homeURL)articleURL, _ : router.Get(articles.show).URL(id, 1)fmt.Println(articleURL: , articleURL)http.ListenAndServe(:3000, router)
}这段代码定义了一个名为 forceHTMLMiddleware 的函数它是一个中间件函数接受一个 http.Handler 类型的参数 h并返回一个经过处理后的 http.Handler。
让我们逐步解释这段代码的功能 函数定义 func forceHTMLMiddleware(h http.Handler) http.Handler { ... }这是一个函数定义它接受一个 http.Handler 类型的参数 h表示要执行的下一个处理程序handler并返回一个经过处理后的 http.Handler。 中间件功能 这个中间件的功能是强制将响应的内容类型设置为 HTML 格式并指定字符集为 UTF-8。w.Header().Set(Content-Type, text/html; charsetutf-8)在处理请求之前通过 w.Header().Set 方法设置响应头中的 Content-Type 字段为 text/html; charsetutf-8表示响应内容为 HTML 格式并且字符集为 UTF-8。h.ServeHTTP(w, r)然后调用传入的下一个处理程序 h 的 ServeHTTP 方法继续处理请求并生成响应。 返回处理程序 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ... })返回一个匿名函数该函数实现了 http.Handler 接口的 ServeHTTP 方法用于处理请求并设置响应头。
通过使用这个中间件函数您可以确保每个经过该中间件的请求的响应内容类型都被强制设置为 HTML 格式并且字符集为 UTF-8。这有助于确保一致的响应格式和字符编码。您可以将这个中间件应用于您的 HTTP 处理程序链中以实现统一的响应处理逻辑。
1.1 测试一下
localhost:3000/about
1.2 push代码
//有时因为github访问受限的原因Push失败可开加速器后重新push fatal: unable to access https://github.com/SGY321/goblog.git/: Failed to connect to github.com port 443 after 21179 ms: Couldnt connect to server git add . git commit -m “使用中间件” git push 2 URI 中的斜杆
访问以下两个链接 localhost:3000/about localhost:3000/about/ 有 / 的链接会报 404 错误 希望 URL 后面是否加斜杆的情况下皆使用同一个返回结果
2.1 StrictSlash Gorilla Mux 提供了一个 StrictSlash(value bool) 函数 slash中文斜杠 浏览器再次访问 localhost:3000/about/ 显示成功 可以看到当请求 about/ 时产生了两个请求第一个是 301 跳转第二个是跳转到的 about 去掉斜杆的链接。
浏览器在处理 301 请求时会缓存起来。后续的 about/ 浏览器都会自动去请求 about 链接也就是说两次请求只会在第一次的时候发生。
这个解决方案看起来不错然而有一个严重的问题 —— 当请求方式为 POST 的时候遇到服务端的 301 跳转将会变成 GET 方式。很明显这并非所愿我们需要一个更好的方案。
2.2 兼容 POST 请求
还原上面的修改
git checkout .写一个函数把 Gorilla Mux 包起来在这个函数中我们先对进来的请求做处理然后再传给 Gorilla Mux 去解析。
.
.
.
func removeTrailingSlash(next http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {// 1. 除首页以外移除所有请求路径后面的斜杆if r.URL.Path ! / {r.URL.Path strings.TrimSuffix(r.URL.Path, /)}// 2. 将请求传递下去next.ServeHTTP(w, r)})
}func main() {...http.ListenAndServe(:3000, removeTrailingSlash(router))
} 文章转载自: http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.mjxgs.cn.gov.cn.mjxgs.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.nrchx.cn.gov.cn.nrchx.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.lwlnw.cn.gov.cn.lwlnw.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn