@cryptotaxi247 / kubo / commits / 6a618df19

go-ipfs-config: allow multiple API/Gateway addresses

Alternative to #6 that doesn't require a migration.

Steven Allen committed Sep 15, 2018 at 13:56 UTC 6a618df199319152a78fdf343f3bbb0d1720f201
4 files changed +43 -6
config/addresses.go
+2 -2
@@ -5,6 +5,6 @@ type Addresses struct {
5 Swarm []string // addresses for the swarm to listen on
6 Announce []string // swarm addresses to announce to the network
7 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
10 }
config/init.go
+2 -2
@@ -105,8 +105,8 @@ func addressesConfig() Addresses {
105 },
106 Announce: []string{},
107 NoAnnounce: []string{},
108 - API: "/ip4/127.0.0.1/tcp/5001",
109 - Gateway: "/ip4/127.0.0.1/tcp/8080",
108 + API: Strings{"/ip4/127.0.0.1/tcp/5001"},
109 + Gateway: Strings{"/ip4/127.0.0.1/tcp/8080"},
110 }
111 }
112
config/profile.go
+2 -2
@@ -66,8 +66,8 @@ profile, enables discovery in local networks.`,
66 is useful when using the daemon in test environments.`,
67
68 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"}
71 c.Addresses.Swarm = []string{
72 "/ip4/127.0.0.1/tcp/0",
73 }
config/types.go new
+37
@@ -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)