Add endpoint for today's pun
This commit is contained in:
parent
c86a245309
commit
a37a7c8da1
1 changed files with 17 additions and 0 deletions
17
main.go
17
main.go
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
@ -25,6 +26,7 @@ type Error struct {
|
|||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/puns/today", TodayPun)
|
||||
http.HandleFunc("/puns/random", RandomPun)
|
||||
http.HandleFunc("/puns/", ShowPun)
|
||||
http.HandleFunc("/", NotFound)
|
||||
|
@ -63,6 +65,21 @@ func RandomPun(w http.ResponseWriter, r *http.Request) {
|
|||
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 {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue