diff --git a/gateway/gateway.go b/gateway/gateway.go index 52554b99..30836de7 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -47,7 +47,7 @@ const apiProtocol = "api" // New creates a new Gateway object associated with the specified router and // following the given configuration. -func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway { +func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) (*Gateway, error) { logger := rootLogger.WithFields(logrus.Fields{"prefix": "gateway"}) cache, _ := lru.New(5000) @@ -67,13 +67,25 @@ func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway { persistentMessageStorePath, usePersistent := gw.Config.GetString("PersistentMessageStorePath") if usePersistent { rootPath := fmt.Sprintf("%s/%s", persistentMessageStorePath, gw.Name) - os.MkdirAll(rootPath, os.ModePerm) + err := os.MkdirAll(rootPath, os.ModePerm) + if err != nil { + return nil, err + } - gw.MessageStore = gw.getMessageMapStore(fmt.Sprintf("%s/Messages", rootPath)) - gw.CanonicalStore = gw.getMessageMapStore(fmt.Sprintf("%s/Canonical", rootPath)) + MessageStore, err := gw.getMessageMapStore(fmt.Sprintf("%s/Messages", rootPath)) + if err != nil { + return nil, err + } + gw.MessageStore = MessageStore + + CanonicalStore, err := gw.getMessageMapStore(fmt.Sprintf("%s/Canonical", rootPath)) + if err != nil { + return nil, err + } + gw.CanonicalStore = CanonicalStore } - return gw + return gw, nil } func (gw *Gateway) SetMessageMap(canonicalMsgID string, msgIDs []*BrMsgID) { diff --git a/gateway/persistent.go b/gateway/persistent.go index f19ede23..e1e659e8 100644 --- a/gateway/persistent.go +++ b/gateway/persistent.go @@ -8,7 +8,7 @@ import ( "github.com/philippgille/gokv/encoding" ) -func (gw *Gateway) getMessageMapStore(path string) gokv.Store { +func (gw *Gateway) getMessageMapStore(path string) (gokv.Store, error) { options := badgerdb.Options{ Dir: path, Codec: encoding.Gob, @@ -16,11 +16,12 @@ func (gw *Gateway) getMessageMapStore(path string) gokv.Store { store, err := badgerdb.NewStore(options) if err != nil { - gw.logger.Error(err) gw.logger.Errorf("Could not connect to db: %s", path) + gw.logger.Error(err) + return nil, err } - return store + return store, nil } func (gw *Gateway) getCanonicalMessageFromStore(messageID string) string { diff --git a/gateway/router.go b/gateway/router.go index 5010730c..9521d354 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -50,7 +50,12 @@ func NewRouter(rootLogger *logrus.Logger, cfg config.Config, bridgeMap map[strin if _, ok := r.Gateways[entry.Name]; ok { return nil, fmt.Errorf("Gateway with name %s already exists", entry.Name) } - r.Gateways[entry.Name] = New(rootLogger, entry, r) + gw, err := New(rootLogger, entry, r) + if err != nil { + return nil, err + } + + r.Gateways[entry.Name] = gw } return r, nil }