-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (69 loc) · 1.38 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
const portNumber string = ":8000"
func home(w http.ResponseWriter, r *http.Request) {
log.Println("hello")
switch r.Method {
case "GET":
fmt.Fprint(w, "Hello world from yedu")
case "POST":
d := json.NewDecoder(r.Body)
c := struct {
Card *string `json:"card"`
}{}
err := d.Decode(&c)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(*c.Card) == 16 {
la := luhnAlgorithm(*c.Card)
if la {
fmt.Fprint(w, "The card is valid ")
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
}
} else {
http.Error(w, "Invalid card", http.StatusBadRequest)
}
default:
fmt.Fprint(w, "does not exist")
}
}
func luhnAlgorithm(cn string) bool {
log.Println("Running algorithm")
digits := []int{}
for i := 0; i < len(cn); i++ {
digits = append(digits, int(cn[i]-'0'))
}
for i := len(digits) - 2; i >= 0; i -= 2 {
digits[i] = digits[i] * 2
if digits[i] > 9 {
digits[i] -= 9
}
}
sum := 0
for i := 0; i < len(digits); i++ {
sum = sum + digits[i]
}
if sum%10 == 0 {
return true
} else {
return false
}
}
func main() {
log.Println("Running programme")
mux := http.NewServeMux()
mux.HandleFunc("/", home)
err := http.ListenAndServe(portNumber, mux)
if err != nil {
log.Println("An error happened", err)
log.Fatal(err)
}
}