grafana/pkg/services/live/pushws/push_pipeline.go
idafurjes a14621fff6
Chore: Add user service method SetUsingOrg and GetSignedInUserWithCacheCtx (#53343)
* Chore: Add user service method SetUsingOrg

* Chore: Add user service method GetSignedInUserWithCacheCtx

* Use method GetSignedInUserWithCacheCtx from user service

* Fix lint after rebase

* Fix lint

* Fix lint error

* roll back some changes

* Roll back changes in api and middleware

* Add xorm tags to SignedInUser ID fields
2022-08-11 13:28:55 +02:00

85 lines
2.1 KiB
Go

package pushws
import (
"net/http"
"github.com/grafana/grafana/pkg/services/live/convert"
"github.com/grafana/grafana/pkg/services/live/livecontext"
"github.com/grafana/grafana/pkg/services/live/pipeline"
"github.com/gorilla/websocket"
)
// PipelinePushHandler handles WebSocket client connections that push data to Live Pipeline.
type PipelinePushHandler struct {
pipeline *pipeline.Pipeline
config Config
upgrade *websocket.Upgrader
converter *convert.Converter
}
// NewPathHandler creates new PipelinePushHandler.
func NewPipelinePushHandler(pipeline *pipeline.Pipeline, c Config) *PipelinePushHandler {
if c.CheckOrigin == nil {
c.CheckOrigin = sameHostOriginCheck()
}
upgrade := &websocket.Upgrader{
ReadBufferSize: c.ReadBufferSize,
WriteBufferSize: c.WriteBufferSize,
CheckOrigin: c.CheckOrigin,
}
return &PipelinePushHandler{
pipeline: pipeline,
config: c,
upgrade: upgrade,
converter: convert.NewConverter(),
}
}
func (s *PipelinePushHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
channelID, ok := livecontext.GetContextChannelID(r.Context())
if !ok || channelID == "" {
logger.Warn("Push request without channel")
rw.WriteHeader(http.StatusInternalServerError)
return
}
user, ok := livecontext.GetContextSignedUser(r.Context())
if !ok {
logger.Error("No user found in context")
rw.WriteHeader(http.StatusInternalServerError)
return
}
conn, err := s.upgrade.Upgrade(rw, r, nil)
if err != nil {
return
}
defer func() { _ = conn.Close() }()
setupWSConn(r.Context(), conn, s.config)
for {
_, body, err := conn.ReadMessage()
if err != nil {
logger.Debug("Error reading websocket connection", "error", err)
break
}
logger.Debug("Live channel push request",
"protocol", "http",
"channel", channelID,
"bodyLength", len(body),
)
ruleFound, err := s.pipeline.ProcessInput(r.Context(), user.OrgID, channelID, body)
if err != nil {
logger.Error("Pipeline input processing error", "error", err, "body", string(body))
return
}
if !ruleFound {
logger.Error("No conversion rule for a channel", "error", err, "channel", channelID)
return
}
}
}