-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
report.go
54 lines (46 loc) · 1.45 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package handlers
import (
"encoding/json"
"log"
"net/http"
"flag"
"github.com/dgraph-io/badger/v2"
)
var domain = flag.String("domain", "goreportcard.com", "Domain used for your goreportcard installation")
var googleAnalyticsKey = flag.String("google_analytics_key", "G-TFTF5Y92QD", "Google Analytics Account ID (GA4)")
// ReportHandler handles the report page
func (gh *GRCHandler) ReportHandler(w http.ResponseWriter, r *http.Request, db *badger.DB, repo string) {
log.Printf("Displaying report: %q", repo)
t, err := gh.loadTemplate("/templates/report.html")
if err != nil {
log.Println("ERROR: could not get report template: ", err)
http.Error(w, err.Error(), 500)
return
}
resp, err := getFromCache(db, repo)
needToLoad := false
if err != nil {
switch err.(type) {
case notFoundError:
// don't bother logging - we already log in getFromCache. continue
default:
log.Println("ERROR ReportHandler:", err) // log error, but continue
}
needToLoad = true
}
respBytes, err := json.Marshal(resp)
if err != nil {
log.Println("ERROR ReportHandler: could not marshal JSON: ", err)
http.Error(w, "Failed to load cache object", 500)
return
}
if err := t.Execute(w, map[string]interface{}{
"repo": repo,
"response": string(respBytes),
"loading": needToLoad,
"domain": domain,
"google_analytics_key": googleAnalyticsKey,
}); err != nil {
log.Println("ERROR:", err)
}
}