Serve badges directly instead of through redirect

This commit is contained in:
Herman Schaaf
2016-08-17 22:34:14 +08:00
parent c4876285e7
commit 9009aacf64
2 changed files with 3 additions and 13 deletions

View File

@@ -3,18 +3,12 @@ package handlers
import (
"log"
"net/http"
"strings"
)
// AssetsHandler handles serving static files
func AssetsHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Serving " + r.URL.Path[1:])
if strings.HasSuffix(r.URL.Path, ".svg") {
// don't cache badges
w.Header().Set("Cache-control", "no-store, no-cache, must-revalidate")
}
http.ServeFile(w, r, r.URL.Path[1:])
}

View File

@@ -43,11 +43,8 @@ func grade(percentage float64) Grade {
}
}
func badgeURL(grade Grade, style string, dev bool) string {
if dev {
return fmt.Sprintf("http://localhost:8000/assets/badges/%s_%s.svg", strings.ToLower(string(grade)), strings.ToLower(style))
}
return fmt.Sprintf("https://goreportcard.com/assets/badges/%s_%s.svg", strings.ToLower(string(grade)), strings.ToLower(style))
func badgePath(grade Grade, style string, dev bool) string {
return fmt.Sprintf("assets/badges/%s_%s.svg", strings.ToLower(string(grade)), strings.ToLower(style))
}
// BadgeHandler handles fetching the badge images
@@ -69,6 +66,5 @@ func BadgeHandler(w http.ResponseWriter, r *http.Request, repo string, dev bool)
}
w.Header().Set("Cache-control", "no-store, no-cache, must-revalidate")
http.Redirect(w, r, badgeURL(resp.Grade, style, dev), http.StatusTemporaryRedirect)
http.ServeFile(w, r, badgePath(resp.Grade, style, dev))
}