Browse Source

settings Payment threshold

master
yuriy0803 2 years ago
parent
commit
f710def9cf
  1. 20
      api/server.go
  2. 23
      storage/redis.go
  3. 15
      www/app/templates/account/settings.hbs

20
api/server.go

@ -487,7 +487,7 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
reply := make(map[string]interface{}) 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 ipAddress = r.FormValue("ip_address")
var login = r.FormValue("login") var login = r.FormValue("login")
@ -496,15 +496,18 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
threshold = "0.5" threshold = "0.5"
} }
alert := "off" // Log-Ausgabe für den Login-Wert
if r.FormValue("alertCheck") != "" { log.Printf("Received login from client: %s", login)
alert = r.FormValue("alertCheck")
}
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) s.backend.SetIP(login, ipAddress)
number, err := strconv.ParseFloat(threshold, 64) number, err := strconv.ParseFloat(threshold, 64)
@ -514,8 +517,6 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
shannon := float64(1000000000) shannon := float64(1000000000)
s.backend.SetThreshold(login, int64(number*shannon)) s.backend.SetThreshold(login, int64(number*shannon))
s.backend.SetAlert(login, alert)
reply["result"] = "success" reply["result"] = "success"
} }
@ -523,5 +524,4 @@ func (s *ApiServer) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Println("Error serializing API response: ", err) log.Println("Error serializing API response: ", err)
} }
} }

23
storage/redis.go

@ -1950,34 +1950,39 @@ func (r *RedisClient) SetThreshold(login string, threshold int64) (bool, error)
return cmd, err return cmd, err
} }
func (r *RedisClient) LogIP(login string, ip string) { 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), "ip_address", ip)
r.client.HSet(r.formatKey("settings", login), "status", "online")
ms := util.MakeTimestamp() ms := util.MakeTimestamp()
ts := ms / 1000 ts := ms / 1000
r.client.HSet(r.formatKey("settings", login), "ip_time", strconv.FormatInt(ts, 10)) r.client.HSet(r.formatKey("settings", login), "ip_time", strconv.FormatInt(ts, 10))
} }
func (r *RedisClient) GetIP(login string) string { func (r *RedisClient) GetIP(login string) string {
login = strings.ToLower(login) login = strings.ToLower(login)
cmd := r.client.HGet(r.formatKey("settings", login), "ip_address") 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 { if cmd.Err() == redis.Nil {
log.Printf("IP not found for login: %s", login)
return "NA" return "NA"
} else if cmd.Err() != nil { } else if cmd.Err() != nil {
log.Printf("Error retrieving IP for login %s: %v", login, cmd.Err())
return "NA" return "NA"
} }
return cmd.Val() return cmd.Val()
} }
func (r *RedisClient) SetIP(login string, ip string) { func (r *RedisClient) SetIP(login string, ip string) {
login = strings.ToLower(login) 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) { log.Printf("Setting IP address %s for login %s", ip, login)
login = strings.ToLower(login)
cmd, err := r.client.HSet(r.formatKey("settings", login), "alert", alert).Result() cmd := r.client.HSet(key, "ip_address", ip)
return cmd, err if cmd.Err() != nil {
log.Printf("Error setting IP address for login %s: %v", login, cmd.Err())
}
} }

15
www/app/templates/account/settings.hbs

@ -13,11 +13,16 @@
<form action="/api/settings" method="post"> <form action="/api/settings" method="post">
<div class="form-group">
<input type="hidden" id="login" name="login" value="{{model.login}}">
</div>
<div class="form-group"> <div class="form-group">
<label for="threshold">Payment Threshold</label> <label for="threshold">Payment Threshold</label>
<input class="form-control" id="threshold" name="threshold" placeholder="min 0.5 - max 10000" min="0.5" max="10000" <input class="form-control" id="threshold" name="threshold" placeholder="min 0.5 - max 10000" min="0.5"
type='number' step='1'> max="10000" type='number' step='1'>
<small id="thresholdHelp" class="form-text text-dark">Payment threshold in {{config.Unit}} (Min: 0.5, Max: 10000)</small> <small id="thresholdHelp" class="form-text text-dark">Payment threshold in {{config.Unit}} (Min: 0.5, Max:
10000)</small>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -42,13 +47,13 @@
if (response.result == "success") { if (response.result == "success") {
$('#alertSuccess').html(response.result); $('#alertSuccess').html(response.result);
$("#alertSuccess").fadeIn(); //or fadeIn $("#alertSuccess").fadeIn(); //or fadeIn
setTimeout(function() { setTimeout(function () {
$("#alertSuccess").fadeOut(); //or fadeOut $("#alertSuccess").fadeOut(); //or fadeOut
}, 5000); }, 5000);
} else { } else {
$('#alertError').html(response.result); $('#alertError').html(response.result);
$("#alertError").fadeIn(); //or fadeIn $("#alertError").fadeIn(); //or fadeIn
setTimeout(function() { setTimeout(function () {
$("#alertError").fadeOut(); //or fadeOut $("#alertError").fadeOut(); //or fadeOut
}, 5000); }, 5000);
} }

Loading…
Cancel
Save