refactor(rate limit): relocate addr str manipulation
This commit is contained in:
@@ -3,12 +3,14 @@ package rate_limit
|
|||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TIMEFRAME = 60
|
const TIMEFRAME = 60
|
||||||
|
|
||||||
func TimeLeft(addr string) int64 {
|
func TimeLeft(addr string) int64 {
|
||||||
|
addr = (strings.Split(addr, ":"))[0]
|
||||||
timeLeft := int64(0)
|
timeLeft := int64(0)
|
||||||
file, err := os.OpenFile("addr_table.gob", os.O_RDWR|os.O_CREATE, 0644)
|
file, err := os.OpenFile("addr_table.gob", os.O_RDWR|os.O_CREATE, 0644)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gabdlr/api-cuit-go/rate_limit"
|
"github.com/gabdlr/api-cuit-go/rate_limit"
|
||||||
)
|
)
|
||||||
@@ -21,12 +20,12 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
errorResponse := &CuitError{Error: "Fallo exitosamente"}
|
errorResponse := &CuitError{Error: "Fallo exitosamente"}
|
||||||
argument := r.URL.Path
|
argument := r.URL.Path
|
||||||
|
//fmt.Printf("%v\n", cuit.ValidateWithVerifierDigit(cuit.StandardizeCuit(argument[1:])))
|
||||||
if len(argument) == 1 {
|
if len(argument) == 1 {
|
||||||
errorResponse.Error = NO_SEARCH_ARG
|
errorResponse.Error = NO_SEARCH_ARG
|
||||||
} else {
|
} else {
|
||||||
remoteAddr := (strings.Split(r.RemoteAddr, ":"))[0]
|
argument = argument[1:]
|
||||||
timeLeft := rate_limit.TimeLeft(remoteAddr)
|
timeLeft := rate_limit.TimeLeft(r.RemoteAddr)
|
||||||
|
|
||||||
if timeLeft > 0 {
|
if timeLeft > 0 {
|
||||||
errorResponse.Error = fmt.Sprintf("Recurso no disponible, debe esperar %v segundos", timeLeft)
|
errorResponse.Error = fmt.Sprintf("Recurso no disponible, debe esperar %v segundos", timeLeft)
|
||||||
|
|||||||
Reference in New Issue
Block a user