1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 15:27:43 +00:00

convert httpClient

This commit is contained in:
Kane York 2016-06-02 08:36:02 -07:00
parent cd7faaba38
commit dd9e66c80d
4 changed files with 20 additions and 21 deletions

View file

@ -40,7 +40,6 @@ type backendInfo struct {
var Backend *backendInfo
var backendHTTPClient http.Client
var backendURL string
var responseCache *cache.Cache
@ -57,7 +56,7 @@ func setupBackend(config *ConfigFile) *backendInfo {
Backend = b
b.serverID = config.ServerID
backendHTTPClient.Timeout = 60 * time.Second
b.HTTPClient.Timeout = 60 * time.Second
backendURL = config.BackendURL
if responseCache != nil {
responseCache.Flush()
@ -156,19 +155,19 @@ func (bfe ErrForwardedFromBackend) Error() string {
var ErrAuthorizationNeeded = errors.New("Must authenticate Twitch username to use this command")
// SendRemoteCommandCached performs a RPC call on the backend, but caches responses.
func SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
cached, ok := responseCache.Get(getCacheKey(remoteCommand, data))
if ok {
return cached.(string), nil
}
return SendRemoteCommand(remoteCommand, data, auth)
return backend.SendRemoteCommand(remoteCommand, data, auth)
}
// SendRemoteCommand performs a RPC call on the backend by POSTing to `/cmd/$remoteCommand`.
// The form data is as follows: `clientData` is the JSON in the `data` parameter
// (should be retrieved from ClientMessage.Arguments), and either `username` or
// `usernameClaimed` depending on whether AuthInfo.UsernameValidates is true is AuthInfo.TwitchUsername.
func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
func (backend *backendInfo) SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
destURL := fmt.Sprintf("%s/cmd/%s", backendURL, remoteCommand)
formData := url.Values{
@ -187,7 +186,7 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
return "", err
}
resp, err := backendHTTPClient.PostForm(destURL, sealedForm)
resp, err := backend.HTTPClient.PostForm(destURL, sealedForm)
if err != nil {
return "", err
}
@ -229,8 +228,8 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
}
// SendAggregatedData sends aggregated emote usage and following data to the backend server.
func SendAggregatedData(sealedForm url.Values) error {
resp, err := backendHTTPClient.PostForm(postStatisticsURL, sealedForm)
func (backend *backendInfo) SendAggregatedData(sealedForm url.Values) error {
resp, err := backend.HTTPClient.PostForm(postStatisticsURL, sealedForm)
if err != nil {
return err
}
@ -259,19 +258,19 @@ func (noe ErrBackendNotOK) Error() string {
// POST data:
// channels=room.trihex
// added=t
func SendNewTopicNotice(topic string) error {
return sendTopicNotice(topic, true)
func (backend *backendInfo) SendNewTopicNotice(topic string) error {
return backend.sendTopicNotice(topic, true)
}
// SendCleanupTopicsNotice notifies the backend that pub/sub topics have no subscribers anymore.
// POST data:
// channels=room.sirstendec,room.bobross,feature.foo
// added=f
func SendCleanupTopicsNotice(topics []string) error {
return sendTopicNotice(strings.Join(topics, ","), false)
func (backend *backendInfo) SendCleanupTopicsNotice(topics []string) error {
return backend.sendTopicNotice(strings.Join(topics, ","), false)
}
func sendTopicNotice(topic string, added bool) error {
func (backend *backendInfo) sendTopicNotice(topic string, added bool) error {
formData := url.Values{}
formData.Set("channels", topic)
if added {
@ -285,7 +284,7 @@ func sendTopicNotice(topic string, added bool) error {
return err
}
resp, err := backendHTTPClient.PostForm(addTopicURL, sealedForm)
resp, err := backend.HTTPClient.PostForm(addTopicURL, sealedForm)
if err != nil {
return err
}

View file

@ -396,7 +396,7 @@ func aggregateDataSender_do() {
return
}
err = SendAggregatedData(form)
err = Backend.SendAggregatedData(form)
if err != nil {
log.Println("error reporting aggregate data:", err)
return
@ -533,7 +533,7 @@ func C2SHandleBunchedCommand(conn *websocket.Conn, client *ClientInfo, msg Clien
pendingBunchedRequests[br] = &bunchSubscriberList{Members: []bunchSubscriber{{Client: client, MessageID: msg.MessageID}}}
go func(request bunchedRequest) {
respStr, err := SendRemoteCommandCached(string(request.Command), request.Param, AuthInfo{})
respStr, err := Backend.SendRemoteCommandCached(string(request.Command), request.Param, AuthInfo{})
var msg ClientMessage
if err == nil {
@ -581,7 +581,7 @@ const AuthorizationFailedErrorString = "Failed to verify your Twitch username."
const AuthorizationNeededError = "You must be signed in to use that command."
func doRemoteCommand(conn *websocket.Conn, msg ClientMessage, client *ClientInfo) {
resp, err := SendRemoteCommandCached(string(msg.Command), copyString(msg.origArguments), client.AuthInfo)
resp, err := Backend.SendRemoteCommandCached(string(msg.Command), copyString(msg.origArguments), client.AuthInfo)
if err == ErrAuthorizationNeeded {
if client.TwitchUsername == "" {

View file

@ -80,7 +80,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
config.MinMemoryKBytes = defaultMinMemoryKB
}
setupBackend(config)
Backend = setupBackend(config)
if serveMux == nil {
serveMux = http.DefaultServeMux
@ -109,7 +109,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
if err != nil {
log.Fatalln("Unable to seal requests:", err)
}
resp, err := backendHTTPClient.PostForm(announceStartupURL, announceForm)
resp, err := Backend.HTTPClient.PostForm(announceStartupURL, announceForm)
if err != nil {
log.Println("could not announce startup to backend:", err)
} else {

View file

@ -162,7 +162,7 @@ func pubsubJanitor_do() {
ChatSubscriptionLock.Unlock()
if len(cleanedUp) != 0 {
err := SendCleanupTopicsNotice(cleanedUp)
err := Backend.SendCleanupTopicsNotice(cleanedUp)
if err != nil {
log.Println("error reporting cleaned subs:", err)
}
@ -186,7 +186,7 @@ func _subscribeWhileRlocked(channelName string, value chan<- ClientMessage) {
ChatSubscriptionLock.Unlock()
go func(topic string) {
err := SendNewTopicNotice(topic)
err := Backend.SendNewTopicNotice(topic)
if err != nil {
log.Println("error reporting new sub:", err)
}