Add folder interface and support native pathmap folder type.
[src/xds/xds-server.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "os"
6
7         "github.com/Sirupsen/logrus"
8         "github.com/codegangsta/cli"
9         common "github.com/iotbzh/xds-common/golib"
10 )
11
12 // Config parameters (json format) of /config command
13 type Config struct {
14         Version       string        `json:"version"`
15         APIVersion    string        `json:"apiVersion"`
16         VersionGitTag string        `json:"gitTag"`
17         Builder       BuilderConfig `json:"builder"`
18
19         // Private (un-exported fields in REST GET /config route)
20         Options  Options        `json:"-"`
21         FileConf FileConfig     `json:"-"`
22         Log      *logrus.Logger `json:"-"`
23 }
24
25 // Options set at the command line
26 type Options struct {
27         ConfigFile     string
28         LogLevel       string
29         LogFile        string
30         NoFolderConfig bool
31 }
32
33 // Config default values
34 const (
35         DefaultAPIVersion = "1"
36         DefaultPort       = "8000"
37         DefaultShareDir   = "/mnt/share"
38         DefaultSdkRootDir = "/xdt/sdk"
39 )
40
41 // Init loads the configuration on start-up
42 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
43         var err error
44
45         // Define default configuration
46         c := Config{
47                 Version:       cliCtx.App.Metadata["version"].(string),
48                 APIVersion:    DefaultAPIVersion,
49                 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
50                 Builder:       BuilderConfig{},
51
52                 Options: Options{
53                         ConfigFile:     cliCtx.GlobalString("config"),
54                         LogLevel:       cliCtx.GlobalString("log"),
55                         LogFile:        cliCtx.GlobalString("logfile"),
56                         NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
57                 },
58                 FileConf: FileConfig{
59                         WebAppDir:    "webapp/dist",
60                         ShareRootDir: DefaultShareDir,
61                         SdkRootDir:   DefaultSdkRootDir,
62                         HTTPPort:     DefaultPort,
63                 },
64                 Log: log,
65         }
66
67         // config file settings overwrite default config
68         err = readGlobalConfig(&c, c.Options.ConfigFile)
69         if err != nil {
70                 return nil, err
71         }
72
73         // Update location of shared dir if needed
74         if !common.Exists(c.FileConf.ShareRootDir) {
75                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
76                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
77                 }
78         }
79         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
80
81         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
82                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
83                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
84                 }
85         }
86         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
87
88         return &c, nil
89 }