diff --git a/main.go b/main.go index df5efd4..e18aeb8 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "net/http" "time" @@ -24,8 +25,21 @@ func main() { log.Infof("User: %+v", user) mux := gorilla.NewRouter() + mux.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) mux.HandleFunc("/login", handleTwitchLogin) mux.HandleFunc("/callback", handleTwitchCallback) + mux.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) { + if user == nil { + http.NotFound(w, r) + return + } + w.Header().Set("Access-Control-Allow-Headers", "*") + if err := json.NewEncoder(w).Encode(user.getWebUser()); err != nil { + log.WithError(err).Error("Unable to encode user data") + http.Error(w, "Unable to encode user data", http.StatusInternalServerError) + return + } + }) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hi!")) }) diff --git a/static/collector.js b/static/collector.js new file mode 100755 index 0000000..184ae69 --- /dev/null +++ b/static/collector.js @@ -0,0 +1,46 @@ +var twitchCollector = { + updateHooks: [], + data: null, + + + update: function(data) { + this.data = data; + var that = this; + this.updateHooks.forEach(function(v) { + v(that.data); + }); + }, + + registerHook: function(kind, callback) { + switch (kind) { + case "update": + this.updateHooks.push(callback); + break; + } + }, + + start: function() { + this.getData(); + var that = this; + window.setInterval(function() { + that.getData() + }, 20000); + }, + + getData: function() { + let xmlhttp = new XMLHttpRequest(); + let url = "/data"; + var that = this; + + xmlhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + let data = JSON.parse(this.responseText); + if (data !== null) { + that.update(data); + } + } + }; + xmlhttp.open("GET", url, true); + xmlhttp.send(); + } +} \ No newline at end of file diff --git a/static/index.html b/static/index.html new file mode 100755 index 0000000..732dcfb --- /dev/null +++ b/static/index.html @@ -0,0 +1,16 @@ + + + + Overlay + + + +
+
+
+ + + + + + \ No newline at end of file diff --git a/static/overlay.js b/static/overlay.js new file mode 100755 index 0000000..70a6975 --- /dev/null +++ b/static/overlay.js @@ -0,0 +1,20 @@ +function showData(data) { + if (typeof data === "undefined" || data === null) { + console.error("Data is empty"); + return; + } + + if (typeof data.Followers === "undefined" || data.Followers === null) { + console.error("No followers data found"); + return; + } + + let followers = document.getElementById("followers"); + followers.innerHTML = data.Followers.total; +} + +let collector = Object.create(twitchCollector); +collector.registerHook("update", function(data) { + showData(data); +}); +collector.start(); \ No newline at end of file diff --git a/user.go b/user.go index f923910..aa2e005 100644 --- a/user.go +++ b/user.go @@ -17,6 +17,17 @@ var ( user *User ) +type WebUser struct { + ID string + Name string + DisplayName string + TwitchUser *TwitchUser `json:"User"` + TwitchChannel *TwitchChannel `json:"Channel"` + TwitchFollowers *TwitchFollowers `json:"Followers"` + TwitchSubscriptions *TwitchSubscriptions `json:"Subscriptions"` + TwitchStream *TwitchStream `json:"Stream"` +} + type User struct { ID string Name string @@ -59,6 +70,19 @@ func createUser(token *oauth2.Token) (*User, error) { return u, nil } +func (u *User) getWebUser() *WebUser { + return &WebUser{ + ID: u.ID, + Name: u.Name, + DisplayName: u.DisplayName, + TwitchUser: u.TwitchUser, + TwitchChannel: u.TwitchChannel, + TwitchFollowers: u.TwitchFollowers, + TwitchSubscriptions: u.TwitchSubscriptions, + TwitchStream: u.TwitchStream, + } +} + func getUser(u *User) error { client := twitchOauthConfig.Client(context.Background(), u.Token) req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/users", nil)