-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
39 lines (30 loc) · 816 Bytes
/
api.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
var quote = NewQuote()
func randomAPI(w http.ResponseWriter, r *http.Request) {
total, err := strconv.Atoi(queryValue(r, "total"))
if err != nil || total <= 0 || total > 10{
total = 1
}
j, _ := json.Marshal(quote.random(total))
fmt.Fprintln(w, string(j))
}
func topicNameAPI(w http.ResponseWriter, r *http.Request) {
n := mux.Vars(r)["topic"]
j, _ := json.Marshal(quote.topicByName(n))
fmt.Fprintln(w, string(j))
}
func findByIDAPI(w http.ResponseWriter, r *http.Request) {
j, _ := json.Marshal(quote.quoteByID(queryValue(r, "id")))
fmt.Fprintln(w, string(j))
}
func dailyQuoteAPI(w http.ResponseWriter, r *http.Request) {
j, _ := json.Marshal(quote.quoteOfTheDay())
fmt.Fprintln(w, string(j))
}