-
Notifications
You must be signed in to change notification settings - Fork 2
/
getting.go
65 lines (56 loc) · 1.66 KB
/
getting.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
package actions
import (
"fmt"
"html"
"net/http"
"strconv"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/ottogroup/penelope/pkg/builder"
"github.com/ottogroup/penelope/pkg/requestobjects"
"go.opencensus.io/trace"
)
type GettingBackupHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewGettingBackupHandler(processorBuilder *builder.ProcessorBuilder) *GettingBackupHandler {
return &GettingBackupHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle Getting operation
func (dl *GettingBackupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "GettingBackupHandler.ServeHTTP")
defer span.End()
backupID, ok := mux.Vars(r)["backup_id"]
if !ok {
msg := "Bad request missing parameter: backup_id"
prepareResponse(w, msg, msg, http.StatusBadRequest)
return
}
request := requestobjects.GetRequest{}
request.BackupID = backupID
q := r.URL.Query()
if q.Get("size") != "" {
i, err := strconv.Atoi(q.Get("size"))
if err != nil {
BadRequestResponse(w, r)
return
}
request.Page.Size = i
}
if q.Get("page") != "" {
i, err := strconv.Atoi(q.Get("page"))
if err != nil {
BadRequestResponse(w, r)
return
}
request.Page.Number = i
}
handleRequestByProcessor(ctx, w, r, request, http.StatusOK, dl.processorBuilder.ProcessorForGetting)
}
func BadRequestResponse(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
if _, err := fmt.Fprintf(w, "Unkown api endpoint %s", r.URL.Path); err != nil {
escapedPath := html.EscapeString(r.URL.Path)
glog.Warningf("Error writing response for %s: %s", escapedPath, err)
}
}