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

View file

@ -396,7 +396,7 @@ func aggregateDataSender_do() {
return return
} }
err = SendAggregatedData(form) err = Backend.SendAggregatedData(form)
if err != nil { if err != nil {
log.Println("error reporting aggregate data:", err) log.Println("error reporting aggregate data:", err)
return return
@ -533,7 +533,7 @@ func C2SHandleBunchedCommand(conn *websocket.Conn, client *ClientInfo, msg Clien
pendingBunchedRequests[br] = &bunchSubscriberList{Members: []bunchSubscriber{{Client: client, MessageID: msg.MessageID}}} pendingBunchedRequests[br] = &bunchSubscriberList{Members: []bunchSubscriber{{Client: client, MessageID: msg.MessageID}}}
go func(request bunchedRequest) { 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 var msg ClientMessage
if err == nil { 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." const AuthorizationNeededError = "You must be signed in to use that command."
func doRemoteCommand(conn *websocket.Conn, msg ClientMessage, client *ClientInfo) { 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 err == ErrAuthorizationNeeded {
if client.TwitchUsername == "" { if client.TwitchUsername == "" {

View file

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

View file

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