go - 如何为静态文件设置http header ?

标签 go cors go-gin

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

最佳答案

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()
}

关于go - 如何为静态文件设置http header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43113649/

相关文章:

go - 使用 Slices 和 Golang sync.Map 结构

go - 来自 PostForm 的 Gin Gonic 值数组

go - nginx 502 错误始终存在,没有应用程序错误

go - 带有到期日期代码的认证代码

go - net/http : is it possible to have http. HandleFunc 自定义参数?

struct - 结构是否实现了其嵌入类型之一实现的所有接口(interface)?

去安装排除文件

javascript - 当来源为 http 且目标 url 为 https 时,如何在本地网络中发出 POST 请求?

c# - Access-Control-Allow-Origin 在 header 请求 ASP.NET Web API 中出现两次

node.js - React Native/Node API : Can't set headers after they are sent