-
Notifications
You must be signed in to change notification settings - Fork 247
/
api.go
104 lines (91 loc) · 2.96 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
// Copyright (c) 2018 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package api
import (
"net/http"
"net/http/pprof"
"strings"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/vechain/thor/api/accounts"
"github.com/vechain/thor/api/blocks"
"github.com/vechain/thor/api/debug"
"github.com/vechain/thor/api/doc"
"github.com/vechain/thor/api/events"
"github.com/vechain/thor/api/node"
"github.com/vechain/thor/api/subscriptions"
"github.com/vechain/thor/api/transactions"
"github.com/vechain/thor/api/transfers"
"github.com/vechain/thor/chain"
"github.com/vechain/thor/logdb"
"github.com/vechain/thor/state"
"github.com/vechain/thor/thor"
"github.com/vechain/thor/txpool"
)
//New return api router
func New(
repo *chain.Repository,
stater *state.Stater,
txPool *txpool.TxPool,
logDB *logdb.LogDB,
nw node.Network,
allowedOrigins string,
backtraceLimit uint32,
callGasLimit uint64,
pprofOn bool,
skipLogs bool,
forkConfig thor.ForkConfig,
) (http.HandlerFunc, func()) {
origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
for i, o := range origins {
origins[i] = strings.ToLower(strings.TrimSpace(o))
}
router := mux.NewRouter()
// to serve api doc and swagger-ui
router.PathPrefix("/doc").Handler(
http.StripPrefix("/doc/", http.FileServer(
&assetfs.AssetFS{
Asset: doc.Asset,
AssetDir: doc.AssetDir,
AssetInfo: doc.AssetInfo})))
// redirect swagger-ui
router.Path("/").HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "doc/swagger-ui/", http.StatusTemporaryRedirect)
})
accounts.New(repo, stater, callGasLimit, forkConfig).
Mount(router, "/accounts")
if !skipLogs {
events.New(repo, logDB).
Mount(router, "/logs/event")
transfers.New(repo, logDB).
Mount(router, "/logs/transfer")
}
blocks.New(repo).
Mount(router, "/blocks")
transactions.New(repo, txPool).
Mount(router, "/transactions")
debug.New(repo, stater, forkConfig).
Mount(router, "/debug")
node.New(nw).
Mount(router, "/node")
subs := subscriptions.New(repo, origins, backtraceLimit)
subs.Mount(router, "/subscriptions")
if pprofOn {
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
}
handler := handlers.CompressHandler(router)
handler = handlers.CORS(
handlers.AllowedOrigins(origins),
handlers.AllowedHeaders([]string{"content-type", "x-genesis-id"}),
handlers.ExposedHeaders([]string{"x-genesis-id", "x-thorest-ver"}),
)(handler)
return handler.ServeHTTP,
subs.Close // subscriptions handles hijacked conns, which need to be closed
}