-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
59 lines (52 loc) · 1.22 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
package main
import (
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
)
func healthzHandler(w http.ResponseWriter, r *http.Request) {
// Implement your health check logic here
// Return a 200 HTTP status code if the application is healthy
w.WriteHeader(http.StatusOK)
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
// Increment the request counter
vaultProxyRequests.Inc()
path := r.URL.Path
// Read the request body into a buffer
// it could be closed before we want upstream it
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error().
Err(err).
Str("path", path).
Msg("Error reading body")
}
token, err := extractToken(r, bodyBytes)
if err != nil {
log.Error().Err(err).Msg("Error extraction token")
} else {
secret, err := getSecret(r, token)
if err == nil {
log.Info().
Str("path", path).
Msg("Served from cache")
writeSecret(w, secret)
return
} else {
log.Error().
Err(err).
Str("path", path).
Msg("Cache error")
}
}
secretEntry, err := getFromUpstream(r, bodyBytes)
if err != nil {
log.Error().
Err(err).
Str("path", path).
Msg("Fetch upstream")
}
cacheSecret(secretEntry, token, path)
writeSecret(w, secretEntry)
}