/
root.go
93 lines (82 loc) · 2.46 KB
/
root.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cmd
import (
"encoding/json"
"fmt"
"log"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tonyalaribe/ninja/core"
"github.com/tonyalaribe/ninja/datalayer"
_ "github.com/tonyalaribe/ninja/datalayer/mongodb"
"github.com/tonyalaribe/ninja/uilayer"
)
var config Config
var rootCmd = &cobra.Command{
Use: "ninja",
Short: "Ninja is a dynamic api engine",
Long: `Ninja lets you build powerful api's(REST, graphql, grpc, etc) for your apps and web applications using a very simple interface.`,
Run: func(cmd *cobra.Command, args []string) {
datastore, err := datalayer.Connect(config.DBConfig.DriverType, config.DBConfig)
if err != nil {
log.Fatalf("Unable to initialize datalayer with error: `%v`", err)
return
}
manager, err := core.New(core.UseDataStore(datastore))
if err != nil {
log.Fatalf("Unable to initialize core with error: `%v`", err)
return
}
uilayer.Register(manager)
},
}
func Execute() {
var cfgFile string
cobra.OnInitialize(initConfig(cfgFile))
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ninja.yaml)")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Config struct {
IsProduction bool `mapstructure:"is_production"`
ShortName string `mapstructure:"short_name"`
LongName string `mapstructure:"long_name"`
DBConfig datalayer.DBConfig `mapstructure:"db_config"`
}
func initConfig(cfgFile string) func() {
return func() {
// Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home and current directory with name ".ninja" (without extension).
viper.AddConfigPath(home)
viper.AddConfigPath(".")
viper.SetConfigName(".ninja")
}
var err error
if err = viper.ReadInConfig(); err != nil {
fmt.Println("Unable to read config:", err)
os.Exit(1)
}
err = viper.Unmarshal(&config)
if err != nil {
log.Panicf("unable to decode into struct, %v", err)
}
if !config.IsProduction {
log.Println("In Development Mode. Logging configuration data:")
indentedConfig, _ := json.MarshalIndent(config, "", "\t")
log.Printf("\n%s\n\n", indentedConfig)
}
}
}