-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (41 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type ValidationResult struct {
CardNumber string `json:"card_number"`
IsValid bool `json:"is_valid"`
}
func validateCreditCardHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is supported", http.StatusMethodNotAllowed)
return
}
var input map[string]string
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
cardNumber, exists := input["card_number"]
if !exists {
http.Error(w, "Missing card_number field", http.StatusBadRequest)
return
}
isValid := luhnAlgorithm(cardNumber)
response := ValidationResult{
CardNumber: cardNumber,
IsValid: isValid,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
http.HandleFunc("/validate", validateCreditCardHandler)
http.Handle("/", http.FileServer(http.Dir("./static")))
fmt.Println("Server is running on port 8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Failed to start server:", err)
}
}