mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Live: streamId in path for ws push endpoint (#33786)
This commit is contained in:
parent
f929b29b0b
commit
bfd5d3b16a
@ -248,6 +248,7 @@ func (g *GrafanaLive) Init() error {
|
|||||||
user := ctx.SignedInUser
|
user := ctx.SignedInUser
|
||||||
newCtx := livecontext.SetContextSignedUser(ctx.Req.Context(), user)
|
newCtx := livecontext.SetContextSignedUser(ctx.Req.Context(), user)
|
||||||
newCtx = livecontext.SetContextValues(newCtx, ctx.Req.URL.Query())
|
newCtx = livecontext.SetContextValues(newCtx, ctx.Req.URL.Query())
|
||||||
|
newCtx = livecontext.SetContextStreamID(newCtx, ctx.Params(":streamId"))
|
||||||
r := ctx.Req.Request
|
r := ctx.Req.Request
|
||||||
r = r.WithContext(newCtx)
|
r = r.WithContext(newCtx)
|
||||||
pushWSHandler.ServeHTTP(ctx.Resp, r)
|
pushWSHandler.ServeHTTP(ctx.Resp, r)
|
||||||
@ -258,7 +259,7 @@ func (g *GrafanaLive) Init() error {
|
|||||||
}, middleware.ReqSignedIn)
|
}, middleware.ReqSignedIn)
|
||||||
|
|
||||||
g.RouteRegister.Group("/api/live", func(group routing.RouteRegister) {
|
g.RouteRegister.Group("/api/live", func(group routing.RouteRegister) {
|
||||||
group.Get("/push", g.pushWebsocketHandler)
|
group.Get("/push/:streamId", g.pushWebsocketHandler)
|
||||||
}, middleware.ReqOrgAdmin)
|
}, middleware.ReqOrgAdmin)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -38,3 +38,18 @@ func GetContextValues(ctx context.Context) (url.Values, bool) {
|
|||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type streamIDContextKey struct{}
|
||||||
|
|
||||||
|
func SetContextStreamID(ctx context.Context, streamID string) context.Context {
|
||||||
|
ctx = context.WithValue(ctx, streamIDContextKey{}, streamID)
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetContextStreamID(ctx context.Context) (string, bool) {
|
||||||
|
if val := ctx.Value(streamIDContextKey{}); val != nil {
|
||||||
|
values, ok := val.(string)
|
||||||
|
return values, ok
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/live/convert"
|
"github.com/grafana/grafana/pkg/services/live/convert"
|
||||||
|
"github.com/grafana/grafana/pkg/services/live/livecontext"
|
||||||
"github.com/grafana/grafana/pkg/services/live/managedstream"
|
"github.com/grafana/grafana/pkg/services/live/managedstream"
|
||||||
"github.com/grafana/grafana/pkg/services/live/pushurl"
|
"github.com/grafana/grafana/pkg/services/live/pushurl"
|
||||||
|
|
||||||
@ -101,15 +102,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (s *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
var streamID string
|
streamID, ok := livecontext.GetContextStreamID(r.Context())
|
||||||
|
if !ok || streamID == "" {
|
||||||
streamID = r.Header.Get("X-Grafana-Live-Stream")
|
|
||||||
if streamID == "" {
|
|
||||||
streamID = r.URL.Query().Get("gf_live_stream")
|
|
||||||
}
|
|
||||||
if streamID == "" {
|
|
||||||
logger.Warn("Push request without stream ID")
|
logger.Warn("Push request without stream ID")
|
||||||
rw.WriteHeader(http.StatusBadRequest)
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user