-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
121 lines (113 loc) · 3.58 KB
/
api.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"fmt"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"go_report/domain"
"go_report/failure"
"go_report/gh"
"log"
"time"
"net/http"
)
func PingHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("pong")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
})
}
// Gets all reports with content
func GetAllHandler(s domain.Storer) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reports, err := s.SelectAll()
if err != nil {
failure.Fail(w, err)
return
}
if err := json.NewEncoder(w).Encode(reports); err != nil {
failure.Fail(w, err)
return
}
})
}
// return content of files
func GetGroupHandler(s domain.Storer) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the stored reports in json corresponding to an GID (i.e. {<id>:[...files]})
gid := r.Context().Value(string(ReportGIDVar)).(string)
if gid == "" {
GetAllHandler(s)(w, r)
return
}
reports, err := s.SelectGroup(gid)
if err != nil {
failure.Fail(w, err)
return
}
if err = json.NewEncoder(w).Encode(reports); err != nil {
failure.Fail(w, err)
return
}
})
}
// return content of file
func GetReportHandler(s domain.Storer) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g, k := r.Context().Value(string(ReportGIDVar)).(string), r.Context().Value(string(ReportKeyVar)).(string)
rpt, err := s.Select(domain.Receipt{
Key: k,
GID: g,
})
if err != nil {
failure.Fail(w, err)
return
}
if err := json.NewEncoder(w).Encode(rpt); err != nil {
failure.Fail(w, errors.Wrap(err, "failed to encode report json to http writer response stream"))
return
}
})
}
func PostHandler(issThreshold int, s domain.Storer, ghs *gh.Service, logger *log.Logger) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// read rpt from context
rpt := r.Context().Value(string(ReportCtxVar)).(domain.Report)
rpt.ReceivedOn = time.Now()
// add to s
rr, err := s.NewEntry(rpt)
if err != nil {
failure.Fail(w, failure.New(errors.Wrap(err, "failed to create store entry"), http.StatusInternalServerError, ""))
return
}
if err := json.NewEncoder(w).Encode(&rr); err != nil {
failure.Fail(w, failure.New(errors.Wrap(err, "failed to encode reciept"), http.StatusInternalServerError, ""))
return
}
if issThreshold > 0 && int(rpt.Severity) >= issThreshold {
logger.Println("Creating github issue for crash report")
err = ghs.CreateGitHubIssue(github.IssueRequest{
Title: github.String(rr.GID + " " + rr.Key),
Body: github.String(fmt.Sprintf("---- Automated Crash Report ----\n\nKey: %v", rr.Key)),
Labels: &[]string{"Critical"},
})
if err != nil {
logger.Println("failed to create github issue (key=%v): %v", rpt.Key, err.Error())
} else {
logger.Println("Successfully created github issue")
}
}
w.WriteHeader(http.StatusCreated)
})
}
// remove a single file by its key
func DeleteReportHandler(s domain.Storer) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g, k := r.Context().Value(string(ReportGIDVar)).(string), r.Context().Value(string(ReportKeyVar)).(string) // if we fail to convert to string, we have a big problem -> let recoverer middleware deal
if err := s.RemoveEntry(domain.Receipt{Key: k, GID:g}); err != nil {
failure.Fail(w, err)
}
w.WriteHeader(http.StatusNoContent)
})
}