forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
74 lines (63 loc) · 1.93 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package api
import (
"fmt"
"net/url"
mgo "gopkg.in/mgo.v2"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
mongodb = "mongodb"
file = "file"
)
// Repository defines the behavior of a proxy specs repository
type Repository interface {
FindAll() ([]*Definition, error)
FindByName(name string) (*Definition, error)
FindByListenPath(path string) (*Definition, error)
Exists(def *Definition) (bool, error)
Add(app *Definition) error
Remove(name string) error
FindValidAPIHealthChecks() ([]*Definition, error)
}
func exists(r Repository, def *Definition) (bool, error) {
_, err := r.FindByName(def.Name)
if nil != err && err != ErrAPIDefinitionNotFound {
return false, err
} else if err != ErrAPIDefinitionNotFound {
return true, ErrAPINameExists
}
_, err = r.FindByListenPath(def.Proxy.ListenPath)
if nil != err && err != ErrAPIDefinitionNotFound {
return false, err
} else if err != ErrAPIDefinitionNotFound {
return true, ErrAPIListenPathExists
}
return false, nil
}
// BuildRepository creates a repository instance that will depend on your given DSN
func BuildRepository(dsn string, session *mgo.Session) (Repository, error) {
dsnURL, err := url.Parse(dsn)
if err != nil {
return nil, errors.Wrap(err, "Error parsing the DSN")
}
switch dsnURL.Scheme {
case mongodb:
repo, err := NewMongoAppRepository(session)
if err != nil {
return nil, errors.Wrap(err, "Could not create a mongodb repository for api definitions")
}
return repo, nil
case file:
log.Debug("File system based configuration chosen")
apiPath := fmt.Sprintf("%s/apis", dsnURL.Path)
log.WithField("api_path", apiPath).Debug("Trying to load configuration files")
repo, err := NewFileSystemRepository(apiPath)
if err != nil {
return nil, errors.Wrap(err, "could not create a file system repository")
}
return repo, nil
default:
return nil, errors.New("The selected scheme is not supported to load API definitions")
}
}