github - 在 Golang 中实现 GitHub 徽章

标签 github go cache-control

我问过这个问题before并没有得到满意的答复,所以这次我会尝试更具体一些。

我想在 golang 中实现一个服务器,它以 svg 的形式输出动态状态更新。 (想想“构建通过/失败”GitHub 徽章。)目的是应该能够在 GitHub 自述文件中嵌入指向服务器地址的链接,自述文件应根据服务器状态自动更新。

这是我想出的 golang 代码,但它似乎不适用于 GitHub 积极缓存。我需要添加更多 Cache-Control header 吗?我需要添加 ETag 吗?

我正在使用以下内容将图像嵌入到 GitHub 自述文件中。

[![Mine](http://58dcd0b5.ngrok.com/view)]()

理想情况下,我希望看到 GitHub 自述文件在我每次加载图像时更改图像——在“正确”/“错误”两个图像之间切换。 (这只是一个概念证明。)

package main

import (
    "log"
    "net/http"
    _ "time"
)
var mymap map[string][]byte

var state bool = false


func viewHandler(w http.ResponseWriter, r *http.Request) {
    log.Printf("State %v", state)
    state = !state
    w.Header().Set("Content-Type", "image/svg+xml")
    w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
    if state {
        w.Write(mymap["correct"])
    } else {
        w.Write(mymap["wrong"])
    }
}

func main() {
    mymap = make(map[string][]byte)
    mymap["correct"] = []byte(`<svg xmlns="http://www.w3.org/2000/svg" width="104" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="104" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h54v20H0z"/><path fill="#4c1" d="M54 0h50v20H54z"/><path fill="url(#b)" d="M0 0h104v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="28" y="15" fill="#010101" fill-opacity=".3">solution</text><text x="28" y="14">solution</text><text x="78" y="15" fill="#010101" fill-opacity=".3">correct</text><text x="78" y="14">correct</text></g></svg>`)
    mymap["wrong"] = []byte(`<svg xmlns="http://www.w3.org/2000/svg" width="99" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="99" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h54v20H0z"/><path fill="#e05d44" d="M54 0h45v20H54z"/><path fill="url(#b)" d="M0 0h99v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="28" y="15" fill="#010101" fill-opacity=".3">solution</text><text x="28" y="14">solution</text><text x="75.5" y="15" fill="#010101" fill-opacity=".3">wrong</text><text x="75.5" y="14">wrong</text></g></svg>`)

    mux := http.NewServeMux()
    mux.HandleFunc("/view", viewHandler)
    http.ListenAndServe(":8085", mux)
}

最佳答案

这是 travis 为他们的图片提供的内容:

Age:0
Cache-Control:no-cache
Content-Length:0
Date:Mon, 30 Mar 2015 07:49:10 GMT
ETag:"88e168c2d5cdb30ee9af739765e78e4d"
Expires:Mon, 30 Mar 2015 07:49:10 GMT
Keep-Alive:timeout=10, max=48
Last-Modified:Wed, 07 Jan 2015 11:26:53 GMT
Timing-Allow-Origin:https://github.com
X-Timer:S1427701750.146025,VS0,VE156

这可能是一个好的开始,尝试这些并看看有什么用。

关于github - 在 Golang 中实现 GitHub 徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340796/

相关文章:

datetime - 如何在 Go 中使用 GMT 格式获取当前日期和时间?

go - 删除自己的二进制文件的可能性

cross-browser - 哪些浏览器支持 stale-if-error 和 stale-while-revalidate?

github - 在 Windows SERVER 上安装 github

github - Google Cloud 为 GitHub Repo 构建触发器说 "No tag matches"总是

git - 使用 github 获取资源

GitHub 仓库转移

oracle - 使用内联约束更改表时出现 ORA-01735

asp.net-core - 将浏览器缓存策略应用于所有 ASP.NET Core MVC 页面

android - 清除所有应用程序的缓存