This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
251 lines (204 loc) · 6.77 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
_ "embed"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"github.com/ipfs/bifrost-gateway/lib"
"go.opentelemetry.io/contrib/propagators/autoprop"
"go.opentelemetry.io/otel"
blockstore "github.com/ipfs/boxo/blockstore"
golog "github.com/ipfs/go-log/v2"
"github.com/spf13/cobra"
)
var goLog = golog.Logger("bifrost-gateway")
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
const (
EnvKuboRPC = "KUBO_RPC_URL"
EnvIPNSRecordGateway = "IPNS_RECORD_GATEWAY_URL"
EnvBlockCacheSize = "BLOCK_CACHE_SIZE"
EnvGraphBackend = "GRAPH_BACKEND"
RequestIDHeader = "X-Bfid"
)
func init() {
rootCmd.Flags().Int("gateway-port", 8081, "gateway port")
rootCmd.Flags().Int("metrics-port", 8041, "metrics port")
}
var rootCmd = &cobra.Command{
Use: name,
Version: version,
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
Short: "IPFS Gateway implementation for https://github.com/protocol/bifrost-infra",
Long: `bifrost-gateway provides HTTP Gateway backed by a remote blockstore.
See documentation at: https://github.com/ipfs/bifrost-gateway/#readme`,
RunE: func(cmd *cobra.Command, args []string) error {
// Get flags.
gatewayPort, _ := cmd.Flags().GetInt("gateway-port")
metricsPort, _ := cmd.Flags().GetInt("metrics-port")
// Get env variables.
saturnOrchestrator := getEnv(EnvSaturnOrchestrator, "")
proxyGateway := getEnvs(EnvProxyGateway, "")
kuboRPC := getEnvs(EnvKuboRPC, "")
blockCacheSize, err := getEnvInt(EnvBlockCacheSize, lib.DefaultCacheBlockStoreSize)
if err != nil {
return err
}
useGraphBackend, err := getEnvBool(EnvGraphBackend, false)
if err != nil {
return err
}
log.Printf("Starting %s %s", name, version)
registerVersionMetric(version)
tp, shutdown, err := newTracerProvider(cmd.Context())
if err != nil {
return err
}
defer func() {
_ = shutdown(cmd.Context())
}()
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(autoprop.NewTextMapPropagator())
cdns := newCachedDNS(dnsCacheRefreshInterval)
defer cdns.Close()
var bs blockstore.Blockstore
if saturnOrchestrator != "" {
saturnLogger := getEnv(EnvSaturnLogger, DefaultSaturnLogger)
log.Printf("Saturn backend (%s) at %s", EnvSaturnOrchestrator, saturnOrchestrator)
log.Printf("Saturn logger (%s) at %s", EnvSaturnLogger, saturnLogger)
if os.Getenv(EnvSaturnLoggerSecret) == "" {
log.Printf("")
log.Printf(" WARNING: %s is not set", EnvSaturnLoggerSecret)
log.Printf("")
}
bs, err = newCabooseBlockStore(saturnOrchestrator, saturnLogger, cdns)
if err != nil {
return err
}
} else if len(proxyGateway) != 0 {
log.Printf(`
⚠️ PROJECT NO LONGER MAINTAINED
The bifrost-gateway project is no longer maintained.
You can continue using it, but it won't receive any security updates or
fixes. Consider forking or migrating to Rainbow.
ℹ️ MIGRATING TO RAINBOW
The PROXY_GATEWAY_URL functionality is backported to Rainbow
https://github.com/ipfs/rainbow/
To use Rainbow with a remote block or CAR backend, configure it with:
RAINBOW_REMOTE_BACKENDS=<gwurl>
RAINBOW_REMOTE_BACKENDS_MODE=block|car
For details, visit:
at https://github.com/ipfs/rainbow/blob/main/docs/environment-variables.md
TLDR:
If you currently use:
PROXY_GATEWAY_URL=http://127.0.0.1:8080 \
GRAPH_BACKEND=false \
./bifrost-gateway
It can be replaced with:
RAINBOW_REMOTE_BACKENDS=http://127.0.0.1:8080 \
RAINBOW_REMOTE_BACKENDS_MODE=block \
./rainbow
Rainbow docker images:
https://github.com/ipfs/rainbow#docker
`)
log.Printf("Proxy backend (PROXY_GATEWAY_URL) at %s", strings.Join(proxyGateway, " "))
bs = newProxyBlockStore(proxyGateway, cdns)
} else {
log.Fatalf("Unable to start. bifrost-gateway requires either PROXY_GATEWAY_URL or STRN_ORCHESTRATOR_URL to be set.\n\nRead docs at https://github.com/ipfs/bifrost-gateway/blob/main/docs/environment-variables.md\n\n")
}
// Prefer IPNS_RECORD_GATEWAY_URL when an explicit URL for IPNS routing is set
ipnsRecordGateway := getEnvs(EnvIPNSRecordGateway, "")
if len(ipnsRecordGateway) == 0 {
// Fallback to PROXY_GATEWAY_URL, assuming it is modern
// enough to support application/vnd.ipfs.ipns-record responses
ipnsRecordGateway = proxyGateway
}
gatewaySrv, err := makeGatewayHandler(bs, kuboRPC, ipnsRecordGateway, gatewayPort, blockCacheSize, cdns, useGraphBackend)
if err != nil {
return err
}
metricsSrv, err := makeMetricsAndDebuggingHandler(metricsPort)
if err != nil {
return err
}
quit := make(chan os.Signal, 1)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
// log important configuration flags
log.Printf("%s: %d", EnvBlockCacheSize, blockCacheSize)
log.Printf("%s: %t", EnvGraphBackend, useGraphBackend)
if len(kuboRPC) != 0 {
log.Printf("Legacy RPC at /api/v0 (%s) provided by %s", EnvKuboRPC, strings.Join(kuboRPC, " "))
}
log.Printf("Path gateway listening on http://127.0.0.1:%d", gatewayPort)
log.Printf(" Smoke test (JPG): http://127.0.0.1:%d/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", gatewayPort)
log.Printf("Subdomain gateway configured on dweb.link and http://localhost:%d", gatewayPort)
log.Printf(" Smoke test (Subdomain+DNSLink+UnixFS+HAMT): http://localhost:%d/ipns/en.wikipedia-on-ipfs.org/wiki/", gatewayPort)
err := gatewaySrv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("Failed to start gateway: %s", err)
quit <- os.Interrupt
}
}()
go func() {
defer wg.Done()
log.Printf("Metrics exposed at http://127.0.0.1:%d/debug/metrics/prometheus", metricsPort)
err := metricsSrv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("Failed to start metrics: %s", err)
quit <- os.Interrupt
}
}()
signal.Notify(quit, os.Interrupt)
<-quit
log.Printf("Closing servers...")
go gatewaySrv.Close()
go metricsSrv.Close()
wg.Wait()
return nil
},
}
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
func getEnvs(key, defaultValue string) []string {
value := os.Getenv(key)
if value == "" {
if defaultValue == "" {
return []string{}
}
value = defaultValue
}
value = strings.TrimSpace(value)
return strings.Split(value, ",")
}
func getEnvInt(key string, defaultValue int) (int, error) {
value := os.Getenv(key)
if value == "" {
return defaultValue, nil
}
return strconv.Atoi(value)
}
func getEnvBool(key string, defaultValue bool) (bool, error) {
value := os.Getenv(key)
if value == "" {
return defaultValue, nil
}
return strconv.ParseBool(value)
}