Memory leak counter measure test and web ui bug fix + improvements
This commit is contained in:
@@ -39,6 +39,9 @@ var (
|
||||
|
||||
// mutex for checking if voice connection already exists
|
||||
mutex = &sync.Mutex{}
|
||||
|
||||
// connection refresher counter
|
||||
rcCounter int = 0
|
||||
)
|
||||
|
||||
// Play represents an individual use of the !airhorn command
|
||||
@@ -61,7 +64,6 @@ type SoundCollection struct {
|
||||
Commands []string
|
||||
Sounds []*Sound
|
||||
ChainWith *SoundCollection
|
||||
|
||||
soundRange int
|
||||
}
|
||||
|
||||
@@ -351,8 +353,8 @@ func playSound(play *Play, vc *discordgo.VoiceConnection) (err error) {
|
||||
time.Sleep(time.Millisecond * time.Duration(play.Sound.PartDelay))
|
||||
mutex.Lock()
|
||||
delete(queues, play.GuildID)
|
||||
mutex.Unlock()
|
||||
vc.Disconnect()
|
||||
mutex.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -458,7 +460,35 @@ func setIdleStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
func checkReconnectCounter() {
|
||||
mutex.Lock()
|
||||
if rcCounter >= 20 {
|
||||
reconnect()
|
||||
rcCounter = 0
|
||||
} else {
|
||||
rcCounter++
|
||||
}
|
||||
mutex.Unlock()
|
||||
}
|
||||
|
||||
func reconnect() {
|
||||
err := discord.Close()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"error": err,
|
||||
}).Error("Failed to close connection.")
|
||||
return
|
||||
}
|
||||
err = discord.Open()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"error": err,
|
||||
}).Fatal("Failed to create discord websocket connection")
|
||||
}
|
||||
}
|
||||
|
||||
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
go checkReconnectCounter()
|
||||
if m.Content == "ping" || m.Content == "pong" {
|
||||
// If the message is "ping" reply with "Pong!"
|
||||
if m.Content == "ping" {
|
||||
|
||||
@@ -15,10 +15,11 @@
|
||||
body {
|
||||
padding-top: 50px;
|
||||
}
|
||||
.vcenter {
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
float: none;
|
||||
.itembuffer {
|
||||
margin: 1px;
|
||||
padding: 5px;
|
||||
border: 1px solid black;
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
</style>
|
||||
<meta charset="utf-8">
|
||||
@@ -55,5 +56,3 @@
|
||||
<h1>Gidbig</h1>
|
||||
<p class="lead">{{ template "sessionHandler" . }}</p>
|
||||
{{ end }}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<div class="col-sm-3 text-center"><div class="itembuffer">{{ .Itemtext }}<br/><a class="btn btn-default" href="javascript:playSound('{{ .Itemcommand }}', '{{ .Itemsoundname }}')" role="button">Play!</a></div></div>
|
||||
@@ -1,5 +0,0 @@
|
||||
<div class="row">
|
||||
{{ range . }}
|
||||
<div class="col-sm-3 text-center">{{ .Itemtext }}<br/><a class="btn btn-primary" href="javascript:playSound('{{ .Itemcommand }}', '{{ .Itemsoundname }}')" role="button">Play!</a></div>
|
||||
{{ end }}
|
||||
</div>
|
||||
@@ -0,0 +1 @@
|
||||
</div>
|
||||
@@ -0,0 +1 @@
|
||||
<div class="row">
|
||||
+29
-18
@@ -48,7 +48,9 @@ type SoundItem struct {
|
||||
func startWebServer(port string, ci string, cs string, redirectURL string) {
|
||||
tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", header, footer))
|
||||
tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", header, footer))
|
||||
tmpls["itemrow.html"] = template.Must(template.ParseFiles(templateDir + "itemrow.html"))
|
||||
tmpls["item.html"] = template.Must(template.ParseFiles(templateDir + "item.html"))
|
||||
tmpls["itemrowstart.html"] = template.Must(template.ParseFiles(templateDir + "itemrowstart.html"))
|
||||
tmpls["itemrowend.html"] = template.Must(template.ParseFiles(templateDir + "itemrowend.html"))
|
||||
store = sessions.NewCookieStore([]byte(cs))
|
||||
discordOauthConfig.ClientID = ci
|
||||
discordOauthConfig.ClientSecret = cs
|
||||
@@ -67,7 +69,7 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
|
||||
func handlePlaySound(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname"))
|
||||
session, _ := store.Get(r, "session-name")
|
||||
session, _ := store.Get(r, "gidbig-session")
|
||||
var guild *discordgo.Guild
|
||||
user, _ := discord.User(session.Values["discordUserID"].(string))
|
||||
for _, g := range discord.State.Guilds {
|
||||
@@ -83,36 +85,45 @@ func handlePlaySound(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleMain(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := store.Get(r, "session-name")
|
||||
session, _ := store.Get(r, "gidbig-session")
|
||||
if session.Values["discordUsername"] != nil {
|
||||
err := tmpls["internal.html"].ExecuteTemplate(w, "header", map[string]interface{}{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
var si [4]SoundItem
|
||||
cnt := 0
|
||||
|
||||
var si []SoundItem
|
||||
for _, sc := range COLLECTIONS {
|
||||
for _, snd := range sc.Sounds {
|
||||
si[cnt%4].Itemcommand = "!" + sc.Prefix
|
||||
si[cnt%4].Itemsoundname = snd.Name
|
||||
si[cnt%4].Itemtext = si[cnt%4].Itemcommand + " " + si[cnt%4].Itemsoundname
|
||||
if cnt != 0 && cnt%4 == 3 {
|
||||
err = tmpls["itemrow.html"].Execute(w, si)
|
||||
si = append(si, SoundItem{
|
||||
Itemcommand: "!" + sc.Prefix,
|
||||
Itemsoundname: snd.Name,
|
||||
Itemtext: "!" + sc.Prefix + " " + snd.Name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for i, snd := range si {
|
||||
if i%4 == 0 {
|
||||
err = tmpls["itemrowstart.html"].Execute(w, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
if cnt%4 != 0 {
|
||||
err = tmpls["itemrow.html"].Execute(w, si)
|
||||
err = tmpls["item.html"].Execute(w, snd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if i%4 == 3 {
|
||||
err = tmpls["itemrowend.html"].Execute(w, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = tmpls["internal.html"].ExecuteTemplate(w, "footer", map[string]interface{}{})
|
||||
@@ -127,7 +138,7 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
cookie := &http.Cookie{
|
||||
Name: "session-name",
|
||||
Name: "gidbig-session",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
@@ -140,7 +151,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
|
||||
rand.Read(b)
|
||||
oauthStateString = base64.URLEncoding.EncodeToString(b)
|
||||
|
||||
session, _ := store.Get(r, "session-name")
|
||||
session, _ := store.Get(r, "gidbig-session")
|
||||
session.Values["state"] = oauthStateString
|
||||
session.Save(r, w)
|
||||
|
||||
@@ -149,7 +160,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := store.Get(r, "session-name")
|
||||
session, err := store.Get(r, "gidbig-session")
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, "aborted")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user