-
Notifications
You must be signed in to change notification settings - Fork 6
/
handlers.go
127 lines (104 loc) · 2.96 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
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
122
123
124
125
126
127
package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/Clever/wag/samples/gen-go-db/models"
"github.com/go-errors/errors"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/gorilla/mux"
"golang.org/x/xerrors"
"gopkg.in/Clever/kayvee-go.v6/logger"
)
var _ = strconv.ParseInt
var _ = strfmt.Default
var _ = swag.ConvertInt32
var _ = errors.New
var _ = mux.Vars
var _ = bytes.Compare
var _ = ioutil.ReadAll
var formats = strfmt.Default
var _ = formats
// convertBase64 takes in a string and returns a strfmt.Base64 if the input
// is valid base64 and an error otherwise.
func convertBase64(input string) (strfmt.Base64, error) {
temp, err := formats.Parse("byte", input)
if err != nil {
return strfmt.Base64{}, err
}
return *temp.(*strfmt.Base64), nil
}
// convertDateTime takes in a string and returns a strfmt.DateTime if the input
// is a valid DateTime and an error otherwise.
func convertDateTime(input string) (strfmt.DateTime, error) {
temp, err := formats.Parse("date-time", input)
if err != nil {
return strfmt.DateTime{}, err
}
return *temp.(*strfmt.DateTime), nil
}
// convertDate takes in a string and returns a strfmt.Date if the input
// is a valid Date and an error otherwise.
func convertDate(input string) (strfmt.Date, error) {
temp, err := formats.Parse("date", input)
if err != nil {
return strfmt.Date{}, err
}
return *temp.(*strfmt.Date), nil
}
func jsonMarshalNoError(i interface{}) string {
bytes, err := json.MarshalIndent(i, "", "\t")
if err != nil {
// This should never happen
return ""
}
return string(bytes)
}
// statusCodeForHealthCheck returns the status code corresponding to the returned
// object. It returns -1 if the type doesn't correspond to anything.
func statusCodeForHealthCheck(obj interface{}) int {
switch obj.(type) {
case *models.BadRequest:
return 400
case *models.InternalError:
return 500
case models.BadRequest:
return 400
case models.InternalError:
return 500
default:
return -1
}
}
func (h handler) HealthCheckHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
err := h.HealthCheck(ctx)
if err != nil {
logger.FromContext(ctx).AddContext("error", err.Error())
if btErr, ok := err.(*errors.Error); ok {
logger.FromContext(ctx).AddContext("stacktrace", string(btErr.Stack()))
} else if xerr, ok := err.(xerrors.Formatter); ok {
logger.FromContext(ctx).AddContext("frames", fmt.Sprintf("%+v", xerr))
}
statusCode := statusCodeForHealthCheck(err)
if statusCode == -1 {
err = models.InternalError{Message: err.Error()}
statusCode = 500
}
http.Error(w, jsonMarshalNoError(err), statusCode)
return
}
w.WriteHeader(200)
w.Write([]byte(""))
}
// newHealthCheckInput takes in an http.Request an returns the input struct.
func newHealthCheckInput(r *http.Request) (*models.HealthCheckInput, error) {
var input models.HealthCheckInput
var err error
_ = err
return &input, nil
}