-
Notifications
You must be signed in to change notification settings - Fork 1
/
apimiddleware.go
120 lines (107 loc) · 3.38 KB
/
apimiddleware.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
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/pressly/chi"
"github.com/pressly/chi/docgen"
//"github.com/decred/dcrrpcclient"
)
// Middlewares
type contextKey int
const (
ctxAPIDocs contextKey = iota
ctxAPIStatus
ctxBlockIndex0
ctxBlockIndex
ctxN
)
func (c *appContext) StatusCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set API status context
ctx := context.WithValue(r.Context(), ctxAPIStatus, &c.Status)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// SomeCtx sets X in the http.Request context used by all handlers with certain
// endpoints
// func SomeCtx(next http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ctx := context.WithValue(r.Context(), ctxX, "A")
// next.ServeHTTP(w, r.WithContext(ctx))
// })
// }
func BlockIndexPathCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pathIdxStr := chi.URLParam(r, "idx")
idx, err := strconv.Atoi(pathIdxStr)
if err != nil {
apiLog.Infof("No/invalid idx value (int64): %v", err)
http.NotFound(w, r)
//http.Error(w, http.StatusText(404), 404)
return
}
ctx := context.WithValue(r.Context(), ctxBlockIndex, idx)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func BlockIndex0PathCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pathIdxStr := chi.URLParam(r, "idx0")
idx, err := strconv.Atoi(pathIdxStr)
if err != nil {
apiLog.Infof("No/invalid idx0 value (int64): %v", err)
http.NotFound(w, r)
//http.Error(w, http.StatusText(404), 404)
return
}
ctx := context.WithValue(r.Context(), ctxBlockIndex0, idx)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func NPathCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pathNStr := chi.URLParam(r, "N")
N, err := strconv.Atoi(pathNStr)
if err != nil {
apiLog.Infof("No/invalid numeric value (uint64): %v", err)
http.NotFound(w, r)
//http.Error(w, http.StatusText(404), 404)
return
}
ctx := context.WithValue(r.Context(), ctxN, N)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (c *appContext) BlockIndexLatestCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
idx := -1
if c.BlockData != nil && c.BlockData.GetHeight() >= 0 {
idx = c.BlockData.GetHeight()
}
ctx := context.WithValue(r.Context(), ctxBlockIndex, idx)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// apiDocs generates a middleware with a "docs" in the context containing a
// map of the routers handlers, etc.
func apiDocs(mux *chi.Mux) func(next http.Handler) http.Handler {
var buf bytes.Buffer
json.Indent(&buf, []byte(docgen.JSONRoutesDoc(mux)), "", "\t")
docs := buf.String()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), ctxAPIDocs, docs)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// APIDirectory is the actual handler used with apiDocs
// (e.g. mux.With(apiDocs(mux)).HandleFunc("/help", APIDirectory))
func APIDirectory(w http.ResponseWriter, r *http.Request) {
docs := r.Context().Value(ctxAPIDocs).(string)
io.WriteString(w, docs)
}