mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-03 08:28:31 +00:00
Add /all_topics endpoint
Temporarily not protected with sealing, we'll see how the performance is first.
This commit is contained in:
parent
b4f4a65a68
commit
b108177942
3 changed files with 35 additions and 0 deletions
|
@ -103,6 +103,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||||
serveMux.HandleFunc("/uncached_pub", HTTPBackendUncachedPublish)
|
serveMux.HandleFunc("/uncached_pub", HTTPBackendUncachedPublish)
|
||||||
serveMux.HandleFunc("/cached_pub", HTTPBackendCachedPublish)
|
serveMux.HandleFunc("/cached_pub", HTTPBackendCachedPublish)
|
||||||
serveMux.HandleFunc("/get_sub_count", HTTPGetSubscriberCount)
|
serveMux.HandleFunc("/get_sub_count", HTTPGetSubscriberCount)
|
||||||
|
serveMux.HandleFunc("/all_topics", HTTPListAllTopics)
|
||||||
|
|
||||||
announceForm, err := Backend.secureForm.Seal(url.Values{
|
announceForm, err := Backend.secureForm.Seal(url.Values{
|
||||||
"startup": []string{"1"},
|
"startup": []string{"1"},
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/rate"
|
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/rate"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/sync/singleflight"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LastSavedMessage contains a reply to a command along with an expiration time.
|
// LastSavedMessage contains a reply to a command along with an expiration time.
|
||||||
|
@ -25,6 +27,8 @@ type LastSavedMessage struct {
|
||||||
var CachedLastMessages = make(map[Command]map[string]LastSavedMessage)
|
var CachedLastMessages = make(map[Command]map[string]LastSavedMessage)
|
||||||
var CachedLSMLock sync.RWMutex
|
var CachedLSMLock sync.RWMutex
|
||||||
|
|
||||||
|
var singleFlighter singleflight.Group
|
||||||
|
|
||||||
func cachedMessageJanitor() {
|
func cachedMessageJanitor() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(1 * time.Hour)
|
time.Sleep(1 * time.Hour)
|
||||||
|
@ -303,3 +307,19 @@ func HTTPGetSubscriberCount(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
fmt.Fprint(w, CountSubscriptions(strings.Split(channel, ",")))
|
fmt.Fprint(w, CountSubscriptions(strings.Split(channel, ",")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HTTPListAllTopics(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseForm()
|
||||||
|
_, err := Backend.secureForm.Unseal(r.Form)
|
||||||
|
if err != nil {
|
||||||
|
//w.WriteHeader(403)
|
||||||
|
//fmt.Fprintf(w, "Error: %v", err)
|
||||||
|
//return
|
||||||
|
}
|
||||||
|
|
||||||
|
topicList, _, _ := singleFlighter.Do("/all_topics", func() (interface{}, error) {
|
||||||
|
return GetAllTopics(), nil
|
||||||
|
})
|
||||||
|
w.WriteHeader(200)
|
||||||
|
json.NewEncoder(w).Encode(topicList)
|
||||||
|
}
|
||||||
|
|
|
@ -47,6 +47,20 @@ func CountSubscriptions(channels []string) int {
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAllTopics() []string {
|
||||||
|
ChatSubscriptionLock.RLock()
|
||||||
|
defer ChatSubscriptionLock.RUnlock()
|
||||||
|
|
||||||
|
count := len(ChatSubscriptionInfo)
|
||||||
|
list := make([]string, count)
|
||||||
|
i := 0
|
||||||
|
for topicName := range ChatSubscriptionInfo {
|
||||||
|
list[i] = topicName
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
func SubscribeChannel(client *ClientInfo, channelName string) {
|
func SubscribeChannel(client *ClientInfo, channelName string) {
|
||||||
ChatSubscriptionLock.RLock()
|
ChatSubscriptionLock.RLock()
|
||||||
_subscribeWhileRlocked(channelName, client)
|
_subscribeWhileRlocked(channelName, client)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue