diff options
| author | Niall Sheridan <nsheridan@gmail.com> | 2016-08-27 21:34:53 +0100 | 
|---|---|---|
| committer | Niall Sheridan <nsheridan@gmail.com> | 2016-08-27 21:34:53 +0100 | 
| commit | 0424ea77d6c09624f35c291a68dde60ea9ca8453 (patch) | |
| tree | faa0ddda5b5e9e95993bc0af744d3d65c064c731 | |
| parent | 921818bca208f0c70e85ec670074cb3905cbbc82 (diff) | |
Allow setting some config from environment
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | cmd/cashierd/main.go | 5 | ||||
| -rw-r--r-- | server/config/config.go | 25 | 
3 files changed, 29 insertions, 2 deletions
| @@ -97,6 +97,7 @@ Configuration is divided into different sections: `server`, `auth`, `ssh`, and `  - `port` : int. Port to listen on.  - `user` : string. User to which the server drops privileges to.  - `cookie_secret`: string. Authentication key for the session cookie. +- `csrf_secret`: string. Authentication key for CSRF protection.  - `http_logfile`: string. Path to the HTTP request log. Logs are written in the [Common Log Format](https://en.wikipedia.org/wiki/Common_Log_Format). If not set logs are written to stderr.  - `datastore`: string. Datastore connection string. See [Datastore](#datastore). diff --git a/cmd/cashierd/main.go b/cmd/cashierd/main.go index b29cd75..e3aec93 100644 --- a/cmd/cashierd/main.go +++ b/cmd/cashierd/main.go @@ -334,7 +334,8 @@ func main() {  		}  	} -	l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.Server.Addr, config.Server.Port)) +	laddr := fmt.Sprintf("%s:%d", config.Server.Addr, config.Server.Port) +	l, err := net.Listen("tcp", laddr)  	if err != nil {  		log.Fatal(err)  	} @@ -399,7 +400,7 @@ func main() {  	r.PathPrefix("/").Handler(http.FileServer(static.FS(false)))  	h := handlers.LoggingHandler(logfile, r) -	log.Print("Starting server...") +	log.Printf("Starting server on %s", laddr)  	s := &http.Server{  		Handler: h,  	} diff --git a/server/config/config.go b/server/config/config.go index f1341c1..3587e9f 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -3,6 +3,8 @@ package config  import (  	"errors"  	"io" +	"os" +	"strconv"  	"github.com/hashicorp/go-multierror"  	"github.com/spf13/viper" @@ -82,6 +84,28 @@ func verifyConfig(u *unmarshalled) error {  	return err  } +func setFromEnv(u *unmarshalled) { +	port, err := strconv.Atoi(os.Getenv("PORT")) +	if err == nil { +		u.Server[0].Port = port +	} +	if os.Getenv("DATASTORE") != "" { +		u.Server[0].Datastore = os.Getenv("DATASTORE") +	} +	if os.Getenv("OAUTH_CLIENT_ID") != "" { +		u.Auth[0].OauthClientID = os.Getenv("OAUTH_CLIENT_ID") +	} +	if os.Getenv("OAUTH_CLIENT_SECRET") != "" { +		u.Auth[0].OauthClientSecret = os.Getenv("OAUTH_CLIENT_SECRET") +	} +	if os.Getenv("CSRF_SECRET") != "" { +		u.Server[0].CSRFSecret = os.Getenv("CSRF_SECRET") +	} +	if os.Getenv("COOKIE_SECRET") != "" { +		u.Server[0].CookieSecret = os.Getenv("COOKIE_SECRET") +	} +} +  // ReadConfig parses a JSON configuration file into a Config struct.  func ReadConfig(r io.Reader) (*Config, error) {  	u := &unmarshalled{} @@ -93,6 +117,7 @@ func ReadConfig(r io.Reader) (*Config, error) {  	if err := v.Unmarshal(u); err != nil {  		return nil, err  	} +	setFromEnv(u)  	if err := verifyConfig(u); err != nil {  		return nil, err  	} | 
