...
Source file
src/discovery/api/configuration.go
1
18
19 package api
20
21 import (
22 config2 "discovery/config"
23 "discovery/db"
24 "discovery/errors"
25 "discovery/log"
26 "encoding/json"
27 "io"
28 "io/ioutil"
29 "net/http"
30 )
31
32 func ConfigurationGet(w http.ResponseWriter, r *http.Request) {
33
34 if config2.GetMachineIp() == "" {
35 errors.ReplyWithError(w, errors.ConfigurationNotReady)
36 return
37 }
38
39 config, err := json.Marshal(config2.GetConfigurationDynamicCopy())
40 if err != nil {
41 log.Log.Errorf("Cannot encode configuration to json")
42 errors.ReplyWithError(w, errors.GenericError)
43 return
44 }
45
46 w.Header().Set("Content-Type", "application/json")
47 io.WriteString(w, string(config))
48 }
49
50 func ConfigurationSet(w http.ResponseWriter, r *http.Request) {
51 currentConfiguration := config2.GetConfigurationDynamicCopy()
52 reqBody, _ := ioutil.ReadAll(r.Body)
53
54 var newConfiguration *config2.ConfigurationDynamic
55 var err error
56
57
58 err = json.Unmarshal(reqBody, ¤tConfiguration)
59 newConfiguration = currentConfiguration
60 if err != nil {
61 log.Log.Errorf("Cannot encode passed configuration")
62 errors.ReplyWithError(w, errors.GenericError)
63 return
64 }
65
66 config2.SetMachineId(newConfiguration.MachineId)
67 config2.SetMachineIp(newConfiguration.MachineIp)
68 config2.SetMachineFogNetId(newConfiguration.MachineGroupName)
69 config2.SetInitServers(newConfiguration.InitServers)
70 db.AddInitServers(newConfiguration.InitServers)
71
72
73 err = config2.SaveConfigurationDynamicToConfigFile()
74 if err != nil {
75 log.Log.Warningf("Cannot save configuration to file %s", config2.GetConfigurationDynamicFilePath())
76 w.WriteHeader(500)
77 return
78 }
79
80 log.Log.Infof("Configuration updated")
81
82 w.WriteHeader(200)
83 }
84
View as plain text