Forked from
Stream Server Project / Stream Server
7 commits behind, 1 commit ahead of the upstream repository.
-
Simon Kirsten authoredSimon Kirsten authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
twitch.go 1.36 KiB
package server
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
streamserver "stream-server"
"stream-server/pkg/twitch"
)
// client is the Twitch Client instance
var client *twitch.Client
func init() {
// If you want to use your own twitch client id set the TWITCH_CLIENT_ID environment variable.
if twitchClientID := os.Getenv("TWITCH_CLIENT_ID"); twitchClientID != "" {
client = twitch.NewClient(twitchClientID)
} else {
client = twitch.NewClient(streamserver.DefaultTwitchClientID)
}
}
// twitchHandler proxies request to the helix twitch API
func twitchHandler(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("https://api.twitch.tv/helix/%s", r.URL.RequestURI())
fmt.Printf("url: %s\n", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
req.Header.Add("Client-ID", client.ClientID)
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, body, "", " ")
if error != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
w.Write(prettyJSON.Bytes())
}