Skip to content

Instantly share code, notes, and snippets.

@timcole
Created December 7, 2018 16:36
Show Gist options
  • Save timcole/1668d227550e924034251a3ff5f2411f to your computer and use it in GitHub Desktop.
Save timcole/1668d227550e924034251a3ff5f2411f to your computer and use it in GitHub Desktop.
package chatters
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"fmt"
)
func ProxyChatters(w http.ResponseWriter, r *http.Request) {
channel := r.URL.Query().Get("c")
if channel == "" {
must(w, fmt.Errorf("no query param"))
}
req, err := http.NewRequest("GET", "https://tmi.twitch.tv/group/user/" + string(channel) + "/chatters", nil)
must(w, err)
client := &http.Client{}
res, err := client.Do(req)
must(w, err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
must(w, err)
var out bytes.Buffer
err = json.Indent(&out, body, "", "\t")
must(w, err)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(200)
w.Write(out.Bytes())
}
func must(w http.ResponseWriter, err error) {
if err != nil {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(500)
w.Write([]byte("failed: " + err.Error()))
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment