From 6683f55f77bc6ebef86c5acb9c3ebdfe146fb8e0 Mon Sep 17 00:00:00 2001 From: Peter Vrba Date: Thu, 23 Jul 2015 00:21:09 +0200 Subject: [PATCH] Chdir to config directory --- cmd/goexpose/goexpose.go | 7 +++++++ config.go | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/goexpose/goexpose.go b/cmd/goexpose/goexpose.go index 7eb3bd6..c0fb6ad 100644 --- a/cmd/goexpose/goexpose.go +++ b/cmd/goexpose/goexpose.go @@ -20,11 +20,18 @@ func main() { err error ) + // read config file if config, err = goexpose.NewConfigFromFilename(*configVar); err != nil { glog.Errorf("config error: %v", err) os.Exit(1) } + // change working directory to config directory + if err = os.Chdir(config.Directory); err != nil { + glog.Errorf("config error: %v", err) + os.Exit(1) + } + if server, err = goexpose.NewServer(config); err != nil { glog.Errorf("server error: %v", err) os.Exit(1) diff --git a/config.go b/config.go index 41fe5ff..8a0711b 100644 --- a/config.go +++ b/config.go @@ -1,14 +1,15 @@ package goexpose import ( + "crypto/sha256" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" + "path/filepath" "regexp" "strings" - "crypto/sha256" - "io" ) /* @@ -17,7 +18,9 @@ Returns filename from file func NewConfigFromFilename(filename string) (config *Config, err error) { config = NewConfig() - var result []byte + var ( + result []byte + ) if result, err = ioutil.ReadFile(filename); err != nil { return } @@ -27,6 +30,11 @@ func NewConfigFromFilename(filename string) (config *Config, err error) { return } + // get config dir + if config.Directory, err = filepath.Abs(filepath.Dir(filename)); err != nil { + return + } + return } @@ -51,6 +59,7 @@ type Config struct { Authorizers map[string]*AuthorizerConfig `json:"authorizers"` Endpoints []*EndpointConfig `json:"endpoints"` ReloadEnv bool `json:"reload_env"` + Directory string `json:"-"` } /* @@ -102,7 +111,7 @@ func (e *EndpointConfig) Validate() (err error) { return } -func (e *EndpointConfig ) RouteName() string { +func (e *EndpointConfig) RouteName() string { hash := sha256.New() io.WriteString(hash, e.Path) return fmt.Sprintf("%x", hash.Sum(nil))