add webserver route for XHR requests and static files

This commit is contained in:
Martin Thielecke 2020-05-12 00:56:44 +02:00
parent 5e413ddfb8
commit 74e9f5c8df
Signed by: mthie
GPG Key ID: D1D25A85C8604DFB
5 changed files with 120 additions and 0 deletions

14
main.go
View File

@ -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!"))
})

46
static/collector.js Executable file
View File

@ -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();
}
}

16
static/index.html Executable file
View File

@ -0,0 +1,16 @@
<html>
<head>
<title>Overlay</title>
</head>
<body>
<div class="container">
<div id="followers"></div>
</div>
<script src="/static/collector.js"></script>
<script src="/static/overlay.js"></script>
</script>
</body>
</html>

20
static/overlay.js Executable file
View File

@ -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();

24
user.go
View File

@ -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)