将http.Handler放入martini中。

huangapple go评论76阅读模式
英文:

Put the http.Handler in martini

问题

如何像http.FileServer一样与martini集成?

package main

import (
	"github.com/go-martini/martini"
	"net/http"
)

func main() {
	m := martini.Classic()
	m.Use(martini.Static(".")) // 添加这一行
	m.Run()
}

通过添加m.Use(martini.Static(".")),你可以在martini中集成静态文件服务,就像http.FileServer一样。

英文:

How do I integrate just like http.FileServer with martini?
` package main

import (
    "github.com/go-martini/martini"
    "net/http"
)

func main() {
    m := martini.Classic()
    //http.Handle("/", http.FileServer(http.Dir("."))) //It doesn't work!
    m.Run()
}`

答案1

得分: 1

我相信 FileServer 在 Martini 中并没有直接使用:参见 issues/20

> 不幸的是,如果没有匹配项,文件服务器中间件会抛出 404 错误,这意味着我们需要自己编写

因此在 static.go 中可以看到 PR 26commit a945713,你可以在 static_test.go 中看到它们的使用。

m := New()
r := NewRouter()
m.Use(Static(currentRoot))
m.Action(r.Handle)
英文:

I believe the FileServer isn't used directly in Martini: see issues/20:

> Unfortunately The fileserver middleware throws a 404 if there is no match, which means we will need to roll our own

Hence PR 26 and commit a945713 in static.go that you can see in static_test.go

m := New()
r := NewRouter()
m.Use(Static(currentRoot))
m.Action(r.Handle)

huangapple
  • 本文由 发表于 2015年3月2日 23:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/28814114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定