Browse Source

settings Payment threshold

master
yuriy0803 2 years ago
parent
commit
f710def9cf
  1. 20
      api/server.go
  2. 23
      storage/redis.go
  3. 55
      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())
}
} }

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

@ -1,33 +1,38 @@
<div class="container"> <div class="container">
<div id="settings" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2"> <div id="settings" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="row"> <div class="row">
<div id="alertSuccess" class="alert alert-success" role="alert"> <div id="alertSuccess" class="alert alert-success" role="alert">
Success Success
</div>
<div id="alertError" class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
Error
</div>
</div> </div>
<div id="alertError" class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
Error
</div>
</div>
<form action="/api/settings" method="post"> <form action="/api/settings" method="post">
<div class="form-group"> <div class="form-group">
<label for="threshold">Payment Threshold</label> <input type="hidden" id="login" name="login" value="{{model.login}}">
<input class="form-control" id="threshold" name="threshold" placeholder="min 0.5 - max 10000" min="0.5" max="10000" </div>
type='number' step='1'>
<small id="thresholdHelp" class="form-text text-dark">Payment threshold in {{config.Unit}} (Min: 0.5, Max: 10000)</small>
</div>
<div class="form-group"> <div class="form-group">
<label for="ip_address">Active Worker IP address</label> <label for="threshold">Payment Threshold</label>
<input class="form-control" name="ip_address" id="ip_address" placeholder="192.168.0.1" type="text"> <input class="form-control" id="threshold" name="threshold" placeholder="min 0.5 - max 10000" min="0.5"
<small id="ip_address" class="form-text text-dark"> max="10000" type='number' step='1'>
Please complete your worker`s IP address in order to validate and save your settings. <small id="thresholdHelp" class="form-text text-dark">Payment threshold in {{config.Unit}} (Min: 0.5, Max:
</small> 10000)</small>
</div> </div>
<button type="submit" class="btn btn-primary">Submit</button>
<div class="form-group">
<label for="ip_address">Active Worker IP address</label>
<input class="form-control" name="ip_address" id="ip_address" placeholder="192.168.0.1" type="text">
<small id="ip_address" class="form-text text-dark">
Please complete your worker`s IP address in order to validate and save your settings.
</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form> </form>
</div> </div>
@ -38,17 +43,17 @@
var $form = $('form'); var $form = $('form');
$form.submit(function () { $form.submit(function () {
$.post($(this).attr('action'), $(this).serialize(), function (response) { $.post($(this).attr('action'), $(this).serialize(), function (response) {
console.log(response.result); console.log(response.result);
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