Refit source files to have a public xs-apiv1 lib package.
[src/xds/xds-server.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "io"
6         "os"
7         "path/filepath"
8
9         "github.com/Sirupsen/logrus"
10         "github.com/codegangsta/cli"
11         common "github.com/iotbzh/xds-common/golib"
12         "github.com/iotbzh/xds-server/lib/xsapiv1"
13 )
14
15 // Config parameters (json format) of /config command
16 type Config struct {
17         // Public APIConfig fields
18         xsapiv1.APIConfig
19
20         // Private (un-exported fields in REST GET /config route)
21         Options       Options        `json:"-"`
22         FileConf      FileConfig     `json:"-"`
23         Log           *logrus.Logger `json:"-"`
24         LogVerboseOut io.Writer      `json:"-"`
25 }
26
27 // Options set at the command line
28 type Options struct {
29         ConfigFile     string
30         LogLevel       string
31         LogFile        string
32         NoFolderConfig bool
33 }
34
35 // Config default values
36 const (
37         DefaultAPIVersion = "1"
38         DefaultPort       = "8000"
39         DefaultShareDir   = "${HOME}/.xds-server/projects"
40         DefaultSTHomeDir  = "${HOME}/.xds-server/syncthing-config"
41         DefaultSdkRootDir = "/xdt/sdk"
42 )
43
44 // Init loads the configuration on start-up
45 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
46         var err error
47
48         dfltShareDir := DefaultShareDir
49         dfltSTHomeDir := DefaultSTHomeDir
50         if resDir, err := common.ResolveEnvVar(DefaultShareDir); err == nil {
51                 dfltShareDir = resDir
52         }
53         if resDir, err := common.ResolveEnvVar(DefaultSTHomeDir); err == nil {
54                 dfltSTHomeDir = resDir
55         }
56
57         uuid, err := ServerIDGet()
58         if err != nil {
59                 return nil, err
60         }
61
62         // Define default configuration
63         c := Config{
64                 APIConfig: xsapiv1.APIConfig{
65                         ServerUID:        uuid,
66                         Version:          cliCtx.App.Metadata["version"].(string),
67                         APIVersion:       DefaultAPIVersion,
68                         VersionGitTag:    cliCtx.App.Metadata["git-tag"].(string),
69                         Builder:          xsapiv1.BuilderConfig{},
70                         SupportedSharing: map[string]bool{ /*FIXME USE folder.TypePathMap*/ "PathMap": true},
71                 },
72
73                 Options: Options{
74                         ConfigFile:     cliCtx.GlobalString("config"),
75                         LogLevel:       cliCtx.GlobalString("log"),
76                         LogFile:        cliCtx.GlobalString("logfile"),
77                         NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
78                 },
79                 FileConf: FileConfig{
80                         WebAppDir:    "webapp/dist",
81                         ShareRootDir: dfltShareDir,
82                         SdkRootDir:   DefaultSdkRootDir,
83                         HTTPPort:     DefaultPort,
84                         SThgConf:     &SyncThingConf{Home: dfltSTHomeDir},
85                         LogsDir:      "",
86                 },
87                 Log: log,
88         }
89
90         c.Log.Infoln("Server UUID:          ", uuid)
91
92         // config file settings overwrite default config
93         err = readGlobalConfig(&c, c.Options.ConfigFile)
94         if err != nil {
95                 return nil, err
96         }
97
98         // Update location of shared dir if needed
99         if !common.Exists(c.FileConf.ShareRootDir) {
100                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
101                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
102                 }
103         }
104         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
105
106         // Where Logs are redirected:
107         //  default 'stdout' (logfile option default value)
108         //  else use file (or filepath) set by --logfile option
109         //  that may be overwritten by LogsDir field of config file
110         logF := c.Options.LogFile
111         logD := c.FileConf.LogsDir
112         if logF != "stdout" {
113                 if logD != "" {
114                         lf := filepath.Base(logF)
115                         if lf == "" || lf == "." {
116                                 lf = "xds-server.log"
117                         }
118                         logF = filepath.Join(logD, lf)
119                 } else {
120                         logD = filepath.Dir(logF)
121                 }
122         }
123         if logD == "" || logD == "." {
124                 logD = "/tmp/xds/logs"
125         }
126         c.Options.LogFile = logF
127         c.FileConf.LogsDir = logD
128
129         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
130                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
131                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
132                 }
133         }
134
135         c.Log.Infoln("Logs file:            ", c.Options.LogFile)
136         c.Log.Infoln("Logs directory:       ", c.FileConf.LogsDir)
137
138         return &c, nil
139 }