gotosocial/internal/api/client/streaming/stream.go

53 lines
1.3 KiB
Go
Raw Normal View History

2021-06-17 17:20:08 +00:00
package streaming
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
func (m *Module) StreamGETHandler(c *gin.Context) {
l := m.log.WithField("func", "StreamGETHandler")
2021-06-17 17:20:08 +00:00
streamType := c.Query(StreamQueryKey)
if streamType == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("no stream type provided under query key %s", StreamQueryKey)})
return
}
accessToken := c.Query(AccessTokenQueryKey)
if accessToken == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("no access token provided under query key %s", AccessTokenQueryKey)})
return
}
account, err := m.processor.AuthorizeStreamingRequest(accessToken)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "could not authorize with given token"})
return
}
upgrader := websocket.Upgrader{
HandshakeTimeout: 5 * time.Second,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"wss"},
CheckOrigin: func(r *http.Request) bool {
return true
},
2021-06-17 17:20:08 +00:00
}
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
l.Infof("error upgrading websocket connection: %s", err)
2021-06-17 17:20:08 +00:00
return
}
if errWithCode := m.processor.OpenStreamForAccount(conn, account, streamType); errWithCode != nil {
2021-06-17 17:20:08 +00:00
c.JSON(errWithCode.Code(), errWithCode.Safe())
}
}