mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
add -dev flag and pass it to report and badge handlers. only badge handler needs it for now. add new make target for starting in dev mode
This commit is contained in:
3
Makefile
3
Makefile
@@ -16,5 +16,8 @@ test:
|
||||
start:
|
||||
go run main.go
|
||||
|
||||
start-dev:
|
||||
go run main.go -dev
|
||||
|
||||
misspell:
|
||||
find . -name '*.go' | xargs misspell -error
|
||||
|
||||
@@ -43,12 +43,15 @@ func grade(percentage float64) Grade {
|
||||
}
|
||||
}
|
||||
|
||||
func badgeURL(grade Grade, style string) string {
|
||||
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))
|
||||
}
|
||||
|
||||
// BadgeHandler handles fetching the badge images
|
||||
func BadgeHandler(w http.ResponseWriter, r *http.Request, repo string) {
|
||||
func BadgeHandler(w http.ResponseWriter, r *http.Request, repo string, dev bool) {
|
||||
name := fmt.Sprintf("%s", repo)
|
||||
resp, err := newChecksResp(name, false)
|
||||
|
||||
@@ -65,5 +68,5 @@ func BadgeHandler(w http.ResponseWriter, r *http.Request, repo string) {
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, badgeURL(resp.Grade, style), http.StatusTemporaryRedirect)
|
||||
http.Redirect(w, r, badgeURL(resp.Grade, style, dev), http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ var domain = flag.String("domain", "goreportcard.com", "Domain used for your gor
|
||||
var googleAnalyticsKey = flag.String("google_analytics_key", "UA-58936835-1", "Google Analytics Account Id")
|
||||
|
||||
// ReportHandler handles the report page
|
||||
func ReportHandler(w http.ResponseWriter, r *http.Request, repo string) {
|
||||
func ReportHandler(w http.ResponseWriter, r *http.Request, repo string, dev bool) {
|
||||
log.Printf("Displaying report: %q", repo)
|
||||
t := template.Must(template.New("report.html").Delims("[[", "]]").ParseFiles("templates/report.html"))
|
||||
resp, err := getFromCache(repo)
|
||||
|
||||
13
main.go
13
main.go
@@ -14,9 +14,12 @@ import (
|
||||
"github.com/boltdb/bolt"
|
||||
)
|
||||
|
||||
var addr = flag.String("http", ":8000", "HTTP listen address")
|
||||
var (
|
||||
addr = flag.String("http", ":8000", "HTTP listen address")
|
||||
dev = flag.Bool("dev", false, "dev mode")
|
||||
)
|
||||
|
||||
func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||
func makeHandler(name string, dev bool, fn func(http.ResponseWriter, *http.Request, string, bool)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
validPath := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_\/\.]+)$`, name))
|
||||
|
||||
@@ -49,7 +52,7 @@ func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string
|
||||
return
|
||||
}
|
||||
|
||||
fn(w, r, repo)
|
||||
fn(w, r, repo, dev)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,8 +90,8 @@ func main() {
|
||||
http.HandleFunc("/assets/", handlers.AssetsHandler)
|
||||
http.HandleFunc("/favicon.ico", handlers.FaviconHandler)
|
||||
http.HandleFunc("/checks", handlers.CheckHandler)
|
||||
http.HandleFunc("/report/", makeHandler("report", handlers.ReportHandler))
|
||||
http.HandleFunc("/badge/", makeHandler("badge", handlers.BadgeHandler))
|
||||
http.HandleFunc("/report/", makeHandler("report", *dev, handlers.ReportHandler))
|
||||
http.HandleFunc("/badge/", makeHandler("badge", *dev, handlers.BadgeHandler))
|
||||
http.HandleFunc("/high_scores/", handlers.HighScoresHandler)
|
||||
http.HandleFunc("/about/", handlers.AboutHandler)
|
||||
http.HandleFunc("/", handlers.HomeHandler)
|
||||
|
||||
Reference in New Issue
Block a user