Add SDKs support.
[src/xds/xds-server.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5
6         "os"
7
8         "github.com/Sirupsen/logrus"
9         "github.com/codegangsta/cli"
10         "github.com/iotbzh/xds-server/lib/common"
11 )
12
13 // Config parameters (json format) of /config command
14 type Config struct {
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"`
20
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:"-"`
27 }
28
29 // Config default values
30 const (
31         DefaultAPIVersion = "1"
32         DefaultPort       = "8000"
33         DefaultShareDir   = "/mnt/share"
34 )
35
36 // Init loads the configuration on start-up
37 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
38         var err error
39
40         // Define default configuration
41         c := Config{
42                 Version:       cliCtx.App.Metadata["version"].(string),
43                 APIVersion:    DefaultAPIVersion,
44                 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
45                 Builder:       BuilderConfig{},
46                 Folders:       FoldersConfig{},
47
48                 WebAppDir:    "webapp/dist",
49                 HTTPPort:     DefaultPort,
50                 ShareRootDir: DefaultShareDir,
51                 Log:          log,
52         }
53
54         // config file settings overwrite default config
55         err = updateConfigFromFile(&c, cliCtx.GlobalString("config"))
56         if err != nil {
57                 return nil, err
58         }
59
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)
64                 }
65         }
66         c.Log.Infoln("Share root directory: ", c.ShareRootDir)
67
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)
71                 }
72         }
73         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
74
75         return &c, nil
76 }