mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
Attempts to save file in golang to no avail
This commit is contained in:
13
config.go
13
config.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -48,6 +49,9 @@ type SMSGlobal struct {
|
||||
Enabled bool
|
||||
}
|
||||
}
|
||||
type ConfigPost struct {
|
||||
Data Config `json:"Data"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Name string
|
||||
@@ -198,6 +202,7 @@ func ReadConfig() (Config, error) {
|
||||
}
|
||||
|
||||
func SaveConfig() error {
|
||||
log.Println("Saving config")
|
||||
payload, err := json.MarshalIndent(bot.config, "", " ")
|
||||
|
||||
if err != nil {
|
||||
@@ -209,6 +214,14 @@ func SaveConfig() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
retrieved, err := ioutil.ReadFile(CONFIG_FILE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !bytes.Equal(retrieved, payload) {
|
||||
return fmt.Errorf("file %q content doesn't match, read %s expected %s\n", CONFIG_FILE, retrieved, payload)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -15,10 +16,44 @@ func getAllSettings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func saveAllSettings(w http.ResponseWriter, r *http.Request) {
|
||||
//Get the data from the request
|
||||
log.Println(r.Body)
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var responseData ConfigPost
|
||||
jsonerr := decoder.Decode(&responseData)
|
||||
if jsonerr != nil {
|
||||
log.Println(jsonerr)
|
||||
panic(jsonerr)
|
||||
}
|
||||
//Save change the settings
|
||||
for _, exch := range bot.config.Exchanges {
|
||||
for i := 0; i < len(responseData.Data.Exchanges); i++ {
|
||||
if responseData.Data.Exchanges[i].Name == exch.Name {
|
||||
log.Println("Looking at exchange " + exch.Name)
|
||||
log.Println("Enabled %s", responseData.Data.Exchanges[i].Enabled)
|
||||
log.Println("Key " + responseData.Data.Exchanges[i].APIKey)
|
||||
exch.Enabled = responseData.Data.Exchanges[i].Enabled
|
||||
exch.APIKey = responseData.Data.Exchanges[i].APIKey
|
||||
exch.APISecret = responseData.Data.Exchanges[i].APISecret
|
||||
exch.EnabledPairs = responseData.Data.Exchanges[i].EnabledPairs
|
||||
}
|
||||
}
|
||||
}
|
||||
//Reload the configuration
|
||||
err := SaveConfig()
|
||||
if err != nil {
|
||||
log.Println("Fatal error checking config values. Error:", err)
|
||||
return
|
||||
}
|
||||
bot.config, err = ReadConfig()
|
||||
if err != nil {
|
||||
log.Println("Fatal error checking config values. Error:", err)
|
||||
return
|
||||
}
|
||||
//Return response status
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(true); err != nil {
|
||||
if err := json.NewEncoder(w).Encode(bot.config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,12 @@ $scope.saveAllSettings = function() {
|
||||
}).
|
||||
success(function (data) {
|
||||
Notification.success('Saved settings');
|
||||
for(var i=0; i<data.Exchanges.length;i++) {
|
||||
data.Exchanges[i].AvailablePairsSplit = data.Exchanges[i].AvailablePairs.split(",");
|
||||
data.Exchanges[i].EnabledPairsSplit = data.Exchanges[i].EnabledPairs.split(",");
|
||||
}
|
||||
$scope.config = data;
|
||||
Notification.info('Settings loaded');
|
||||
}).
|
||||
error(function (data) {
|
||||
Notification.error('Save failed');
|
||||
|
||||
@@ -40,11 +40,11 @@ app.get('/config/all', function (req, res) {
|
||||
|
||||
app.post('/config/all/save', function(req, res) {
|
||||
requestify.post('http://localhost:9050/config/all/save', {
|
||||
data: req.body
|
||||
Data: req.body
|
||||
})
|
||||
.then(function(response) {
|
||||
console.log(response);
|
||||
res.send(response);
|
||||
res.send(response.body);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user