Skip to content

Commit

Permalink
Make file and static backend work with config too
Browse files Browse the repository at this point in the history
  • Loading branch information
tino committed Dec 6, 2017
1 parent 3e08824 commit 883be35
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ type Registry struct {
}

type Static struct {
Routes string
NoRouteHTMLPath string
Routes string
}

type File struct {
Path string
NoRouteHTMLPath string
Path string
}

type Consul struct {
Expand Down
2 changes: 2 additions & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.DurationVar(&cfg.Registry.Timeout, "registry.timeout", defaultConfig.Registry.Timeout, "timeout for registry to become available")
f.DurationVar(&cfg.Registry.Retry, "registry.retry", defaultConfig.Registry.Retry, "retry interval during startup")
f.StringVar(&cfg.Registry.File.Path, "registry.file.path", defaultConfig.Registry.File.Path, "path to file based routing table")
f.StringVar(&cfg.Registry.File.NoRouteHTMLPath, "registry.file.noroutehtmlpath", defaultConfig.Registry.File.NoRouteHTMLPath, "path to file for HTML returned when no route is found")
f.StringVar(&cfg.Registry.Static.Routes, "registry.static.routes", defaultConfig.Registry.Static.Routes, "static routes")
f.StringVar(&cfg.Registry.Static.NoRouteHTMLPath, "registry.static.noroutehtmlpath", defaultConfig.Registry.Static.NoRouteHTMLPath, "path to file for HTML returned when no route is found")
f.StringVar(&cfg.Registry.Consul.Addr, "registry.consul.addr", defaultConfig.Registry.Consul.Addr, "address of the consul agent")
f.StringVar(&cfg.Registry.Consul.Token, "registry.consul.token", defaultConfig.Registry.Consul.Token, "token for consul agent")
f.StringVar(&cfg.Registry.Consul.KVPath, "registry.consul.kvpath", defaultConfig.Registry.Consul.KVPath, "consul KV path for manual overrides")
Expand Down
18 changes: 17 additions & 1 deletion fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@
# registry.static.routes =


# registry.static.noroutehtmlpath configures the KV path for the HTML of the
# noroutes page.
#
# The default is
#
# registry.static.noroutehtmlpath =


# registry.file.path configures a file based routing table.
# The value configures the path to the file with the routing table.
#
Expand All @@ -543,6 +551,14 @@
# registry.file.path =


# registry.file.noroutehtmlpath configures the KV path for the HTML of the
# noroutes page.
#
# The default is
#
# registry.file.noroutehtmlpath =


# registry.consul.addr configures the address of the consul agent to connect to.
#
# The default is
Expand Down Expand Up @@ -574,7 +590,7 @@
#
# The default is
#
# registry.consul.kvpath = /fabio/noroutes.html
# registry.consul.noroutehtmlpath = /fabio/noroutes.html

# registry.consul.service.status configures the valid service status
# values for services included in the routing table.
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ func initBackend(cfg *config.Config) {
for {
switch cfg.Registry.Backend {
case "file":
registry.Default, err = file.NewBackend(cfg.Registry.File.Path)
registry.Default, err = file.NewBackend(&cfg.Registry.File)
case "static":
registry.Default, err = static.NewBackend(cfg.Registry.Static.Routes)
registry.Default, err = static.NewBackend(&cfg.Registry.Static)
case "consul":
registry.Default, err = consul.NewBackend(&cfg.Registry.Consul)
default:
Expand Down
10 changes: 6 additions & 4 deletions registry/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"io/ioutil"
"log"

"github.com/fabiolb/fabio/config"
"github.com/fabiolb/fabio/registry"
"github.com/fabiolb/fabio/registry/static"
)

func NewBackend(filename string) (registry.Backend, error) {
data, err := ioutil.ReadFile(filename)
func NewBackend(cfg *config.File) (registry.Backend, error) {
data, err := ioutil.ReadFile(cfg.Path)
if err != nil {
log.Println("[ERROR] Cannot read routes from ", filename)
log.Println("[ERROR] Cannot read routes from ", cfg.Path)
return nil, err
}
return static.NewBackend(string(data))
staticCfg := config.Static{cfg.NoRouteHTMLPath, string(data)}
return static.NewBackend(&staticCfg)
}
18 changes: 9 additions & 9 deletions registry/static/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"io/ioutil"
"log"

"github.com/fabiolb/fabio/config"
"github.com/fabiolb/fabio/registry"
"github.com/fabiolb/fabio/route"
)

type be struct{}

var staticRoutes string
type be struct {
cfg *config.Static
}

func NewBackend(routes string) (registry.Backend, error) {
staticRoutes = routes
return &be{}, nil
func NewBackend(cfg *config.Static) (registry.Backend, error) {
return &be{cfg}, nil
}

func (b *be) Register() error {
Expand All @@ -37,7 +37,7 @@ func (b *be) WriteManual(value string, version uint64) (ok bool, err error) {

func (b *be) WatchServices() chan string {
ch := make(chan string, 1)
ch <- staticRoutes
ch <- b.cfg.Routes
return ch
}

Expand All @@ -48,9 +48,9 @@ func (b *be) WatchManual() chan string {
// WatchNoRouteHTML implementation that reads the noroute html from a
// noroute.html file if it exists
func (b *be) WatchNoRouteHTML() chan string {
data, err := ioutil.ReadFile("noroute.html")
data, err := ioutil.ReadFile(b.cfg.NoRouteHTMLPath)
if err != nil {
log.Println("[WARN] No noroute.html to read noroute html from")
log.Printf("[WARN] Could not read NoRouteHTMLPath (%s)", b.cfg.NoRouteHTMLPath)
}
route.SetHTML(string(data))
return make(chan string)
Expand Down

0 comments on commit 883be35

Please sign in to comment.