From 6a618df199319152a78fdf343f3bbb0d1720f201 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sat, 15 Sep 2018 13:56:12 -0700 Subject: [PATCH] go-ipfs-config: allow multiple API/Gateway addresses Alternative to #6 that doesn't require a migration. --- config/addresses.go | 4 ++-- config/init.go | 4 ++-- config/profile.go | 4 ++-- config/types.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 config/types.go diff --git a/config/addresses.go b/config/addresses.go index 22c530655b2..2d88468fdac 100644 --- a/config/addresses.go +++ b/config/addresses.go @@ -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 } diff --git a/config/init.go b/config/init.go index c4ea9e41bcd..f45bf56725b 100644 --- a/config/init.go +++ b/config/init.go @@ -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"}, } } diff --git a/config/profile.go b/config/profile.go index d3f1b42d09c..d23cadc6d97 100644 --- a/config/profile.go +++ b/config/profile.go @@ -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", } diff --git a/config/types.go b/config/types.go new file mode 100644 index 00000000000..376cc64f072 --- /dev/null +++ b/config/types.go @@ -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)