Last active
April 2, 2019 10:29
-
-
Save amirulabu/0d14ae838de2a8d249d997adb44eece7 to your computer and use it in GitHub Desktop.
gowasep - its like wasap.my, but built using go - demo at wasep.mirul.xyz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"net/http" | |
"regexp" | |
) | |
var phoneNumberLink = regexp.MustCompile(`^/\+?\d+$`) | |
func handlerFunc(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/html") | |
if r.URL.Path == "/" { | |
fmt.Fprint(w, "<h1>Welcome to Wasep</h1>"+ | |
"<p>Add your phone number at the back of this link</p>") | |
} else if phoneNumberLink.MatchString(r.URL.Path) { | |
re := regexp.MustCompile(`\+?\d+$`) | |
phoneNumber := fmt.Sprintf("%s", re.FindAllString(r.URL.Path, 1)[0]) | |
wasepURL := "https://api.whatsapp.com/send?phone=" + phoneNumber | |
fmt.Println(phoneNumber, wasepURL) | |
http.Redirect(w, r, wasepURL, 302) | |
} else { | |
http.Error(w, "<h1>404 Error</h1>"+ | |
"<p>Cannot find the page you were looking for</p>", http.StatusNotFound) | |
} | |
} | |
func main() { | |
var port = flag.Int("port", 3003, "Port to use [3003]") | |
flag.Parse() | |
fmt.Println("Serving at port", *port) | |
http.ListenAndServe(fmt.Sprintf(":%d", *port), http.HandlerFunc(handlerFunc)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment