inject badger db into handlers part 2

This commit is contained in:
Shawn Smith
2019-09-30 21:55:27 +09:00
parent 19ede72e70
commit 815bf67ea2
2 changed files with 3 additions and 9 deletions

View File

@@ -21,15 +21,9 @@ func formatScore(x float64) string {
}
// HighScoresHandler handles the stats page
func HighScoresHandler(w http.ResponseWriter, r *http.Request) {
db, err := badger.Open(badger.DefaultOptions("/tmp/badger"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
func HighScoresHandler(w http.ResponseWriter, r *http.Request, db *badger.DB) {
count, scores := 0, &ScoreHeap{}
err = db.View(func(txn *badger.Txn) error {
err := db.View(func(txn *badger.Txn) error {
var scoreBytes = []byte("[]")
item, err := txn.Get([]byte("scores"))

View File

@@ -119,7 +119,7 @@ func main() {
http.HandleFunc(m.instrument("/checks", injectBadgerHandler(db, handlers.CheckHandler)))
http.HandleFunc(m.instrument("/report/", makeHandler(db, "report", handlers.ReportHandler)))
http.HandleFunc(m.instrument("/badge/", makeHandler(db, "badge", handlers.BadgeHandler)))
http.HandleFunc(m.instrument("/high_scores/", handlers.HighScoresHandler))
http.HandleFunc(m.instrument("/high_scores/", injectBadgerHandler(db, handlers.HighScoresHandler)))
http.HandleFunc(m.instrument("/supporters/", handlers.SupportersHandler))
http.HandleFunc(m.instrument("/about/", handlers.AboutHandler))
http.HandleFunc(m.instrument("/", injectBadgerHandler(db, handlers.HomeHandler)))