Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
added support for config by environment variable, resolved #3
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Apr 6, 2020
1 parent 19f7ca3 commit 00be0c9
Show file tree
Hide file tree
Showing 18 changed files with 838 additions and 29 deletions.
12 changes: 5 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ LABEL BUILD=${buildnum}

WORKDIR /home
COPY --from=builder /home/thing-directory .
COPY sample_conf/thing-directory.json /conf/
COPY wot/wot_td_schema.json /conf/
COPY sample_conf/thing-directory.json /home/conf/
COPY wot/wot_td_schema.json /home/conf/

ENV SC_DNSSDENABLED=false
ENV SC_STORAGE_TYPE=leveldb
ENV SC_STORAGE_DSN=/data
ENV TD_STORAGE_DSN=/data

VOLUME /conf /data
VOLUME /data
EXPOSE 8081

ENTRYPOINT ["./thing-directory"]
CMD ["--conf", "/conf/thing-directory.json", "--schema", "/conf/wot_td_schema.json"]
# Note: this loads the default config files from /home/conf/. Use --help to to learn about CLI arguments.
48 changes: 29 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/url"

"github.com/kelseyhightower/envconfig"
"github.com/linksmart/go-sec/authz"
)

Expand All @@ -19,22 +20,23 @@ const (
)

type Config struct {
ServiceID string `json:"serviceID"`
Description string `json:"description"`
PublicEndpoint string `json:"publicEndpoint"`
BindAddr string `json:"bindAddr"`
BindPort int `json:"bindPort"`
DnssdEnabled bool `json:"dnssdEnabled"`
Storage StorageConfig `json:"storage"`
ServiceCatalog *ServiceCatalog `json:"serviceCatalog"`
Auth ValidatorConf `json:"auth"`
ServiceID string `json:"serviceID"`
Description string `json:"description"`
PublicEndpoint string `json:"publicEndpoint"`
BindAddr string `json:"bindAddr"`
BindPort int `json:"bindPort"`
DnssdEnabled bool `json:"dnssdEnabled"`
Storage StorageConfig `json:"storage"`
ServiceCatalog ServiceCatalog `json:"serviceCatalog"`
Auth ValidatorConf `json:"auth"`
}

type ServiceCatalog struct {
Discover bool `json:"discover"`
Endpoint string `json:"endpoint"`
Ttl int `json:"ttl"`
Auth *ObtainerConf `json:"auth"`
Enabled bool `json:"enabled"`
Discover bool `json:"discover"`
Endpoint string `json:"endpoint"`
Ttl int `json:"ttl"`
Auth ObtainerConf `json:"auth"`
}

type StorageConfig struct {
Expand Down Expand Up @@ -64,14 +66,14 @@ func (c *Config) Validate() error {
err = fmt.Errorf("Unsupported storage backend")
}

if c.ServiceCatalog != nil {
if c.ServiceCatalog.Enabled {
if c.ServiceCatalog.Endpoint == "" && c.ServiceCatalog.Discover == false {
err = fmt.Errorf("All ServiceCatalog entries must have either endpoint or a discovery flag defined")
}
if c.ServiceCatalog.Ttl <= 0 {
err = fmt.Errorf("All ServiceCatalog entries must have TTL >= 0")
}
if c.ServiceCatalog.Auth != nil {
if c.ServiceCatalog.Auth.Enabled {
// Validate ticket obtainer config
err = c.ServiceCatalog.Auth.Validate()
if err != nil {
Expand All @@ -97,16 +99,22 @@ func loadConfig(path string) (*Config, error) {
return nil, err
}

c := new(Config)
err = json.Unmarshal(file, c)
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}

if err = c.Validate(); err != nil {
// Override loaded values with environment variables
err = envconfig.Process("td", &config)
if err != nil {
return nil, err
}

if err = config.Validate(); err != nil {
return nil, err
}
return c, nil
return &config, nil
}

// Ticket Validator Config
Expand Down Expand Up @@ -158,6 +166,8 @@ func (c ValidatorConf) Validate() error {

// Ticket Obtainer Client Config
type ObtainerConf struct {
// Auth switch
Enabled bool `json:"enabled"`
// Authentication provider name
Provider string `json:"provider"`
// Authentication provider URL
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.7.3
github.com/justinas/alice v0.0.0-20160512134231-052b8b6c18ed
github.com/kelseyhightower/envconfig v1.4.0
github.com/linksmart/go-sec v1.0.1
github.com/linksmart/service-catalog/v3 v3.0.0-beta.1.0.20200302143206-92739dd2a511
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/justinas/alice v0.0.0-20160512134231-052b8b6c18ed h1:Ab4XhecWusSSeIfQ
github.com/justinas/alice v0.0.0-20160512134231-052b8b6c18ed/go.mod h1:oLH0CmIaxCGXD67VKGR5AacGXZSMznlmeqM8RzPrcY8=
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/linksmart/go-sec v1.0.1 h1:UNeRj81/KHCy4hkFcZn7x5N/nM+uNxe+xsLBQzNF7kg=
github.com/linksmart/go-sec v1.0.1/go.mod h1:bTksBzP6fCEwIM43z8m3jSRa4YIAWdUwMBYjcoftm1c=
github.com/linksmart/service-catalog/v3 v3.0.0-beta.1.0.20200302143206-92739dd2a511 h1:JNHuaKtZUDsgbGJ5bdFBZ4vIUlJB7EBvjLdSaNOFatQ=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func main() {
}

// Register in the LinkSmart Service Catalog
if config.ServiceCatalog != nil {
if config.ServiceCatalog.Enabled {
unregisterService, err := registerInServiceCatalog(config)
if err != nil {
panic("Error registering service:" + err.Error())
Expand Down
2 changes: 1 addition & 1 deletion registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func registerInServiceCatalog(conf *Config) (func() error, error) {

var ticket *obtainer.Client
var err error
if cat.Auth != nil {
if cat.Auth.Enabled {
// Setup ticket client
ticket, err = obtainer.NewClient(cat.Auth.Provider, cat.Auth.ProviderURL, cat.Auth.Username, cat.Auth.Password, cat.Auth.ServiceID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sample_conf/thing-directory.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bindPort": 8081,
"storage": {
"type": "leveldb",
"dsn": "/data"
"dsn": "./data"
},
"serviceCatalog": null,
"auth": {
Expand Down
13 changes: 13 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/MAINTAINERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

192 changes: 192 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 00be0c9

Please sign in to comment.