Add API KEY support to allow CORS requests.
[src/xds/xds-agent.git] / lib / xdsconfig / fileconfig.go
1 package xdsconfig
2
3 import (
4         "encoding/json"
5         "os"
6         "os/user"
7         "path"
8         "path/filepath"
9
10         "github.com/iotbzh/xds-agent/lib/common"
11 )
12
13 type SyncThingConf struct {
14         BinDir     string `json:"binDir"`
15         Home       string `json:"home"`
16         GuiAddress string `json:"gui-address"`
17         GuiAPIKey  string `json:"gui-apikey"`
18 }
19
20 type FileConfig struct {
21         HTTPPort  string         `json:"httpPort"`
22         LogsDir   string         `json:"logsDir"`
23         XDSAPIKey string         `json:"xds-apikey"`
24         SThgConf  *SyncThingConf `json:"syncthing"`
25 }
26
27 // getConfigFromFile reads configuration from a config file.
28 // Order to determine which config file is used:
29 //  1/ from command line option: "--config myConfig.json"
30 //  2/ $HOME/.xds/agent-config.json file
31 //  3/ <current_dir>/agent-config.json file
32 //  4/ <executable dir>/agent-config.json file
33
34 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
35
36         searchIn := make([]string, 0, 3)
37         if confFile != "" {
38                 searchIn = append(searchIn, confFile)
39         }
40         if usr, err := user.Current(); err == nil {
41                 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent-config.json"))
42         }
43         cwd, err := os.Getwd()
44         if err == nil {
45                 searchIn = append(searchIn, path.Join(cwd, "agent-config.json"))
46         }
47         exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
48         if err == nil {
49                 searchIn = append(searchIn, path.Join(exePath, "agent-config.json"))
50         }
51
52         var cFile *string
53         for _, p := range searchIn {
54                 if _, err := os.Stat(p); err == nil {
55                         cFile = &p
56                         break
57                 }
58         }
59         fCfg := FileConfig{}
60         if cFile == nil {
61                 // No config file found
62                 return &fCfg, nil
63         }
64
65         c.Log.Infof("Use config file: %s", *cFile)
66
67         // TODO move on viper package to support comments in JSON and also
68         // bind with flags (command line options)
69         // see https://github.com/spf13/viper#working-with-flags
70
71         fd, _ := os.Open(*cFile)
72         defer fd.Close()
73         if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
74                 return nil, err
75         }
76
77         // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
78         for _, field := range []*string{
79                 &fCfg.LogsDir,
80                 &fCfg.SThgConf.Home,
81                 &fCfg.SThgConf.BinDir} {
82
83                 var err error
84                 *field, err = common.ResolveEnvVar(*field)
85                 if err != nil {
86                         return nil, err
87                 }
88         }
89
90         // Config file settings overwrite default config
91         if fCfg.HTTPPort != "" {
92                 c.HTTPPort = fCfg.HTTPPort
93         }
94
95         return &fCfg, nil
96 }