8 "github.com/Sirupsen/logrus"
9 "github.com/codegangsta/cli"
10 "github.com/iotbzh/xds-server/lib/common"
13 // Config parameters (json format) of /config command
15 Version string `json:"version"`
16 APIVersion string `json:"apiVersion"`
17 VersionGitTag string `json:"gitTag"`
18 Builder BuilderConfig `json:"builder"`
19 Folders FoldersConfig `json:"folders"`
21 // Private (un-exported fields in REST GET /config route)
22 FileConf FileConfig `json:"-"`
23 WebAppDir string `json:"-"`
24 HTTPPort string `json:"-"`
25 ShareRootDir string `json:"-"`
26 Log *logrus.Logger `json:"-"`
29 // Config default values
31 DefaultAPIVersion = "1"
33 DefaultShareDir = "/mnt/share"
36 // Init loads the configuration on start-up
37 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
40 // Define default configuration
42 Version: cliCtx.App.Metadata["version"].(string),
43 APIVersion: DefaultAPIVersion,
44 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
45 Builder: BuilderConfig{},
46 Folders: FoldersConfig{},
48 WebAppDir: "webapp/dist",
49 HTTPPort: DefaultPort,
50 ShareRootDir: DefaultShareDir,
54 // config file settings overwrite default config
55 err = updateConfigFromFile(&c, cliCtx.GlobalString("config"))
60 // Update location of shared dir if needed
61 if !common.Exists(c.ShareRootDir) {
62 if err := os.MkdirAll(c.ShareRootDir, 0770); err != nil {
63 return nil, fmt.Errorf("No valid shared directory found: %v", err)
66 c.Log.Infoln("Share root directory: ", c.ShareRootDir)
68 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
69 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
70 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
73 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)