gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:58:07 24 4
gpt4 key购买 nike

我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com