add webserver route for XHR requests and static files
This commit is contained in:
parent
5e413ddfb8
commit
74e9f5c8df
14
main.go
14
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -24,8 +25,21 @@ func main() {
|
||||||
log.Infof("User: %+v", user)
|
log.Infof("User: %+v", user)
|
||||||
|
|
||||||
mux := gorilla.NewRouter()
|
mux := gorilla.NewRouter()
|
||||||
|
mux.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
|
||||||
mux.HandleFunc("/login", handleTwitchLogin)
|
mux.HandleFunc("/login", handleTwitchLogin)
|
||||||
mux.HandleFunc("/callback", handleTwitchCallback)
|
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) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("Hi!"))
|
w.Write([]byte("Hi!"))
|
||||||
})
|
})
|
||||||
|
|
46
static/collector.js
Executable file
46
static/collector.js
Executable 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
16
static/index.html
Executable 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
20
static/overlay.js
Executable 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
24
user.go
|
@ -17,6 +17,17 @@ var (
|
||||||
user *User
|
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 {
|
type User struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
|
@ -59,6 +70,19 @@ func createUser(token *oauth2.Token) (*User, error) {
|
||||||
return u, nil
|
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 {
|
func getUser(u *User) error {
|
||||||
client := twitchOauthConfig.Client(context.Background(), u.Token)
|
client := twitchOauthConfig.Client(context.Background(), u.Token)
|
||||||
req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/users", nil)
|
req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/users", nil)
|
||||||
|
|
Reference in New Issue
Block a user