Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add $IPFS_PATH/gateway file #9156

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,12 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
return nil, fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err)
}

if len(listeners) > 0 {
if err := node.Repo.SetGatewayAddr(listeners[0].Addr()); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems quite arbitrary to pick the index 0, I would maybe add a filter to pick localhost if possible but I don't think it's worth fixing.

return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
}
}

errc := make(chan error)
var wg sync.WaitGroup
for _, lis := range listeners {
Expand Down
45 changes: 45 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,44 @@ 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()

markg85 marked this conversation as resolved.
Show resolved Hide resolved
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, 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 +514,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
6 changes: 6 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ test_expect_success "GET compact blocks succeeds" '
test_cmp expected actual
'

test_expect_success "Verify gateway file" '
cat "$IPFS_PATH/gateway" >> gateway_file_actual &&
echo -n "http://$GWAY_ADDR" >> gateway_daemon_actual &&
test_cmp gateway_daemon_actual gateway_file_actual
'

test_kill_ipfs_daemon


Expand Down