2017-04-14 16:15:07 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-05-22 11:13:47 -06:00
|
|
|
"github.com/docopt/docopt.go"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"sour.is/x/httpsrv"
|
|
|
|
"sour.is/x/log"
|
2017-04-14 16:15:07 -06:00
|
|
|
)
|
|
|
|
|
2017-04-24 09:20:46 -06:00
|
|
|
var (
|
|
|
|
APP_VERSION string
|
2017-05-22 11:13:47 -06:00
|
|
|
APP_BUILD string
|
2017-04-24 09:20:46 -06:00
|
|
|
)
|
2017-04-14 16:15:07 -06:00
|
|
|
var APP_NAME string = "Paste API"
|
|
|
|
var APP_USAGE string = `Paste API
|
|
|
|
|
|
|
|
Usage:
|
2017-04-24 09:20:46 -06:00
|
|
|
paste version
|
|
|
|
paste [ -v | -vv ] serve
|
2017-04-14 16:15:07 -06:00
|
|
|
|
|
|
|
Options:
|
2017-04-24 09:20:46 -06:00
|
|
|
-v Log info to console.
|
|
|
|
-vv Log debug to console.
|
2017-04-14 16:15:07 -06:00
|
|
|
-l <ListenAddress>, --listen=<ListenAddress> Address to listen on.
|
2017-04-24 09:20:46 -06:00
|
|
|
-c <ConfigDir>, --config=<ConfigDir> Set Config Directory.
|
2017-04-14 16:15:07 -06:00
|
|
|
|
|
|
|
Config:
|
|
|
|
The config file is read from the following locations:
|
2017-04-24 09:20:46 -06:00
|
|
|
- <ConfigDir>
|
|
|
|
- /etc/opt/sour.is/paste/
|
2017-04-14 16:15:07 -06:00
|
|
|
- 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)
|
|
|
|
}
|
|
|
|
|
2017-04-24 09:20:46 -06:00
|
|
|
if args["-v"].(int) == 1 {
|
|
|
|
log.SetVerbose(log.Vinfo)
|
|
|
|
}
|
|
|
|
if args["-v"].(int) == 2 {
|
|
|
|
log.SetVerbose(log.Vdebug)
|
|
|
|
log.Debug("Debug Logging.")
|
2017-04-14 16:15:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetConfigName("config")
|
2017-05-22 11:13:47 -06:00
|
|
|
if args["--config"] != nil {
|
2017-04-24 09:20:46 -06:00
|
|
|
viper.AddConfigPath(args["--config"].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AddConfigPath("/etc/opt/sour.is/paste/")
|
2017-04-14 16:15:07 -06:00
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
|
|
viper.SetConfigType("toml")
|
2017-04-24 09:20:46 -06:00
|
|
|
viper.ReadConfig(bytes.NewBuffer([]byte(defaultConfig)))
|
2017-04-14 16:15:07 -06:00
|
|
|
|
|
|
|
err = viper.MergeInConfig()
|
|
|
|
if err != nil { // Handle errors reading the config file
|
2017-04-24 09:20:46 -06:00
|
|
|
log.Fatalf("Fatal error config file: %s \n", err)
|
2017-04-14 16:15:07 -06:00
|
|
|
}
|
|
|
|
|
2017-04-24 09:20:46 -06:00
|
|
|
viper.Set("app.name", APP_NAME)
|
|
|
|
|
|
|
|
viper.SetDefault("app.version", "VERSION")
|
|
|
|
if APP_VERSION != "" {
|
|
|
|
viper.Set("app.version", APP_VERSION)
|
|
|
|
}
|
2017-04-14 16:15:07 -06:00
|
|
|
|
2017-04-24 09:20:46 -06:00
|
|
|
viper.SetDefault("app.build", "SNAPSHOT")
|
|
|
|
if APP_BUILD != "" {
|
|
|
|
viper.Set("app.build", APP_BUILD)
|
|
|
|
}
|
2017-04-14 16:15:07 -06:00
|
|
|
|
|
|
|
if args["serve"] == true {
|
|
|
|
|
|
|
|
if args["--listen"] != nil {
|
|
|
|
viper.Set("listen", args["--listen"].(string))
|
|
|
|
}
|
|
|
|
|
2017-04-24 09:20:46 -06:00
|
|
|
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"))
|
2017-04-14 16:15:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultConfig []byte = []byte(`
|
2017-04-24 09:20:46 -06:00
|
|
|
[http]
|
2017-04-14 16:15:07 -06:00
|
|
|
listen = ":9010"
|
|
|
|
|
|
|
|
[module.paste]
|
|
|
|
random = "4096"
|
|
|
|
store = "data/"
|
2017-05-22 11:13:47 -06:00
|
|
|
`)
|