-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
162 lines (140 loc) · 4.21 KB
/
server.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
// Copyright (C) 2022, Chain4Travel AG. All rights reserved.
//
// This file is a derived work, based on ava-labs code whose
// original notices appear below.
//
// It is distributed under the same license conditions as the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********************************************************
// (c) 2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package api
import (
"context"
"net/http"
"time"
"go.uber.org/zap"
"github.com/lasthyphen/dijetsnodego/ids"
"github.com/lasthyphen/mages/caching"
"github.com/lasthyphen/mages/cfg"
"github.com/lasthyphen/mages/models"
"github.com/lasthyphen/mages/services"
"github.com/lasthyphen/mages/services/indexes/djtx"
"github.com/lasthyphen/mages/servicesctrl"
"github.com/lasthyphen/mages/stream/consumers"
"github.com/gocraft/web"
)
// Server is an HTTP server configured with various magellan APIs
type Server struct {
sc *servicesctrl.Control
server *http.Server
}
// NewServer creates a new *Server based on the given config
func NewServer(sc *servicesctrl.Control, conf cfg.Config) (*Server, error) {
router, err := newRouter(sc, conf)
if err != nil {
return nil, err
}
// Set address prefix to use the configured network
models.SetBech32HRP(conf.NetworkID)
return &Server{
sc: sc,
server: &http.Server{
Addr: conf.ListenAddr,
ReadTimeout: 5 * time.Second,
WriteTimeout: cfg.HTTPWriteTimeout,
IdleTimeout: 15 * time.Second,
Handler: router,
ReadHeaderTimeout: 5 * time.Second,
},
}, err
}
// Listen begins listening for new socket connections and blocks until closed
func (s *Server) Listen() error {
s.sc.Log.Info("server listening",
zap.String("addr", s.server.Addr),
)
return s.server.ListenAndServe()
}
// Close shuts the server down
func (s *Server) Close() error {
s.sc.Log.Info("Server shutting down")
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
return s.server.Shutdown(ctx)
}
func newRouter(sc *servicesctrl.Control, conf cfg.Config) (*web.Router, error) {
sc.Log.Info("creating new router",
zap.Stringer("chainID", sc.GenesisContainer.XChainID),
)
var xChainID, cChainID ids.ID
for key, chain := range conf.Chains {
switch chain.VMType {
case models.CVMName:
cChainID, _ = ids.FromString(key)
case models.AVMName:
xChainID, _ = ids.FromString(key)
}
}
indexBytes, err := newIndexResponse(
conf.NetworkID,
xChainID,
cChainID,
sc.GenesisContainer.DjtxAssetID,
)
if err != nil {
return nil, err
}
legacyIndexResponse, err := newLegacyIndexResponse(
conf.NetworkID,
sc.GenesisContainer.XChainID,
sc.GenesisContainer.DjtxAssetID,
)
if err != nil {
return nil, err
}
// Create connections and readers
connections, err := sc.DatabaseRO()
if err != nil {
return nil, err
}
cache := caching.NewCache()
delayCache := caching.NewDelayCache(cache)
consumersmap := make(map[string]services.Consumer)
for chid, chain := range conf.Chains {
consumer, err := consumers.IndexerConsumer(conf.NetworkID, chain.VMType, chid, &conf)
if err != nil {
return nil, err
}
consumersmap[chid] = consumer
}
djtxReader, err := djtx.NewReader(conf.NetworkID, connections, consumersmap, sc)
if err != nil {
return nil, err
}
ctx := Context{sc: sc}
// Build router
router := web.New(ctx).
Middleware(newContextSetter(sc, conf.NetworkID, connections, delayCache)).
Middleware((*Context).setHeaders).
Get("/", func(c *Context, resp web.ResponseWriter, _ *web.Request) {
if _, err := resp.Write(indexBytes); err != nil {
sc.Log.Warn("response write failed",
zap.Error(err),
)
}
}).
NotFound((*Context).notFoundHandler).
Middleware(func(c *Context, w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
c.djtxReader = djtxReader
c.djtxAssetID = sc.GenesisContainer.DjtxAssetID
next(w, r)
})
AddV2Routes(&ctx, router, "/v2", indexBytes, nil)
// Legacy routes.
AddV2Routes(&ctx, router, "/x", legacyIndexResponse, &sc.GenesisContainer.XChainID)
AddV2Routes(&ctx, router, "/X", legacyIndexResponse, &sc.GenesisContainer.XChainID)
return router, nil
}