1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 04:21:18 +00:00

Added check for configuration

This commit is contained in:
Knut Ahlers 2015-07-06 23:23:07 +02:00
parent d80b6cbaa3
commit 72d90f5fbe

View file

@ -1,11 +1,17 @@
package config // import "github.com/Luzifer/mondash/config" package config // import "github.com/Luzifer/mondash/config"
import ( import (
"fmt"
"log"
"net/url"
"os" "os"
"strings"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
var storageDrivers = []string{"s3", "file"}
// Config is a storage struct for configuration parameters // Config is a storage struct for configuration parameters
type Config struct { type Config struct {
Storage string Storage string
@ -26,7 +32,7 @@ type Config struct {
// Load parses arguments / ENV variable to load configuration // Load parses arguments / ENV variable to load configuration
func Load() *Config { func Load() *Config {
cfg := &Config{} cfg := &Config{}
pflag.StringVar(&cfg.Storage, "storage", "s3", "Storage engine to use (s3, file)") pflag.StringVar(&cfg.Storage, "storage", "s3", fmt.Sprintf("Storage engine to use (%s)", strings.Join(storageDrivers, ", ")))
pflag.StringVar(&cfg.BaseURL, "baseurl", os.Getenv("BASE_URL"), "The Base-URL the application is running on for example https://mondash.org") pflag.StringVar(&cfg.BaseURL, "baseurl", os.Getenv("BASE_URL"), "The Base-URL the application is running on for example https://mondash.org")
pflag.StringVar(&cfg.APIToken, "api-token", os.Getenv("API_TOKEN"), "API Token used for the /welcome dashboard (you can choose your own)") pflag.StringVar(&cfg.APIToken, "api-token", os.Getenv("API_TOKEN"), "API Token used for the /welcome dashboard (you can choose your own)")
pflag.StringVar(&cfg.Listen, "listen", ":3000", "Address to listen on") pflag.StringVar(&cfg.Listen, "listen", ":3000", "Address to listen on")
@ -40,3 +46,32 @@ func Load() *Config {
pflag.Parse() pflag.Parse()
return cfg return cfg
} }
func (c Config) isValid() bool {
// Storage Driver check
validStoragedriver := false
for _, d := range storageDrivers {
if c.Storage == d {
validStoragedriver = true
break
}
}
if !validStoragedriver {
log.Printf("You specified a wrong storage driver: %s\n\n", c.Storage)
return false
}
// Minimum characters of API token
if len(c.APIToken) < 10 {
log.Printf("You need to specify an api-token with more than 9 characters.\n\n")
return false
}
// Base-URL check
if _, err := url.Parse(c.BaseURL); err != nil {
log.Printf("The baseurl '%s' does not look like a valid URL: %s.\n\n", c.BaseURL, err)
return false
}
return true
}