Skip to content

Commit

Permalink
Remove service-config library from route-registrar
Browse files Browse the repository at this point in the history
- Use go-yaml library instead
- More inline with our other components
  • Loading branch information
crhino committed Jul 28, 2016
1 parent 636c501 commit 1a70a5e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
19 changes: 19 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package config

import (
"fmt"
"io/ioutil"
"net/url"
"strconv"
"time"

"gopkg.in/yaml.v2"

"github.com/cloudfoundry/multierror"
)

Expand Down Expand Up @@ -65,6 +68,22 @@ type Route struct {
HealthCheck *HealthCheck
}

func NewConfigSchemaFromFile(configFile string) (ConfigSchema, error) {
var config ConfigSchema

c, err := ioutil.ReadFile(configFile)
if err != nil {
return ConfigSchema{}, err
}

err = yaml.Unmarshal(c, &config)
if err != nil {
return ConfigSchema{}, err
}

return config, nil
}

func (c ConfigSchema) ToConfig() (*Config, error) {
errors := multierror.MultiError{}

Expand Down
45 changes: 45 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config_test

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/cloudfoundry-incubator/route-registrar/config"
Expand Down Expand Up @@ -71,6 +73,49 @@ var _ = Describe("Config", func() {
}
})

Describe("NewConfigSchemaFromFile", func() {
It("returns a valid config", func() {
cfg_file := "../example_config/example.yml"
cfg, err := config.NewConfigSchemaFromFile(cfg_file)

Expect(err).NotTo(HaveOccurred())
Expect(cfg).To(Equal(configSchema))
})

Context("when the file does not exists", func() {
It("returns an error", func() {
cfg_file := "notexist"
_, err := config.NewConfigSchemaFromFile(cfg_file)

Expect(err).To(HaveOccurred())
})
})

Context("when the config is invalid", func() {
var (
configFile *os.File
)

BeforeEach(func() {
var err error
configFile, err = ioutil.TempFile("", "route-registrar-config")
Expect(err).NotTo(HaveOccurred())

_, err = configFile.Write([]byte("invalid yaml %^&#"))
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
os.Remove(configFile.Name())
})

It("returns an error", func() {
_, err := config.NewConfigSchemaFromFile(configFile.Name())
Expect(err).To(HaveOccurred())
})
})
})

Describe("ToConfig", func() {
It("returns a Config object and no error", func() {
c, err := configSchema.ToConfig()
Expand Down
18 changes: 18 additions & 0 deletions example_config/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
host: "127.0.0.1"
routes:
- name: "route-0"
port: 3000
uris: ["my-app.my-domain.com"]
registration_interval: 20s
- name: "route-1"
port: 3001
uris: ["my-other-app.my-domain.com"]
registration_interval: 10s
message_bus_servers:
- host: "some-host"
user: "some-user"
password: "some-password"
- host: "another-host"
user: "another-user"
password: "another-password"
12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import (
"syscall"

"github.com/cloudfoundry-incubator/cf-lager"
"github.com/pivotal-cf-experimental/service-config"
"github.com/pivotal-golang/lager"
"github.com/tedsuo/ifrit"
"github.com/cloudfoundry-incubator/route-registrar/config"
"github.com/cloudfoundry-incubator/route-registrar/healthchecker"
"github.com/cloudfoundry-incubator/route-registrar/messagebus"
"github.com/cloudfoundry-incubator/route-registrar/registrar"
"github.com/pivotal-golang/lager"
"github.com/tedsuo/ifrit"
)

func main() {
var configPath string
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

pidfile := flags.String("pidfile", "", "Path to pid file")
cf_lager.AddFlags(flags)

serviceConfig := service_config.New()
serviceConfig.AddFlags(flags)
flags.StringVar(&configPath, "configPath", "", "path to configuration file with json encoded content")
flags.Set("configPath", "registrar_settings.yml")

flags.Parse(os.Args[1:])
Expand All @@ -35,8 +34,7 @@ func main() {

logger.Info("Initializing")

var configSchema config.ConfigSchema
err := serviceConfig.Read(&configSchema)
configSchema, err := config.NewConfigSchemaFromFile(configPath)
if err != nil {
logger.Fatal("error parsing file: %s\n", err)
}
Expand Down

0 comments on commit 1a70a5e

Please sign in to comment.