Skip to content

Commit

Permalink
go-ipfs-config: allow multiple API/Gateway addresses
Browse files Browse the repository at this point in the history
Alternative to ipfs#6 that doesn't require a migration.
  • Loading branch information
Stebalien committed Sep 21, 2018
1 parent dd22be5 commit 6a618df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions config/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ type Addresses struct {
Swarm []string // addresses for the swarm to listen on
Announce []string // swarm addresses to announce to the network
NoAnnounce []string // swarm addresses not to announce to the network
API string // address for the local API (RPC)
Gateway string // address to listen on for IPFS HTTP object gateway
API Strings // address for the local API (RPC)
Gateway Strings // address to listen on for IPFS HTTP object gateway
}
4 changes: 2 additions & 2 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func addressesConfig() Addresses {
},
Announce: []string{},
NoAnnounce: []string{},
API: "/ip4/127.0.0.1/tcp/5001",
Gateway: "/ip4/127.0.0.1/tcp/8080",
API: Strings{"/ip4/127.0.0.1/tcp/5001"},
Gateway: Strings{"/ip4/127.0.0.1/tcp/8080"},
}
}

Expand Down
4 changes: 2 additions & 2 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ profile, enables discovery in local networks.`,
is useful when using the daemon in test environments.`,

Transform: func(c *Config) error {
c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
c.Addresses.API = Strings{"/ip4/127.0.0.1/tcp/0"}
c.Addresses.Gateway = Strings{"/ip4/127.0.0.1/tcp/0"}
c.Addresses.Swarm = []string{
"/ip4/127.0.0.1/tcp/0",
}
Expand Down
37 changes: 37 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"encoding/json"
)

// Strings is a helper type that (un)marshals a single string to/from a single
// JSON string and a slice of strings to/from a JSON array of strings.
type Strings []string

// UnmarshalJSON conforms to the json.Unmarshaler interface.
func (o *Strings) UnmarshalJSON(data []byte) error {
if data[0] == '[' {
return json.Unmarshal(data, (*[]string)(o))
}
var value string
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*o = []string{value}
return nil
}

// MarshalJSON conforms to the json.Marshaler interface.
func (o Strings) MarshalJSON() ([]byte, error) {
switch len(o) {
case 0:
return json.Marshal(nil)
case 1:
return json.Marshal(o[0])
default:
return json.Marshal([]string(o))
}
}

var _ json.Unmarshaler = (*Strings)(nil)
var _ json.Marshaler = (*Strings)(nil)

0 comments on commit 6a618df

Please sign in to comment.