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

64 lines
1.7 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
"log"
"net/url"
2015-07-06 19:41:21 +00:00
"github.com/Luzifer/rconfig"
2015-07-06 19:41:21 +00:00
)
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 `flag:"storage" default:"s3" description:"Storage engine to use"`
BaseURL string `flag:"baseurl" env:"BASE_URL" description:"The Base-URL the application is running on for example https://mondash.org"`
APIToken string `flag:"api-token" env:"API_TOKEN" description:"API Token used for the /welcome dashboard (you can choose your own)"`
2015-07-06 19:41:21 +00:00
Listen string `flag:"listen" default:":3000" description:"Address to listen on"`
2015-07-06 19:41:21 +00:00
S3 struct {
Bucket string `flag:"s3Bucket" env:"S3Bucket" "Bucket to use for S3 storage"`
2015-07-06 19:41:21 +00:00
}
2015-07-06 20:08:27 +00:00
FileStorage struct {
Directory string `flag:"fileDirectory" default:"./" description:"Directory to use for plain text storage"`
2015-07-06 20:08:27 +00:00
}
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{}
rconfig.Parse(cfg)
2015-07-06 19:41:21 +00:00
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
}