-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
http.go
48 lines (37 loc) · 1.1 KB
/
http.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
package distributor
import (
"encoding/json"
"net/http"
"github.com/weaveworks/common/httpgrpc"
"github.com/cortexproject/cortex/pkg/util"
"github.com/grafana/loki/pkg/logproto"
)
var contentType = http.CanonicalHeaderKey("Content-Type")
const applicationJSON = "application/json"
// PushHandler reads a snappy-compressed proto from the HTTP body.
func (d *Distributor) PushHandler(w http.ResponseWriter, r *http.Request) {
var req logproto.PushRequest
switch r.Header.Get(contentType) {
case applicationJSON:
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
default:
if _, err := util.ParseProtoReader(r.Context(), r.Body, &req, util.RawSnappy); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
_, err := d.Push(r.Context(), &req)
if err == nil {
w.WriteHeader(http.StatusNoContent)
return
}
resp, ok := httpgrpc.HTTPResponseFromError(err)
if ok {
http.Error(w, string(resp.Body), int(resp.Code))
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}