-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
42 lines (37 loc) · 1.03 KB
/
handlers.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
)
func DrmShowsIndexAction(w http.ResponseWriter, r *http.Request) {
var errResp ErrorResponse
errResp.ErrorMesg = "Could not decode request: JSON parsing failed"
var clireq ClientRequest
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Println("Error reading response : ")
return
}
if err := r.Body.Close(); err != nil {
log.Println("Error in request body : ")
return
}
if err := json.Unmarshal(body, &clireq); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(400) // Bad request
if err := json.NewEncoder(w).Encode(errResp); err != nil {
log.Println("Error in sending ErrResp : ")
}
return
}
sResp := FindDrmShows(clireq.Payload)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(sResp); err != nil {
log.Println("Error encoding response JSON : ", sResp)
}
return
}