-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (81 loc) · 2.49 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
90
91
92
93
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/chasekaylee/gawkbox-mobile/twitch"
)
/*
1) search for live creators based on username
GET - with query as body
/api/search
2) deliever top 10 featured streamers when req made
GET
/api/featured
*/
func main() {
fmt.Println("Booting the server...")
// Set Client ID required for Twitch API header
twitch.SetID("io3r29b0slg7j9qf2woudfzdqshhmh")
// Initial Route
http.HandleFunc("/", handleHome)
// Search Route
http.HandleFunc("/api/search", handleSearch)
// Featured Route
http.HandleFunc("/api/featured", handleFeatured)
// Run your server
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// HandleHome =>
func handleHome(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Welcome to GawkBox!")
}
// HandleSearch => A search handler function for the route /search
// expects a query string (username) from client
// on success returns stream information for user + 200 status code
// if Streamer is offline "Stream is currently offline" is returned + 404 status code
func handleSearch(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
fmt.Println("Recieved the following request:", query.Get("query"))
// on success returns streamers channel ID
t, err := twitch.GetStreamer(query.Get("query"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// on success returns stream information for specific user
us, err := twitch.GetUserStream(t.ID)
if err == nil {
s, err := json.Marshal(us)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(s)
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
// HandleFeatured => A featured handler function for the route /featured for HTTP server
// on success returns stream slice of information for top 10 featured streams + 200 status code
// on failure returns error + 500 status code
func handleFeatured(w http.ResponseWriter, r *http.Request) {
// on success returns 10 streams
f, err := twitch.GetFeaturedStreams()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fs, err := json.Marshal(f)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(fs)
}