- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有以下代码,一切正常。
var view404 = template.Must(template.ParseFiles("views/404.html"))
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
err := view404.Execute(w, nil)
check(err)
}
func main() {
router := mux.NewRouter()
router.StrictSlash(true)
router.NotFoundHandler = http.HandlerFunc(NotFound)
router.Handle("/", IndexHandler).Methods("GET")
router.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
http.Handle("/", router)
http.ListenAndServe(":8000", nil)
}
对像 /cannot/find
这样的路由的请求显示了我的自定义 404 模板。我的 /public/
目录中的所有静态文件也都得到了正确的服务。
我在处理不存在的静态文件和为它们显示自定义 NotFound
处理程序时遇到问题。对 /public/cannot/find
的请求调用标准的 http.NotFoundHandler,它回复
404 page not found
如何为普通路由和静态文件设置相同的自定义 NotFoundHandler?
更新
我最终通过包装 http.ServeFile
实现了我自己的 FileHandler
,正如@Dewy Broto 所建议的。
type FileHandler struct {
Path string
}
func (f FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
prefix := "public"
http.ServeFile(w, r, path.Join(prefix, f.Path))
}
// ...
router.Handle("/css/styles.css", FileHandler{"/css/styles.css"}).Methods("GET")
现在我的 NotFound
处理程序捕获所有丢失的路由,甚至丢失的文件。
最佳答案
文件服务器正在生成 404 响应。 FileServer 处理 mux 传递给它的所有请求,包括丢失文件的请求。有几种方法可以使用自定义 404 页面提供静态文件:
这是第二种方法中描述的包装器的草图:
type hookedResponseWriter struct {
http.ResponseWriter
ignore bool
}
func (hrw *hookedResponseWriter) WriteHeader(status int) {
hrw.ResponseWriter.WriteHeader(status)
if status == 404 {
hrw.ignore = true
// Write custom error here to hrw.ResponseWriter
}
}
func (hrw *hookedResponseWriter) Write(p []byte) (int, error) {
if hrw.ignore {
return len(p), nil
}
return hrw.ResponseWriter.Write(p)
}
type NotFoundHook struct {
h http.Handler
}
func (nfh NotFoundHook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
nfh.h.ServeHTTP(&hookedResponseWriter{ResponseWriter: w}, r)
}
通过包装文件服务器来使用钩子(Hook):
router.PathPrefix("/public/").Handler(NotFoundHook{http.StripPrefix("/public/", http.FileServer(http.Dir("public")))})
这个简单 Hook 的一个警告是它阻止了服务器中从文件复制到套接字的优化。
关于go - 使用 Gorilla Mux 和标准 http.FileServer 的自定义 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26141953/
在围棋中,我尝试了简单的数据库连接。我需要进口大猩猩/MUX,但我不能。。我正在使用VS代码。在cd进入我的项目目录之后,我创建了main.go并运行了go get-u githorb.com/Gor
我得到 session ,来 self 曾经使用的 PHP 在服务器端设置一个或多个键及其值,并能够检索或覆盖它,直到 session 过期。 与 gorilla / session 相同 var(
在 Go 中,我尝试了简单的数据库连接。我需要导入 gorilla/mux ,但我做不到。 我正在使用 VS 代码。之后 cd转到我的项目目录,我创建了 main.go确实跑了go get -u gi
尝试使用 github.com/gorilla/mux 配置 go 服务器路由以响应所有带有 index.html 的请求,但排除扩展名为 .jpg|.js|.png 的请求 由于扩展名而被排除的静态
我从表格中收到日期。该日期具有特定格式“dd/mm/yyyy”。 为了将其解析为我的结构,我使用 gorilla/schema 包,但该包无法将接收到的数据识别为日期。 我如何解析日期并将其放入正确的
我有一个客户端的websocket代码看起来像这样 c, _, err := websocket.DefaultDialer.Dial("", nil) if err != nil {
我正在学习 Go 并遇到了 gorilla/mux 路由器。我想在文本文件中有动态路由,例如: /user/1/post/1 为此,我编写了以下代码: func (s *Server) RegRout
因此,我知道 Gorilla Mux 不支持可选的查询参数,人们建议使用查询参数创建不同的路由,从而使其更加牢固。但是在我的情况下,无法按预期的方式解析路线。 如果我调用/service/{local
我正在使用来自 gorilla websockets 的聊天应用程序示例,但我有一个问题,有时,当后端需要向客户端发送两条不同的消息时,它们只在一个消息事件中发送,这对我来说很糟糕,因为JSON.pa
我想通过 gorilla /websocket发送图像数据的字节,我当前的代码是: var b bytes.Buffer empty := bufio.NewWriter(&b) png.Encode
我正在访问一些我以前写的简单Web服务器的旧代码,并且我的请求模式不再起作用。我有一个初始化我的路线的函数,如下所示: func (a *App) InitializeRoutes() { a
我想创建一个/user 子路由器,如下所示 user := app.Router.PathPrefix("/user").Subrouter() user.HandleFunc("/create",
我尝试关闭连接手册(使用 Dark WebSocket Terminal ),但客户端告诉我 1005(无状态 Rcvd) 服务器: ReadLoop: for { mt, message,
我有以下代码: r := mux.NewRouter() r.HandleFunc("/", homeHandler) r.HandleFunc("/login", loginHandler) adm
请看下面我的代码。如您所见,这是一个具有两个端点的简单服务: /test <-- 使用中间件保护的基本身份验证 /static <-- 提供 ./static 目录中的所有文件 现在我也在尝试将基本身
// SetReadLimit sets the maximum size for a message read from the peer. If a // message exceeds the
我有以下路线: router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1) router.Methods("GET").Pat
因此,我正在使用 Go 和 Gorilla Mux 开发一个简单的 RESTful API。我的第二条路线不工作时遇到问题,它返回 404 错误。我不确定问题出在哪里,因为我是 Go 和 Gorill
我正在 Golang 中构建一个与 Gorilla websockets 通信的网关。 我在 Ubuntu 16.04 上运行它,目前正在使用 .NET 控制台应用程序对其进行测试。 在 Window
我无法让 gorilla mux 工作.. 当请求 http://www.localhost:9000 时,Web 服务器返回 404 page not found 但这有效 http://local
我是一名优秀的程序员,十分优秀!