38 lines
879 B
Go
38 lines
879 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gabdlr/api-cuit-go/rate_limit"
|
|
)
|
|
|
|
type CuitError struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
const NO_SEARCH_ARG = "Sin argumento de búsqueda"
|
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
errorResponse := &CuitError{Error: "Fallo exitosamente"}
|
|
argument := r.URL.Path
|
|
//fmt.Printf("%v\n", cuit.ValidateWithVerifierDigit(cuit.StandardizeCuit(argument[1:])))
|
|
if len(argument) == 1 {
|
|
errorResponse.Error = NO_SEARCH_ARG
|
|
} else {
|
|
argument = argument[1:]
|
|
timeLeft := rate_limit.TimeLeft(r.RemoteAddr)
|
|
|
|
if timeLeft > 0 {
|
|
errorResponse.Error = fmt.Sprintf("Recurso no disponible, debe esperar %v segundos", timeLeft)
|
|
}
|
|
}
|
|
|
|
jsonResponse, _ := json.Marshal(errorResponse)
|
|
w.Write(jsonResponse)
|
|
}
|