mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
Serve badges directly instead of through redirect
This commit is contained in:
@@ -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:])
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user