fix: enable rate limit and fix error message
This commit is contained in:
@@ -20,7 +20,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
errorResponse := &CuitError{Error: "Fallo exitosamente"}
|
errorResponse := &CuitError{Error: "ocurrió un error"}
|
||||||
argument := r.URL.Path
|
argument := r.URL.Path
|
||||||
if len(argument) == 1 {
|
if len(argument) == 1 {
|
||||||
errorResponse.Error = NO_SEARCH_ARG
|
errorResponse.Error = NO_SEARCH_ARG
|
||||||
@@ -29,21 +29,22 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
timeLeft := rate_limit.TimeLeft(r.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)
|
||||||
}
|
} else {
|
||||||
if cuit.IsValid(argument) {
|
if cuit.IsValid(argument) {
|
||||||
cRes, cErr := cache.Search(argument)
|
cRes, cErr := cache.Search(argument)
|
||||||
if cErr == nil {
|
if cErr == nil {
|
||||||
w.Write(cRes)
|
w.Write(cRes)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := cuit.Search(argument)
|
res, err := cuit.Search(argument)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cache.Save(argument, res)
|
cache.Save(argument, res)
|
||||||
w.Write(res)
|
w.Write(res)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
errorResponse.Error = err.Error()
|
errorResponse.Error = err.Error()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user