Skip to content

Commit 8bbbe6b

Browse files
authored
go-ipfs-config: Merge pull request ipfs#11 from ipfs/feat/multi-addr
allow multiple API/Gateway addresses
2 parents fa1e389 + c0666ba commit 8bbbe6b

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

config/addresses.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ type Addresses struct {
55
Swarm []string // addresses for the swarm to listen on
66
Announce []string // swarm addresses to announce to the network
77
NoAnnounce []string // swarm addresses not to announce to the network
8-
API string // address for the local API (RPC)
9-
Gateway string // address to listen on for IPFS HTTP object gateway
8+
API Strings // address for the local API (RPC)
9+
Gateway Strings // address to listen on for IPFS HTTP object gateway
1010
}

config/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func addressesConfig() Addresses {
106106
},
107107
Announce: []string{},
108108
NoAnnounce: []string{},
109-
API: "/ip4/127.0.0.1/tcp/5001",
110-
Gateway: "/ip4/127.0.0.1/tcp/8080",
109+
API: Strings{"/ip4/127.0.0.1/tcp/5001"},
110+
Gateway: Strings{"/ip4/127.0.0.1/tcp/8080"},
111111
}
112112
}
113113

config/profile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ profile, enables discovery in local networks.`,
6666
is useful when using the daemon in test environments.`,
6767

6868
Transform: func(c *Config) error {
69-
c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
70-
c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
69+
c.Addresses.API = Strings{"/ip4/127.0.0.1/tcp/0"}
70+
c.Addresses.Gateway = Strings{"/ip4/127.0.0.1/tcp/0"}
7171
c.Addresses.Swarm = []string{
7272
"/ip4/127.0.0.1/tcp/0",
7373
}

config/types.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
// Strings is a helper type that (un)marshals a single string to/from a single
8+
// JSON string and a slice of strings to/from a JSON array of strings.
9+
type Strings []string
10+
11+
// UnmarshalJSON conforms to the json.Unmarshaler interface.
12+
func (o *Strings) UnmarshalJSON(data []byte) error {
13+
if data[0] == '[' {
14+
return json.Unmarshal(data, (*[]string)(o))
15+
}
16+
var value string
17+
if err := json.Unmarshal(data, &value); err != nil {
18+
return err
19+
}
20+
*o = []string{value}
21+
return nil
22+
}
23+
24+
// MarshalJSON conforms to the json.Marshaler interface.
25+
func (o Strings) MarshalJSON() ([]byte, error) {
26+
switch len(o) {
27+
case 0:
28+
return json.Marshal(nil)
29+
case 1:
30+
return json.Marshal(o[0])
31+
default:
32+
return json.Marshal([]string(o))
33+
}
34+
}
35+
36+
var _ json.Unmarshaler = (*Strings)(nil)
37+
var _ json.Marshaler = (*Strings)(nil)

0 commit comments

Comments
 (0)