Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,10 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
return nil, fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err)
}

if err := node.Repo.SetGatewayAddr(listeners[0].Addr()); err != nil {
return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
}

errc := make(chan error)
var wg sync.WaitGroup
for _, lis := range listeners {
Expand Down
43 changes: 43 additions & 0 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -63,6 +64,7 @@ func (err NoRepoError) Error() string {
}

const apiFile = "api"
const gatewayFile = "gateway"
const swarmKeyFile = "swarm.key"

const specFn = "datastore_spec"
Expand Down Expand Up @@ -387,6 +389,42 @@ func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error {
return err
}

// SetGatewayAddr writes the Gateway Addr to the /gateway file.
func (r *FSRepo) SetGatewayAddr(addr net.Addr) error {
// Create a temp file to write the address, so that we don't leave empty file when the
// program crashes after creating the file.
tmpPath := filepath.Join(r.path, "."+gatewayFile+".tmp")
f, err := os.Create(tmpPath)
if err != nil {
return err
}
var good bool
// Silently remove as worst last case with defers.
defer func() {
if !good { os.Remove(tmpPath) }
}()
defer f.Close()

if _, err := fmt.Fprintf(f, "http://%s", addr.String()); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}

// Atomically rename the temp file to the correct file name.
err = os.Rename(tmpPath, filepath.Join(r.path, gatewayFile))
good = err == nil
if good {
return nil
}
// Remove the temp file when rename return error
if err1 := os.Remove(tmpPath); err1 != nil {
return fmt.Errorf("File Rename error: %w, File remove error: %s", err.Error(), err1.Error())
}
return err
}

// openConfig returns an error if the config file is not present.
func (r *FSRepo) openConfig() error {
conf, err := serialize.Load(r.configFilePath)
Expand Down Expand Up @@ -474,6 +512,11 @@ func (r *FSRepo) Close() error {
log.Warn("error removing api file: ", err)
}

err = os.Remove(filepath.Join(r.path, gatewayFile))
if err != nil && !os.IsNotExist(err) {
log.Warn("error removing gateway file: ", err)
}

if err := r.ds.Close(); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions repo/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repo
import (
"context"
"errors"
"net"

filestore "github.com/ipfs/go-filestore"
keystore "github.com/ipfs/go-ipfs-keystore"
Expand Down Expand Up @@ -50,6 +51,8 @@ func (m *Mock) Close() error { return m.D.Close() }

func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }

func (m *Mock) SetGatewayAddr(addr net.Addr) error { return errTODO }

func (m *Mock) Keystore() keystore.Keystore { return m.K }

func (m *Mock) SwarmKey() ([]byte, error) {
Expand Down
4 changes: 4 additions & 0 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"net"

filestore "github.com/ipfs/go-filestore"
keystore "github.com/ipfs/go-ipfs-keystore"
Expand Down Expand Up @@ -51,6 +52,9 @@ type Repo interface {
// SetAPIAddr sets the API address in the repo.
SetAPIAddr(addr ma.Multiaddr) error

// SetGatewayAddr sets the Gateway address in the repo.
SetGatewayAddr(addr net.Addr) error

// SwarmKey returns the configured shared symmetric key for the private networks feature.
SwarmKey() ([]byte, error)

Expand Down