feat: add $IPFS_PATH/gateway file
The file contains the gateway your node is hosting in the http://<host>:<port> RFC 3986 format. Structurally it works exactly the same as the API file.
Mark Gaiser committed
Aug 8, 2022 at 13:50 UTC
d419c0e95b830577431efd248c3fa32a140dedf7
5 files changed
+64
cmd/ipfs/daemon.go
+6
@@ -806,6 +806,12 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
806
return nil, fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err)
807
}
808
809
+ if len(listeners) > 0 {
810
+ if err := node.Repo.SetGatewayAddr(listeners[0].Addr()); err != nil {
811
+ return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
812
+ }
813
+ }
814
+
815
errc := make(chan error)
816
var wg sync.WaitGroup
817
for _, lis := range listeners {
repo/fsrepo/fsrepo.go
+45
@@ -5,6 +5,7 @@ import (
5
"errors"
6
"fmt"
7
"io"
8
+ "net"
9
"os"
10
"path/filepath"
11
"strings"
@@ -63,6 +64,7 @@ func (err NoRepoError) Error() string {
64
}
65
66
const apiFile = "api"
67
+const gatewayFile = "gateway"
68
const swarmKeyFile = "swarm.key"
69
70
const specFn = "datastore_spec"
@@ -387,6 +389,44 @@ func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error {
389
return err
390
}
391
392
+// SetGatewayAddr writes the Gateway Addr to the /gateway file.
393
+func (r *FSRepo) SetGatewayAddr(addr net.Addr) error {
394
+ // Create a temp file to write the address, so that we don't leave empty file when the
395
+ // program crashes after creating the file.
396
+ tmpPath := filepath.Join(r.path, "."+gatewayFile+".tmp")
397
+ f, err := os.Create(tmpPath)
398
+ if err != nil {
399
+ return err
400
+ }
401
+ var good bool
402
+ // Silently remove as worst last case with defers.
403
+ defer func() {
404
+ if !good {
405
+ os.Remove(tmpPath)
406
+ }
407
+ }()
408
+ defer f.Close()
409
+
410
+ if _, err := fmt.Fprintf(f, "http://%s", addr.String()); err != nil {
411
+ return err
412
+ }
413
+ if err := f.Close(); err != nil {
414
+ return err
415
+ }
416
+
417
+ // Atomically rename the temp file to the correct file name.
418
+ err = os.Rename(tmpPath, filepath.Join(r.path, gatewayFile))
419
+ good = err == nil
420
+ if good {
421
+ return nil
422
+ }
423
+ // Remove the temp file when rename return error
424
+ if err1 := os.Remove(tmpPath); err1 != nil {
425
+ return fmt.Errorf("File Rename error: %w, File remove error: %s", err, err1.Error())
426
+ }
427
+ return err
428
+}
429
+
430
// openConfig returns an error if the config file is not present.
431
func (r *FSRepo) openConfig() error {
432
conf, err := serialize.Load(r.configFilePath)
@@ -474,6 +514,11 @@ func (r *FSRepo) Close() error {
514
log.Warn("error removing api file: ", err)
515
}
516
517
+ err = os.Remove(filepath.Join(r.path, gatewayFile))
518
+ if err != nil && !os.IsNotExist(err) {
519
+ log.Warn("error removing gateway file: ", err)
520
+ }
521
+
522
if err := r.ds.Close(); err != nil {
523
return err
524
}
repo/mock.go
+3
@@ -3,6 +3,7 @@ package repo
3
import (
4
"context"
5
"errors"
6
+ "net"
7
8
filestore "github.com/ipfs/go-filestore"
9
keystore "github.com/ipfs/go-ipfs-keystore"
@@ -50,6 +51,8 @@ func (m *Mock) Close() error { return m.D.Close() }
51
52
func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }
53
54
+func (m *Mock) SetGatewayAddr(addr net.Addr) error { return errTODO }
55
+
56
func (m *Mock) Keystore() keystore.Keystore { return m.K }
57
58
func (m *Mock) SwarmKey() ([]byte, error) {
repo/repo.go
+4
@@ -4,6 +4,7 @@ import (
4
"context"
5
"errors"
6
"io"
7
+ "net"
8
9
filestore "github.com/ipfs/go-filestore"
10
keystore "github.com/ipfs/go-ipfs-keystore"
@@ -51,6 +52,9 @@ type Repo interface {
52
// SetAPIAddr sets the API address in the repo.
53
SetAPIAddr(addr ma.Multiaddr) error
54
55
+ // SetGatewayAddr sets the Gateway address in the repo.
56
+ SetGatewayAddr(addr net.Addr) error
57
+
58
// SwarmKey returns the configured shared symmetric key for the private networks feature.
59
SwarmKey() ([]byte, error)
60
test/sharness/t0110-gateway.sh
+6
@@ -287,6 +287,12 @@ test_expect_success "GET compact blocks succeeds" '
287
test_cmp expected actual
288
'
289
290
+test_expect_success "Verify gateway file" '
291
+ cat "$IPFS_PATH/gateway" >> gateway_file_actual &&
292
+ echo -n "http://$GWAY_ADDR" >> gateway_daemon_actual &&
293
+ test_cmp gateway_daemon_actual gateway_file_actual
294
+'
295
+
296
test_kill_ipfs_daemon
297
298