dongqianchi0512 2015-03-30 07:42
浏览 81
已采纳

在Golang中实现GitHub徽章

I have asked this question before and didn't receive satisfactory answer, so this time I'd try to be more specific.

I would like to implement a server in golang which outputs dynamic status updates in the form of svg. (Think "Build Passing/Failing" GitHub Badges.) The purpose is that one should be able to embed a link to the server's address in GitHub Readme and the Readme should update automatically depending on the server state.

Here's the golang code that I came up with but it doesn't seem to work with GitHub aggressive caching. Do I need to add more Cache-Control headers? Do I need to add ETag?

I'm using the following to embed the image in GitHub Readme.

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

Ideally, I would like to see the GitHub Readme change the image every time I load it -- flipping between the two images "correct"/"wrong". (This is just a proof of concept.)

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)
}
  • 写回答

3条回答 默认 最新

  • douxunzui1519 2015-08-28 03:33
    关注

    Following this commit to shields.io server, I made the following changes to the above code and it works now.

    w.Header().Set("Date", date)
    w.Header().Set("Expires", date)
    

    For completeness (and in case someone wants to try it out), here's the complete code. (Also on 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) {
        date := time.Now().Format(http.TimeFormat)
        log.Printf("%v", date)
        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")
        w.Header().Set("Date", date)
        w.Header().Set("Expires", date)
        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)
        log.Println("Server started. Listening on 8085...")
        http.ListenAndServe(":8085", mux)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?