forked from square/keysync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
168 lines (144 loc) · 4.98 KB
/
api.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
// Copyright 2016 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keysync
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"time"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/square/go-sq-metrics"
)
var (
httpPost = []string{"POST"}
httpGet = []string{"HEAD", "GET"}
)
const (
pollIntervalFailureThresholdMultiplier = 10
)
// APIServer holds state needed for responding to HTTP api requests
type APIServer struct {
syncer *Syncer
logger *logrus.Entry
}
// StatusResponse from API endpoints
type StatusResponse struct {
Ok bool `json:"ok"`
Message string `json:"message"`
}
func writeSuccess(w http.ResponseWriter) {
resp := &StatusResponse{Ok: true}
out, _ := json.Marshal(resp)
w.WriteHeader(http.StatusOK)
w.Write(out)
}
func writeError(w http.ResponseWriter, status int, err error) {
resp := &StatusResponse{Ok: false, Message: err.Error()}
out, _ := json.Marshal(resp)
w.WriteHeader(status)
w.Write(out)
}
func (a *APIServer) syncAll(w http.ResponseWriter, r *http.Request) {
a.logger.Info("Syncing all from API")
errors := a.syncer.RunOnce()
if len(errors) != 0 {
err := fmt.Errorf("Errors: %v", errors)
a.logger.WithError(err).Warn("Error syncing")
writeError(w, http.StatusInternalServerError, err)
return
}
writeSuccess(w)
}
func (a *APIServer) syncOne(w http.ResponseWriter, r *http.Request) {
client, hasClient := mux.Vars(r)["client"]
if !hasClient || client == "" {
// Should be unreachable
a.logger.Info("Invalid request: No client provided.")
writeError(w, http.StatusBadRequest, errors.New("invalid request: no client provided"))
return
}
logger := a.logger.WithField("client", client)
logger.Info("Syncing one")
a.syncer.syncMutex.Lock()
defer a.syncer.syncMutex.Unlock()
err := a.syncer.LoadClients()
if err != nil {
logger.WithError(err).Warn("Failed while loading clients")
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed while loading clients: %s", err))
return
}
syncerEntry, ok := a.syncer.clients[client]
if !ok {
logger.Infof("Unknown client: %s", client)
writeError(w, http.StatusNotFound, fmt.Errorf("unknown client: %s", client))
return
}
err = syncerEntry.Sync()
if err != nil {
logger.WithError(err).Warnf("Error syncing %s", client)
writeError(w, http.StatusInternalServerError, fmt.Errorf("error syncing %s: %s", client, err))
return
}
writeSuccess(w)
}
func (a *APIServer) status(w http.ResponseWriter, r *http.Request) {
lastSuccess, ok := a.syncer.timeSinceLastSuccess()
if !ok {
writeError(w, http.StatusServiceUnavailable, errors.New("initial sync has not yet completed"))
return
}
failureThreshold := a.syncer.pollInterval * pollIntervalFailureThresholdMultiplier
if lastSuccess > failureThreshold {
err := a.syncer.mostRecentError()
writeError(w, http.StatusServiceUnavailable, fmt.Errorf("haven't synced in over %d seconds (most recent err: %s)", int64(lastSuccess/time.Second), err))
return
}
writeSuccess(w)
}
// handle wraps the HandlerFunc with logging, and registers it in the given router.
func handle(router *mux.Router, path string, methods []string, fn http.HandlerFunc, logger *logrus.Entry) {
wrapped := func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
fn(w, r)
logger.WithFields(logrus.Fields{
"url": r.URL,
"duration": time.Since(start),
}).Info("Request")
}
router.HandleFunc(path, wrapped).Methods(methods...)
}
// NewAPIServer is the constructor for an APIServer
func NewAPIServer(syncer *Syncer, port uint16, baseLogger *logrus.Entry, metrics *sqmetrics.SquareMetrics) {
logger := baseLogger.WithField("logger", "api_server")
apiServer := APIServer{syncer: syncer, logger: logger}
router := mux.NewRouter()
// Debug endpoints
handle(router, "/debug/pprof", httpGet, pprof.Index, logger)
handle(router, "/debug/pprof/cmdline", httpGet, pprof.Cmdline, logger)
handle(router, "/debug/pprof/profile", httpGet, pprof.Profile, logger)
handle(router, "/debug/pprof/symbol", httpGet, pprof.Symbol, logger)
// Sync endpoints
handle(router, "/sync", httpPost, apiServer.syncAll, logger)
handle(router, "/sync/{client}", httpPost, apiServer.syncOne, logger)
// Status and metrics endpoints
router.HandleFunc("/status", apiServer.status).Methods(httpGet...)
handle(router, "/metrics", httpGet, metrics.ServeHTTP, logger)
go func() {
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", port), router)
logger.WithError(err).WithField("port", port).Error("Listen and Serve")
}()
}