Browse Source

Add config option to disable log coloration (#103)

* Add config option to disable log coloration

Color logs are great in person, but not so awesome when you're trying
to view them in a log aggregation services e.g. splunk, stackdriver,
ELK, etc.

* dump running config to log on startup

* init config before testing

...and use a temporary directory for config_test, lest the presence
of a local one cause confusing test results.
dependabot/npm_and_yarn/web/prismjs-1.21.0
memory 8 years ago
committed by Max Schmitt
parent
commit
fbf234407b
  1. 1
      build/config.yaml
  2. 20
      main.go
  3. 9
      main_test.go
  4. 19
      util/config.go
  5. 2
      util/config_test.go
  6. 2
      util/private.go
  7. 2
      util/private_test.go

1
build/config.yaml

@ -5,6 +5,7 @@ RedisHost: localhost:6379 # If using the redis backend, a host:port combination
RedisPassword: replace me # if using the redis backend, a conneciton password. RedisPassword: replace me # if using the redis backend, a conneciton password.
DataDir: ./data # Contains: the database and the private key DataDir: ./data # Contains: the database and the private key
EnableDebugMode: true # Activates more detailed logging EnableDebugMode: true # Activates more detailed logging
EnableColorLogs: true # Enables/disables ANSI color sequences in log output; default is true
ShortedIDLength: 10 # Length of the random generated ID which is used for new shortened URLs ShortedIDLength: 10 # Length of the random generated ID which is used for new shortened URLs
AuthBackend: oauth # Can be 'oauth' or 'proxy' AuthBackend: oauth # Can be 'oauth' or 'proxy'
Google: # only relevant when using the oauth authbackend Google: # only relevant when using the oauth authbackend

20
main.go

@ -14,11 +14,19 @@ import (
func main() { func main() {
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
if err := initConfig(); err != nil {
logrus.Fatal(err)
}
signal.Notify(stop, os.Interrupt) signal.Notify(stop, os.Interrupt)
logrus.SetFormatter(&logrus.TextFormatter{ logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true, ForceColors: util.GetConfig().EnableColorLogs,
DisableColors: !util.GetConfig().EnableColorLogs,
}) })
logrus.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout)) if util.GetConfig().EnableColorLogs == true {
logrus.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
} else {
logrus.SetOutput(os.Stdout)
}
close, err := initShortener() close, err := initShortener()
if err != nil { if err != nil {
logrus.Fatalf("could not init shortener: %v", err) logrus.Fatalf("could not init shortener: %v", err)
@ -28,10 +36,14 @@ func main() {
close() close()
} }
func initShortener() (func(), error) { func initConfig() error {
if err := util.ReadInConfig(); err != nil { if err := util.ReadInConfig(); err != nil {
return nil, errors.Wrap(err, "could not reload config file") return errors.Wrap(err, "could not load config")
} }
return nil
}
func initShortener() (func(), error) {
if util.GetConfig().EnableDebugMode { if util.GetConfig().EnableDebugMode {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }

9
main_test.go

@ -1,6 +1,7 @@
package main package main
import ( import (
"io/ioutil"
"net" "net"
"testing" "testing"
"time" "time"
@ -9,6 +10,14 @@ import (
) )
func TestInitShortener(t *testing.T) { func TestInitShortener(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "shorten")
if err != nil {
t.Fatal(err)
}
util.Config.DataDir = tmpdir
if err = initConfig(); err != nil {
t.Fatal(err)
}
close, err := initShortener() close, err := initShortener()
if err != nil { if err != nil {
t.Fatalf("could not init shortener: %v", err) t.Fatalf("could not init shortener: %v", err)

19
util/config.go

@ -23,6 +23,7 @@ type Configuration struct {
AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"` AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"`
UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"` UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"`
EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"` EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"`
EnableColorLogs bool `yaml:"EnableColorLogs" env:"ENABLE_COLOR_LOGS"`
ShortedIDLength int `yaml:"ShortedIDLength" env:"SHORTED_ID_LENGTH"` ShortedIDLength int `yaml:"ShortedIDLength" env:"SHORTED_ID_LENGTH"`
Google oAuthConf `yaml:"Google" env:"GOOGLE"` Google oAuthConf `yaml:"Google" env:"GOOGLE"`
GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"` GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"`
@ -42,12 +43,13 @@ type proxyAuthConf struct {
} }
// config contains the default values // config contains the default values
var config = Configuration{ var Config = Configuration{
ListenAddr: ":8080", ListenAddr: ":8080",
BaseURL: "http://localhost:3000", BaseURL: "http://localhost:3000",
DataDir: "data", DataDir: "data",
Backend: "boltdb", Backend: "boltdb",
EnableDebugMode: false, EnableDebugMode: false,
EnableColorLogs: true,
UseSSL: false, UseSSL: false,
ShortedIDLength: 4, ShortedIDLength: 4,
AuthBackend: "oauth", AuthBackend: "oauth",
@ -57,7 +59,7 @@ var config = Configuration{
func ReadInConfig() error { func ReadInConfig() error {
file, err := ioutil.ReadFile("config.yaml") file, err := ioutil.ReadFile("config.yaml")
if err == nil { if err == nil {
if err := yaml.Unmarshal(file, &config); err != nil { if err := yaml.Unmarshal(file, &Config); err != nil {
return errors.Wrap(err, "could not unmarshal yaml file") return errors.Wrap(err, "could not unmarshal yaml file")
} }
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
@ -65,15 +67,16 @@ func ReadInConfig() error {
} else { } else {
logrus.Info("No configuration file found, using defaults with environment variable overrides.") logrus.Info("No configuration file found, using defaults with environment variable overrides.")
} }
if err := envstruct.ApplyEnvVars(&config, "GUS"); err != nil { if err := envstruct.ApplyEnvVars(&Config, "GUS"); err != nil {
return errors.Wrap(err, "could not apply environment configuration") return errors.Wrap(err, "could not apply environment configuration")
} }
config.DataDir, err = filepath.Abs(config.DataDir) logrus.Info("Loaded configuration: %v", Config)
Config.DataDir, err = filepath.Abs(Config.DataDir)
if err != nil { if err != nil {
return errors.Wrap(err, "could not get relative data dir path") return errors.Wrap(err, "could not get relative data dir path")
} }
if _, err = os.Stat(config.DataDir); os.IsNotExist(err) { if _, err = os.Stat(Config.DataDir); os.IsNotExist(err) {
if err = os.MkdirAll(config.DataDir, 0755); err != nil { if err = os.MkdirAll(Config.DataDir, 0755); err != nil {
return errors.Wrap(err, "could not create config directory") return errors.Wrap(err, "could not create config directory")
} }
} }
@ -86,10 +89,10 @@ func (o oAuthConf) Enabled() bool {
// GetConfig returns the configuration from the memory // GetConfig returns the configuration from the memory
func GetConfig() Configuration { func GetConfig() Configuration {
return config return Config
} }
// SetConfig sets the configuration into the memory // SetConfig sets the configuration into the memory
func SetConfig(c Configuration) { func SetConfig(c Configuration) {
config = c Config = c
} }

2
util/config_test.go

@ -8,6 +8,6 @@ func TestReadInConfig(t *testing.T) {
if err := ReadInConfig(); err != nil { if err := ReadInConfig(); err != nil {
t.Fatalf("could not read in config file: %v", err) t.Fatalf("could not read in config file: %v", err)
} }
config := config config := Config
config.DataDir = "./test" config.DataDir = "./test"
} }

2
util/private.go

@ -14,7 +14,7 @@ var privateKey []byte
// CheckForPrivateKey checks if already an private key exists, if not it will // CheckForPrivateKey checks if already an private key exists, if not it will
// be randomly generated and saved as a private.dat file in the data directory // be randomly generated and saved as a private.dat file in the data directory
func CheckForPrivateKey() error { func CheckForPrivateKey() error {
privateDatPath := filepath.Join(config.DataDir, "private.dat") privateDatPath := filepath.Join(Config.DataDir, "private.dat")
privateDatContent, err := ioutil.ReadFile(privateDatPath) privateDatContent, err := ioutil.ReadFile(privateDatPath)
if err == nil { if err == nil {
privateKey = privateDatContent privateKey = privateDatContent

2
util/private_test.go

@ -14,7 +14,7 @@ func TestCheckforPrivateKey(t *testing.T) {
if GetPrivateKey() == nil { if GetPrivateKey() == nil {
t.Fatalf("private key is nil") t.Fatalf("private key is nil")
} }
if err := os.RemoveAll(config.DataDir); err != nil { if err := os.RemoveAll(Config.DataDir); err != nil {
t.Fatalf("could not remove data dir: %v", err) t.Fatalf("could not remove data dir: %v", err)
} }
} }

Loading…
Cancel
Save