aboutsummaryrefslogtreecommitdiffstats
path: root/golang/cmd/fatcatd/root.go
blob: b5263b06f22d6f61f8662443b72c1f9932fff761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

package main

import (
	"os"
	"fmt"

    log "github.com/sirupsen/logrus"
    "github.com/spf13/viper"
    "github.com/spf13/cobra"
    "github.com/getsentry/raven-go"
)

var cfgFile = "";

var rootCmd = &cobra.Command{
    Use:   "fatcatd",
    Short: "REST API Server",
	Long:  "A scalable, versioned, API-oriented catalog of bibliographic entities and file metadata",
    Version: "0.1.0",
}

func init() {
    cobra.OnInitialize(initConfig)
    rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./fatcatd.toml)")
    rootCmd.PersistentFlags().BoolP("verbose", "v", false, "increase logging volume")
    viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))

    serveCmd.Flags().String("db-url", "postgres://", "database connecion string")

    rootCmd.AddCommand(serveCmd)
}

func initConfig() {

    viper.SetDefault("port", 9411)
    viper.SetDefault("db_url", "postgres://bnewbold@localhost/fatcat")
    viper.SetEnvPrefix("FATCAT")
    viper.AutomaticEnv()

    if cfgFile != "" {
        // Use config file from the flag.
        viper.SetConfigFile(cfgFile)
    } else {
        viper.SetConfigType("toml")
        viper.AddConfigPath(".")
        viper.SetConfigName("fatcatd")
    }

    err := viper.ReadInConfig()
    if err != nil {
        log.Fatalf("Error loading config: %s \n", err)
    }

    // not default of stderr
    log.SetOutput(os.Stdout);

    if viper.GetBool("verbose") == true {
        log.SetLevel(log.DebugLevel)
    }
    log.Debug("It's verbose!")

    raven.SetDSN(viper.GetString("sentry_dsn"));

}

func Execute() {
    if err := rootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}