dragon87836215 2017-03-30 09:24
浏览 123
已采纳

如何设置静态文件的HTTP标头?

I use gin-gonic's r.Static("files", "./files") to serve all files in the files directory. Is there a way to set headers for these file requests so I can allow CORS?

  • 写回答

1条回答 默认 最新

  • dsgsdg206050 2017-03-31 03:52
    关注

    There is an official Gin middleware providing this functionality.

    A Good starting template (from their examples)

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突