-
Notifications
You must be signed in to change notification settings - Fork 128
/
explorermiddleware.go
95 lines (83 loc) · 2.35 KB
/
explorermiddleware.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
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
package explorer
import (
"context"
"net/http"
"strconv"
"github.com/go-chi/chi"
)
type contextKey int
const (
ctxSearch contextKey = iota
ctxBlockIndex
ctxBlockHash
ctxTxHash
ctxAddress
)
func (exp *explorerUI) BlockHashPathOrIndexCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
height, err := strconv.ParseInt(chi.URLParam(r, "blockhash"), 10, 0)
var hash string
if err != nil {
hash = chi.URLParam(r, "blockhash")
height, err = exp.blockData.GetBlockHeight(hash)
if err != nil {
log.Errorf("GetBlockHeight(%s) failed: %v", hash, err)
exp.ErrorPage(w, "Something went wrong...", "could not find that block", true)
return
}
} else {
hash, err = exp.blockData.GetBlockHash(height)
if err != nil {
log.Errorf("GetBlockHeight(%d) failed: %v", height, err)
exp.ErrorPage(w, "Something went wrong...", "could not find that block", true)
return
}
}
ctx := context.WithValue(r.Context(), ctxBlockHash, hash)
ctx = context.WithValue(ctx, ctxBlockIndex, height)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getBlockHashCtx(r *http.Request) string {
hash, ok := r.Context().Value(ctxBlockHash).(string)
if !ok {
log.Trace("Block Hash not set")
return ""
}
return hash
}
func getBlockHeightCtx(r *http.Request) int64 {
idxI, ok := r.Context().Value(ctxBlockIndex).(int)
idx := int64(idxI)
if !ok {
log.Trace("Block Height not set")
return -1
}
return idx
}
func getTxIDCtx(r *http.Request) string {
hash, ok := r.Context().Value(ctxTxHash).(string)
if !ok {
log.Trace("Txid not set")
return ""
}
return hash
}
// TransactionHashCtx embeds "txid" into the request context
func TransactionHashCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
txid := chi.URLParam(r, "txid")
ctx := context.WithValue(r.Context(), ctxTxHash, txid)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// AddressPathCtx embeds "address" into the request context
func AddressPathCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
address := chi.URLParam(r, "address")
ctx := context.WithValue(r.Context(), ctxAddress, address)
next.ServeHTTP(w, r.WithContext(ctx))
})
}