11 common "github.com/iotbzh/xds-common/golib"
15 // ConfigDir Directory in user HOME directory where xds config will be saved
16 ConfigDir = ".xds-server"
17 // GlobalConfigFilename Global config filename
18 GlobalConfigFilename = "config.json"
19 // ServerDataFilename Server data filename
20 ServerDataFilename = "server-data.xml"
21 // FoldersConfigFilename Folders config filename
22 FoldersConfigFilename = "server-config_folders.xml"
25 // SyncThingConf definition
26 type SyncThingConf struct {
27 BinDir string `json:"binDir"`
28 Home string `json:"home"`
29 GuiAddress string `json:"gui-address"`
30 GuiAPIKey string `json:"gui-apikey"`
31 RescanIntervalS int `json:"rescanIntervalS"`
34 // FileConfig is the JSON structure of xds-server config file (config.json)
35 type FileConfig struct {
36 WebAppDir string `json:"webAppDir"`
37 ShareRootDir string `json:"shareRootDir"`
38 SdkRootDir string `json:"sdkRootDir"`
39 HTTPPort string `json:"httpPort"`
40 SThgConf *SyncThingConf `json:"syncthing"`
41 LogsDir string `json:"logsDir"`
44 // readGlobalConfig reads configuration from a config file.
45 // Order to determine which config file is used:
46 // 1/ from command line option: "--config myConfig.json"
47 // 2/ $HOME/.xds-server/config.json file
48 // 3/ /etc/xds-server/config.json file
49 // 4/ <xds-server executable dir>/config.json file
50 func readGlobalConfig(c *Config, confFile string) error {
52 searchIn := make([]string, 0, 3)
54 searchIn = append(searchIn, confFile)
56 if usr, err := user.Current(); err == nil {
57 searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir,
58 GlobalConfigFilename))
61 searchIn = append(searchIn, "/etc/xds-server/config.json")
64 ee, _ := os.Executable()
65 exeAbsPath, err := filepath.Abs(ee)
67 exePath, err = filepath.EvalSymlinks(exeAbsPath)
69 exePath = filepath.Dir(ee)
71 exePath = filepath.Dir(exeAbsPath)
74 searchIn = append(searchIn, path.Join(exePath, "config.json"))
77 for _, p := range searchIn {
78 if _, err := os.Stat(p); err == nil {
84 // No config file found
87 c.Log.Infof("Use config file: %s", *cFile)
89 // TODO move on viper package to support comments in JSON and also
90 // bind with flags (command line options)
91 // see https://github.com/spf13/viper#working-with-flags
92 fd, _ := os.Open(*cFile)
95 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
99 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
105 if fCfg.SThgConf != nil {
106 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
108 for _, field := range vars {
110 if *field, err = common.ResolveEnvVar(*field); err != nil {
115 // Use config file settings else use default config
116 if fCfg.WebAppDir == "" {
117 fCfg.WebAppDir = c.FileConf.WebAppDir
119 if fCfg.ShareRootDir == "" {
120 fCfg.ShareRootDir = c.FileConf.ShareRootDir
122 if fCfg.SdkRootDir == "" {
123 fCfg.SdkRootDir = c.FileConf.SdkRootDir
125 if fCfg.HTTPPort == "" {
126 fCfg.HTTPPort = c.FileConf.HTTPPort
128 if fCfg.LogsDir == "" {
129 fCfg.LogsDir = c.FileConf.LogsDir
132 // Resolve webapp dir (support relative or full path)
133 fCfg.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
134 if !strings.HasPrefix(fCfg.WebAppDir, "/") && exePath != "" {
137 // Check first from current directory
138 for _, rootD := range []string{exePath, cwd} {
139 ff := path.Join(rootD, fCfg.WebAppDir, "index.html")
140 if common.Exists(ff) {
141 fCfg.WebAppDir = path.Join(rootD, fCfg.WebAppDir)
151 func configFilenameGet(cfgFile string) (string, error) {
152 usr, err := user.Current()
156 return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil
159 // FoldersConfigFilenameGet
160 func FoldersConfigFilenameGet() (string, error) {
161 return configFilenameGet(FoldersConfigFilename)
164 // ServerDataFilenameGet
165 func ServerDataFilenameGet() (string, error) {
166 return configFilenameGet(ServerDataFilename)