go-paste/config.go

112 lines
2.3 KiB
Go

package main
import (
"github.com/spf13/viper"
"github.com/docopt/docopt.go"
"sour.is/x/log"
"sour.is/x/httpsrv"
"bytes"
"fmt"
)
var (
APP_VERSION string
APP_BUILD string
)
var APP_NAME string = "Paste API"
var APP_USAGE string = `Paste API
Usage:
paste version
paste [ -v | -vv ] serve
Options:
-v Log info to console.
-vv Log debug to console.
-l <ListenAddress>, --listen=<ListenAddress> Address to listen on.
-c <ConfigDir>, --config=<ConfigDir> Set Config Directory.
Config:
The config file is read from the following locations:
- <ConfigDir>
- /etc/opt/sour.is/paste/
- Working Directory
`
var args map[string]interface{}
func init() {
var err error
if args, err = docopt.Parse(APP_USAGE, nil, true, APP_NAME, false); err != nil {
log.Fatal(err)
}
if args["-v"].(int) == 1 {
log.SetVerbose(log.Vinfo)
}
if args["-v"].(int) == 2 {
log.SetVerbose(log.Vdebug)
log.Debug("Debug Logging.")
}
viper.SetConfigName("config")
if args["--config"] != nil{
viper.AddConfigPath(args["--config"].(string))
}
viper.AddConfigPath("/etc/opt/sour.is/paste/")
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.ReadConfig(bytes.NewBuffer([]byte(defaultConfig)))
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s \n", err)
}
viper.Set("app.name", APP_NAME)
viper.SetDefault("app.version", "VERSION")
if APP_VERSION != "" {
viper.Set("app.version", APP_VERSION)
}
viper.SetDefault("app.build", "SNAPSHOT")
if APP_BUILD != "" {
viper.Set("app.build", APP_BUILD)
}
if args["serve"] == true {
if args["--listen"] != nil {
viper.Set("listen", args["--listen"].(string))
}
log.Noticef("Startup: %s (%s %s)",
viper.GetString("app.name"),
viper.GetString("app.version"),
viper.GetString("app.build"))
log.Notice("Read config from: ", viper.ConfigFileUsed())
if viper.IsSet("http") {
httpsrv.Config()
}
} else if args["version"] == true {
fmt.Printf("Version: %s (%s %s)\n",
viper.GetString("app.name"),
viper.GetString("app.version"),
viper.GetString("app.build"))
}
}
var defaultConfig []byte = []byte(`
[http]
listen = ":9010"
[module.paste]
random = "4096"
store = "data/"
`)