1
0
mirror of https://github.com/Luzifer/mondash.git synced 2024-09-20 01:12:58 +00:00
mondash/config/config.go

78 lines
1.9 KiB
Go
Raw Normal View History

2015-07-06 19:41:21 +00:00
package config // import "github.com/Luzifer/mondash/config"
import (
2015-07-06 21:23:07 +00:00
"fmt"
"log"
"net/url"
2015-07-06 19:41:21 +00:00
"os"
2015-07-06 21:23:07 +00:00
"strings"
2015-07-06 19:41:21 +00:00
"github.com/spf13/pflag"
)
2015-07-06 21:23:07 +00:00
var storageDrivers = []string{"s3", "file"}
2015-07-06 19:44:13 +00:00
// Config is a storage struct for configuration parameters
2015-07-06 19:41:21 +00:00
type Config struct {
Storage string
BaseURL string
APIToken string
Listen string
2015-07-06 19:41:21 +00:00
S3 struct {
Bucket string
}
2015-07-06 20:08:27 +00:00
FileStorage struct {
Directory string
}
2015-07-06 19:41:21 +00:00
}
2015-07-06 19:44:13 +00:00
// Load parses arguments / ENV variable to load configuration
2015-07-06 19:41:21 +00:00
func Load() *Config {
cfg := &Config{}
2015-07-06 21:23:07 +00:00
pflag.StringVar(&cfg.Storage, "storage", "s3", fmt.Sprintf("Storage engine to use (%s)", strings.Join(storageDrivers, ", ")))
2015-07-06 19:41:21 +00:00
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.Listen, "listen", ":3000", "Address to listen on")
2015-07-06 19:41:21 +00:00
// S3
pflag.StringVar(&cfg.S3.Bucket, "s3Bucket", os.Getenv("S3Bucket"), "Bucket to use for S3 storage")
2015-07-06 20:08:27 +00:00
// FileStorage
pflag.StringVar(&cfg.FileStorage.Directory, "fileDirectory", "./", "Directory to use for plain text storage")
2015-07-06 19:41:21 +00:00
pflag.Parse()
return cfg
}
2015-07-06 21:23:07 +00:00
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
}