1
0
Fork 0

Add endpoint for today's pun

This commit is contained in:
Andrew Tomaka 2016-03-05 12:46:08 -05:00
parent c86a245309
commit a37a7c8da1

17
main.go
View file

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
@ -25,6 +26,7 @@ type Error struct {
} }
func main() { func main() {
http.HandleFunc("/puns/today", TodayPun)
http.HandleFunc("/puns/random", RandomPun) http.HandleFunc("/puns/random", RandomPun)
http.HandleFunc("/puns/", ShowPun) http.HandleFunc("/puns/", ShowPun)
http.HandleFunc("/", NotFound) http.HandleFunc("/", NotFound)
@ -63,6 +65,21 @@ func RandomPun(w http.ResponseWriter, r *http.Request) {
WritePun(w, r, pun) WritePun(w, r, pun)
} }
func TodayPun(w http.ResponseWriter, r *http.Request) {
pun := getPun(PUN_BASE)
pun.Text = stripQuotes(pun.Text)
WritePun(w, r, pun)
}
func stripQuotes(punText string) string {
punText = strings.Replace(punText, "“", "", 1)
punText = strings.Replace(punText, "”", "", 1)
return punText
}
func getPun(url string) Pun { func getPun(url string) Pun {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {