diff options
Diffstat (limited to 'src/dkv')
-rw-r--r-- | src/dkv/api/backendPropertiesConnection.go | 62 | ||||
-rw-r--r-- | src/dkv/api/backendfakes.go | 14 | ||||
-rw-r--r-- | src/dkv/api/configHandlers.go | 10 | ||||
-rw-r--r-- | src/dkv/api/initialise.go | 2 | ||||
-rw-r--r-- | src/dkv/api/registrationHandlers.go | 2 |
5 files changed, 44 insertions, 46 deletions
diff --git a/src/dkv/api/backendPropertiesConnection.go b/src/dkv/api/backendPropertiesConnection.go index 24140cd..d485b40 100644 --- a/src/dkv/api/backendPropertiesConnection.go +++ b/src/dkv/api/backendPropertiesConnection.go @@ -22,32 +22,28 @@ import ( "io/ioutil" "log" "os" - "sync" ) type KeyValuesInterface interface { - WriteKVsToConsul(string, string) error - ConfigReader(string, string, string) error - ReadMultiplePropertiesRecursive(string) error - ReadMultipleProperties(string) error - ReadProperty(string) error + WriteKVsToConsul(string, string, map[string]string) error + ConfigReader(string, string, string) (map[string]string, error) + ReadMultiplePropertiesRecursive(string, *map[string]string) error + ReadMultipleProperties(string, *map[string]string) error + ReadProperty(string, *map[string]string) error } -type KeyValuesStruct struct { - sync.RWMutex - kvs map[string]string -} +type KeyValuesStruct struct{} var KeyValues KeyValuesInterface -func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string) error { +func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { var prefix = "" if subdomain != "" { prefix += token + "/" + subdomain } else { prefix += token + "/" } - for key, value := range kvStruct.kvs { + for key, value := range kvs { key = prefix + key err := Consul.RequestPUT(key, value) if err != nil { @@ -59,50 +55,48 @@ func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string return nil } -func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) error { - defer kvStruct.Unlock() - - kvStruct.Lock() +func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { var filepath = MOUNTPATH + kvs := make(map[string]string) if filename != "" && subdomain != "" { // Specific file in specific domain. filepath += token + "/" + subdomain + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename != "" && subdomain == "" { // Specific file in Token filepath += token + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename == "" && subdomain != "" { // All files in specific domain filepath += token + "/" + subdomain - err := kvStruct.ReadMultipleProperties(filepath) + err := kvStruct.ReadMultipleProperties(filepath, &kvs) if err != nil { - return err + return kvs, err } } filepath += token - err := kvStruct.ReadMultiplePropertiesRecursive(filepath) + err := kvStruct.ReadMultiplePropertiesRecursive(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } -func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string, kvs *map[string]string) error { // Go inside each sub directory and run ReadMultipleProperties inside. files, err := ioutil.ReadDir(path) if err != nil { @@ -112,35 +106,35 @@ func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) er for _, f := range files { fi, _ := os.Stat(path + "/" + f.Name()) if fi.Mode().IsDir() { - kvStruct.ReadMultipleProperties(path + "/" + f.Name()) + kvStruct.ReadMultipleProperties(path+"/"+f.Name(), kvs) } else { - kvStruct.ReadProperty(path + "/" + f.Name()) + kvStruct.ReadProperty(path+"/"+f.Name(), kvs) } } return nil } -func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string, kvs *map[string]string) error { files, err := ioutil.ReadDir(path) if err != nil { return err } for _, f := range files { - kvStruct.ReadProperty(path + f.Name()) + kvStruct.ReadProperty(path+f.Name(), kvs) } return nil } -func (kvStruct *KeyValuesStruct) ReadProperty(path string) error { +func (kvStruct *KeyValuesStruct) ReadProperty(path string, kvs *map[string]string) error { _, err := os.Stat(path) if err != nil { return errors.New("File does not exists.") } p := properties.MustLoadFile(path, properties.UTF8) for _, key := range p.Keys() { - kvStruct.kvs[key] = p.MustGet(key) + (*kvs)[key] = p.MustGet(key) } return nil } diff --git a/src/dkv/api/backendfakes.go b/src/dkv/api/backendfakes.go index 5415608..c5ca39a 100644 --- a/src/dkv/api/backendfakes.go +++ b/src/dkv/api/backendfakes.go @@ -73,11 +73,12 @@ type FakeKeyValues struct { KeyValuesStruct } -func (f *FakeKeyValues) ConfigReader(token string, subdomain string, filename string) error { - return nil +func (f *FakeKeyValues) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { + kvs := make(map[string]string) + return kvs, nil } -func (f *FakeKeyValues) WriteKVsToConsul(token string, subdomain string) error { +func (f *FakeKeyValues) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { return nil } @@ -86,11 +87,12 @@ type FakeKeyValuesErr struct { KeyValuesStruct } -func (f *FakeKeyValuesErr) ConfigReader(token string, subdomain string, filename string) error { - return errors.New("Internal Server Error") +func (f *FakeKeyValuesErr) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { + kvs := make(map[string]string) + return kvs, errors.New("Internal Server Error") } -func (f *FakeKeyValuesErr) WriteKVsToConsul(token string, subdomain string) error { +func (f *FakeKeyValuesErr) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { return errors.New("Internal Server Error") } diff --git a/src/dkv/api/configHandlers.go b/src/dkv/api/configHandlers.go index f5bac34..7178433 100644 --- a/src/dkv/api/configHandlers.go +++ b/src/dkv/api/configHandlers.go @@ -81,6 +81,8 @@ func HandleConfigUpload(w http.ResponseWriter, r *http.Request) { } defer f.Close() io.Copy(f, file) + + GenerateResponse(w, r, http.StatusOK, "Configuration uploaded to Token: "+token) } func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { @@ -102,14 +104,14 @@ func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { return } - err = KeyValues.ConfigReader(body.Token, body.Subdomain, body.Filename) + kvs_map, err := KeyValues.ConfigReader(body.Token, body.Subdomain, body.Filename) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) return } - err = KeyValues.WriteKVsToConsul(body.Token, body.Subdomain) + err = KeyValues.WriteKVsToConsul(body.Token, body.Subdomain, kvs_map) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) @@ -119,12 +121,12 @@ func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { } func HandleDefaultConfigLoad(w http.ResponseWriter, r *http.Request) { - err := KeyValues.ConfigReader("default", "", "") + kvs_map, err := KeyValues.ConfigReader("default", "", "") if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) return } - err = KeyValues.WriteKVsToConsul("default", "") + err = KeyValues.WriteKVsToConsul("default", "", kvs_map) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) } else { diff --git a/src/dkv/api/initialise.go b/src/dkv/api/initialise.go index dfbcbde..f4edc6c 100644 --- a/src/dkv/api/initialise.go +++ b/src/dkv/api/initialise.go @@ -20,7 +20,7 @@ import "os" func Initialise() error { Consul = &ConsulStruct{} - KeyValues = &KeyValuesStruct{kvs: make(map[string]string)} + KeyValues = &KeyValuesStruct{} Directory = &DirectoryStruct{directory: ""} err := Consul.InitializeConsulClient() diff --git a/src/dkv/api/registrationHandlers.go b/src/dkv/api/registrationHandlers.go index 1c50b33..0f697db 100644 --- a/src/dkv/api/registrationHandlers.go +++ b/src/dkv/api/registrationHandlers.go @@ -91,7 +91,7 @@ func HandleServiceGet(w http.ResponseWriter, r *http.Request) { if found == true { GenerateResponse(w, r, http.StatusOK, service) } else { - GenerateResponse(w, r, http.StatusNotFound, "Service for Token:"+token+"not found.") + GenerateResponse(w, r, http.StatusNotFound, "Service for Token: "+token+" not found.") } } |