This repository has been archived by the owner on May 2, 2018. It is now read-only.
/
handlers.go
178 lines (152 loc) · 5.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package brands
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"github.com/Financial-Times/go-fthealth/v1a"
"github.com/Financial-Times/service-status-go/gtg"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
// BrandHandler - struct for the handlers
type BrandHandler struct {
service BrandService
}
// NewBrandHandler - Create a new BrandHandler
func NewBrandHandler(service BrandService) BrandHandler {
return BrandHandler{service}
}
// GetBrands - Return a JSON encoded list of all brands
func (h *BrandHandler) GetBrands(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json")
if !h.service.isInitialised() || !h.service.isDataLoaded() {
writeStatusServiceUnavailable(writer)
return
}
if c, _ := h.service.getCount(); c == 0 {
writeJSONMessageWithStatus(writer, "Brands not found", http.StatusNotFound)
return
}
pv, err := h.service.getBrands()
if err != nil {
writeJSONMessageWithStatus(writer, err.Error(), http.StatusInternalServerError)
return
}
defer pv.Close()
writer.WriteHeader(http.StatusOK)
io.Copy(writer, &pv)
}
// GetBrandUUIDs - Get a list of JSON objects (not a JSON list) giving each id.
func (h *BrandHandler) GetBrandUUIDs(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json")
if !h.service.isInitialised() || !h.service.isDataLoaded() {
writeStatusServiceUnavailable(writer)
return
}
if c, _ := h.service.getCount(); c == 0 {
writeJSONMessageWithStatus(writer, "Brands not found", http.StatusNotFound)
return
}
pv, err := h.service.getBrandUUIDs()
if err != nil {
writeJSONMessageWithStatus(writer, err.Error(), http.StatusInternalServerError)
return
}
defer pv.Close()
writer.WriteHeader(http.StatusOK)
io.Copy(writer, &pv)
}
// GetCount - Get a count of the number of available brands
func (h *BrandHandler) GetCount(writer http.ResponseWriter, req *http.Request) {
if !h.service.isInitialised() || !h.service.isDataLoaded() {
writer.Header().Add("Content-Type", "application/json")
writeStatusServiceUnavailable(writer)
return
}
count, err := h.service.getCount()
if err != nil {
writer.Header().Add("Content-Type", "application/json")
writeJSONMessageWithStatus(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.Write([]byte(strconv.Itoa(count)))
}
// HealthCheck - Return FT standard healthcheck
func (h *BrandHandler) HealthCheck() v1a.Check {
return v1a.Check{
BusinessImpact: "Unable to respond to requests",
Name: "Check service has finished initilising.",
PanicGuide: "https://sites.google.com/a/ft.com/ft-technology-service-transition/home/run-book-library/v1-brands-transformer",
Severity: 1,
TechnicalSummary: "Cannot serve any content as data not loaded.",
Checker: func() (string, error) {
if h.service.isInitialised() {
return "Service is up and running", nil
}
return "Error as service initilising", errors.New("Service is initilising.")
},
}
}
// G2GCheck - Return FT standard good-to-go check
func (h *BrandHandler) G2GCheck() gtg.Status {
if h.service.isInitialised() && h.service.isDataLoaded() {
return gtg.Status{GoodToGo: true}
}
return gtg.Status{GoodToGo: false}
}
// GetBrandByUUID - Return the JSON for a single brand
func (h *BrandHandler) GetBrandByUUID(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json")
if !h.service.isInitialised() || !h.service.isDataLoaded() {
writeStatusServiceUnavailable(writer)
return
}
vars := mux.Vars(req)
uuid := vars["uuid"]
obj, found, err := h.service.getBrandByUUID(uuid)
if err != nil {
writeJSONMessageWithStatus(writer, err.Error(), http.StatusInternalServerError)
}
writeJSONResponse(obj, found, writer)
}
// Reload - Reload the cache with fresh information
func (h *BrandHandler) Reload(writer http.ResponseWriter, req *http.Request) {
go func() {
if err := h.service.reloadDB(); err != nil {
log.Errorf("ERROR reloading db: %v", err.Error())
}
}()
writeJSONMessageWithStatus(writer, "Reloading brands", http.StatusAccepted)
}
func writeJSONResponse(obj interface{}, found bool, writer http.ResponseWriter) {
if !found {
writeJSONMessageWithStatus(writer, "Brand not found", http.StatusNotFound)
return
}
enc := json.NewEncoder(writer)
if err := enc.Encode(obj); err != nil {
log.Errorf("Error on json encoding=%v", err)
writeJSONMessageWithStatus(writer, err.Error(), http.StatusInternalServerError)
return
}
}
func writeJSONMessageWithStatus(w http.ResponseWriter, msg string, statusCode int) {
w.WriteHeader(statusCode)
fmt.Fprintln(w, fmt.Sprintf("{\"message\": \"%s\"}", msg))
}
func writeStatusServiceUnavailable(w http.ResponseWriter) {
writeJSONMessageWithStatus(w, "Service Unavailable", http.StatusServiceUnavailable)
}
//OnlyPostAllowed - Used to tell the user the METHOD type is not POST.
func (h *BrandHandler) OnlyPostAllowed(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Allow", "POST")
writer.WriteHeader(http.StatusMethodNotAllowed)
}
//OnlyGetAllowed - Used to tell the user the METHOD type is not GET.
func (h *BrandHandler) OnlyGetAllowed(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Allow", "GET")
writer.WriteHeader(http.StatusMethodNotAllowed)
}