如何为静态文件设置http标头?

时间:2017-03-30 09:24:17

标签: go cors go-gin

我使用gin-gonic' r.Static("files", "./files")来提供files目录中的所有文件。有没有办法为这些文件请求设置标头,所以我可以允许CORS?

1 个答案:

答案 0 :(得分:2)

an official Gin middleware提供此功能。

良好的起始模板(来自他们的例子)

func main() {
    router := gin.Default()
    // - No origin allowed by default
    // - GET,POST, PUT, HEAD methods
    // - Credentials share disabled
    // - Preflight requests cached for 12 hours
    config := cors.DefaultConfig()
    config.AllowOrigins = []string{"http://google.com"}
    config.AddAllowOrigins("http://facebook.com")
    // config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}

    router.Use(cors.New(config))
    router.Run()
}
相关问题