Skip to content
Snippets Groups Projects
Commit 0424ea77 authored by Niall Sheridan's avatar Niall Sheridan
Browse files

Allow setting some config from environment

parent 921818bc
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,7 @@ Configuration is divided into different sections: `server`, `auth`, `ssh`, and ` ...@@ -97,6 +97,7 @@ Configuration is divided into different sections: `server`, `auth`, `ssh`, and `
- `port` : int. Port to listen on. - `port` : int. Port to listen on.
- `user` : string. User to which the server drops privileges to. - `user` : string. User to which the server drops privileges to.
- `cookie_secret`: string. Authentication key for the session cookie. - `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. - `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). - `datastore`: string. Datastore connection string. See [Datastore](#datastore).
... ...
......
...@@ -334,7 +334,8 @@ func main() { ...@@ -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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -399,7 +400,7 @@ func main() { ...@@ -399,7 +400,7 @@ func main() {
r.PathPrefix("/").Handler(http.FileServer(static.FS(false))) r.PathPrefix("/").Handler(http.FileServer(static.FS(false)))
h := handlers.LoggingHandler(logfile, r) h := handlers.LoggingHandler(logfile, r)
log.Print("Starting server...") log.Printf("Starting server on %s", laddr)
s := &http.Server{ s := &http.Server{
Handler: h, Handler: h,
} }
... ...
......
...@@ -3,6 +3,8 @@ package config ...@@ -3,6 +3,8 @@ package config
import ( import (
"errors" "errors"
"io" "io"
"os"
"strconv"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"github.com/spf13/viper" "github.com/spf13/viper"
...@@ -82,6 +84,28 @@ func verifyConfig(u *unmarshalled) error { ...@@ -82,6 +84,28 @@ func verifyConfig(u *unmarshalled) error {
return err 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. // ReadConfig parses a JSON configuration file into a Config struct.
func ReadConfig(r io.Reader) (*Config, error) { func ReadConfig(r io.Reader) (*Config, error) {
u := &unmarshalled{} u := &unmarshalled{}
...@@ -93,6 +117,7 @@ func ReadConfig(r io.Reader) (*Config, error) { ...@@ -93,6 +117,7 @@ func ReadConfig(r io.Reader) (*Config, error) {
if err := v.Unmarshal(u); err != nil { if err := v.Unmarshal(u); err != nil {
return nil, err return nil, err
} }
setFromEnv(u)
if err := verifyConfig(u); err != nil { if err := verifyConfig(u); err != nil {
return nil, err return nil, err
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment