/
main.go
89 lines (73 loc) · 2.36 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
87
88
89
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"strings"
)
var tpl *template.Template
func init(){
tpl = template.Must(template.ParseGlob("templates/*.html",))
}
func wame(w http.ResponseWriter, r *http.Request){
tpl.ExecuteTemplate(w, "index.html", nil)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
} // this was for simple deployment on heroku
fs := http.FileServer(http.Dir("public"))
http.Handle("/public/", http.StripPrefix("/public/", fs))
http.HandleFunc("/", wame)
http.HandleFunc("/walink", walink)
http.ListenAndServe(":"+port, nil)
}
func walink(w http.ResponseWriter, r *http.Request){
if r.Method != "POST" {
http.Redirect(w, r, "/", http.StatusSeeOther )
return
}
countrycode := r.FormValue("cid")
phonenum := r.FormValue("numbs") // takes the value from the form
v := strings.Fields(``+phonenum+``) //split numbers into new lines
var WameNum []string //declared a global variable
switch countrycode {
case "https://wa.me/+234":
for _, number := range v {
l := number[1:]
WameNum = append(WameNum, l)
}
case "https://wa.me/+1":
for _, number := range v {
l := number[2:]
WameNum = append(WameNum, l)
}
case "https://wa.me/+44":
for _, number := range v {
l := number[3:]
WameNum = append(WameNum, l)
}
} // a switch case to check the country code to decide the number format stripping, so it runs a range over the numbers and strips the required things off based on the rules
var inform string //declared a global variable
switch countrycode {
case "https://wa.me/+234":
inform = fmt.Sprint("Your Nigerian WaMe Links are ready!!!")
case "https://wa.me/+1":
inform = fmt.Sprint("Your USA WaMe Links are ready!!!")
case "https://wa.me/+44":
inform = fmt.Sprint("Your UK WaMe Links are ready!!!")
} //this switch case is to check the country code to decide the message to be display for the user based on the country they have picked
d := struct {
CountryMessage string
Country string
Number [] string
}{
CountryMessage: inform,
Country: countrycode,
Number: WameNum,
}
// a struct to hold the data and pass them to the template to be deisplayed
tpl.ExecuteTemplate(w, "walink.html", d)
}