From f710def9cf6a7ec3b1683b7a772f9bcdb293d658 Mon Sep 17 00:00:00 2001 From: yuriy0803 <68668177+yuriy0803@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:51:06 +0100 Subject: [PATCH] settings Payment threshold --- api/server.go | 20 +++++----- storage/redis.go | 23 ++++++----- www/app/templates/account/settings.hbs | 55 ++++++++++++++------------ 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/api/server.go b/api/server.go index 43be17e..2eef8cb 100644 --- a/api/server.go +++ b/api/server.go @@ -487,7 +487,7 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) reply := make(map[string]interface{}) - reply["result"] = "IP address doesn`t match" + reply["result"] = "IP address doesn't match" var ipAddress = r.FormValue("ip_address") var login = r.FormValue("login") @@ -496,15 +496,18 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) { threshold = "0.5" } - alert := "off" - if r.FormValue("alertCheck") != "" { - alert = r.FormValue("alertCheck") - } + // Log-Ausgabe für den Login-Wert + log.Printf("Received login from client: %s", login) - ip_address := s.backend.GetIP(login) + // Log-Ausgabe für den IP-Adressen-Vergleich + log.Printf("Received IP address from client: %s", ipAddress) - if ip_address == ipAddress { + // Überprüfung des Login-Werts in der Redis-Datenbank + ipFromRedis := s.backend.GetIP(login) + log.Printf("IP address from Redis for login %s: %s", login, ipFromRedis) + // Überprüfung, ob die IP-Adresse übereinstimmt + if ipFromRedis == ipAddress { s.backend.SetIP(login, ipAddress) number, err := strconv.ParseFloat(threshold, 64) @@ -514,8 +517,6 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) { shannon := float64(1000000000) s.backend.SetThreshold(login, int64(number*shannon)) - s.backend.SetAlert(login, alert) - reply["result"] = "success" } @@ -523,5 +524,4 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println("Error serializing API response: ", err) } - } diff --git a/storage/redis.go b/storage/redis.go index ab8baa5..2fe0b7f 100644 --- a/storage/redis.go +++ b/storage/redis.go @@ -1950,34 +1950,39 @@ func (r *RedisClient) SetThreshold(login string, threshold int64) (bool, error) return cmd, err } func (r *RedisClient) LogIP(login string, ip string) { - + login = strings.ToLower(login) r.client.HSet(r.formatKey("settings", login), "ip_address", ip) - r.client.HSet(r.formatKey("settings", login), "status", "online") ms := util.MakeTimestamp() ts := ms / 1000 r.client.HSet(r.formatKey("settings", login), "ip_time", strconv.FormatInt(ts, 10)) - } func (r *RedisClient) GetIP(login string) string { login = strings.ToLower(login) cmd := r.client.HGet(r.formatKey("settings", login), "ip_address") + + log.Printf("Getting IP for login %s. Result: %v", login, cmd.Val()) + if cmd.Err() == redis.Nil { + log.Printf("IP not found for login: %s", login) return "NA" } else if cmd.Err() != nil { + log.Printf("Error retrieving IP for login %s: %v", login, cmd.Err()) return "NA" } + return cmd.Val() } func (r *RedisClient) SetIP(login string, ip string) { login = strings.ToLower(login) - r.client.HSet(r.formatKey("settings", login), "ip_address", ip) -} + key := r.formatKey("settings", login) // Überprüfen Sie das genaue Format des Schlüssels -func (r *RedisClient) SetAlert(login string, alert string) (bool, error) { - login = strings.ToLower(login) - cmd, err := r.client.HSet(r.formatKey("settings", login), "alert", alert).Result() - return cmd, err + log.Printf("Setting IP address %s for login %s", ip, login) + + cmd := r.client.HSet(key, "ip_address", ip) + if cmd.Err() != nil { + log.Printf("Error setting IP address for login %s: %v", login, cmd.Err()) + } } diff --git a/www/app/templates/account/settings.hbs b/www/app/templates/account/settings.hbs index 139c93b..457c03c 100644 --- a/www/app/templates/account/settings.hbs +++ b/www/app/templates/account/settings.hbs @@ -1,33 +1,38 @@
- - + + +
-
- - - Payment threshold in {{config.Unit}} (Min: 0.5, Max: 10000) -
+
+ +
-
- - - - Please complete your worker`s IP address in order to validate and save your settings. - -
- +
+ + + Payment threshold in {{config.Unit}} (Min: 0.5, Max: + 10000) +
+ +
+ + + + Please complete your worker`s IP address in order to validate and save your settings. + +
+
@@ -38,17 +43,17 @@ var $form = $('form'); $form.submit(function () { $.post($(this).attr('action'), $(this).serialize(), function (response) { - console.log(response.result); + console.log(response.result); if (response.result == "success") { $('#alertSuccess').html(response.result); $("#alertSuccess").fadeIn(); //or fadeIn - setTimeout(function() { + setTimeout(function () { $("#alertSuccess").fadeOut(); //or fadeOut }, 5000); } else { $('#alertError').html(response.result); $("#alertError").fadeIn(); //or fadeIn - setTimeout(function() { + setTimeout(function () { $("#alertError").fadeOut(); //or fadeOut }, 5000); }