46 lines
940 B
Go
46 lines
940 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.tertyshy.dev/cards/validation"
|
|
)
|
|
|
|
type ValidatePayload struct {
|
|
Pan string `json:"pan"`
|
|
Month string `json:"month"`
|
|
Year string `json:"year"`
|
|
}
|
|
|
|
type ValidationResponse struct {
|
|
Status int `json:"status"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func validate(w http.ResponseWriter, r *http.Request) {
|
|
var payload ValidatePayload
|
|
var response ValidationResponse
|
|
decoder := json.NewDecoder(r.Body)
|
|
err := decoder.Decode(&payload)
|
|
|
|
if err != nil {
|
|
response = ValidationResponse{
|
|
-1,
|
|
err.Error(),
|
|
}
|
|
} else {
|
|
result := validation.Validate(validation.Card{payload.Pan, payload.Month, payload.Year})
|
|
response = ValidationResponse{result.Code, result.Error.Error()}
|
|
}
|
|
|
|
// todo: correct response type and code
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("POST /validate", validate)
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|