api: Cleanup websocket protocol
This commit cleans up the websocket protocol to be in line with the documentation.
This commit is contained in:
parent
16fa721048
commit
1c68893a02
31 changed files with 3549 additions and 120 deletions
|
|
@ -3,8 +3,11 @@ package socket
|
|||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger"
|
||||
|
|
@ -25,22 +28,44 @@ type Sockets struct {
|
|||
Instances *instances.Instances
|
||||
Exporter *exporter.Exporter
|
||||
Purger *purger.Purger
|
||||
Auth *auth.Auth
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Routeable = (*Sockets)(nil)
|
||||
)
|
||||
|
||||
func (socket *Sockets) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Prefix: "/api/v1/ws",
|
||||
Exact: true,
|
||||
Decorator: socket.Dependencies.Auth.Require(true, scopes.ScopeUserValid, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (sockets *Sockets) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
|
||||
return &httpx.WebSocket{
|
||||
Context: ctx,
|
||||
Handler: sockets.Serve,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Serve handles a connection to the websocket api
|
||||
func (socket *Sockets) Serve(conn httpx.WebSocketConnection) {
|
||||
// handle the websocket connection!
|
||||
name, err := socket.actions.Get(socket.Actions).Handle(conn)
|
||||
name, err := socket.actions.Get(socket.Actions).Handle(socket.Dependencies.Auth, conn)
|
||||
if err != nil {
|
||||
zerolog.Ctx(conn.Context()).Err(err).Str("name", name).Msg("Error handling websocket")
|
||||
}
|
||||
}
|
||||
|
||||
// Generic returns a new action that calls handler with the provided number of parameters
|
||||
func (sockets *Sockets) Generic(numParams int, handler func(ctx context.Context, socket *Sockets, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
func (sockets *Sockets) Generic(scope component.Scope, scopeParam string, numParams int, handler func(ctx context.Context, socket *Sockets, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
NumParams: numParams,
|
||||
Scope: scope,
|
||||
ScopeParam: scopeParam,
|
||||
NumParams: numParams,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
return handler(ctx, sockets, in, out, params...)
|
||||
},
|
||||
|
|
@ -48,8 +73,11 @@ func (sockets *Sockets) Generic(numParams int, handler func(ctx context.Context,
|
|||
}
|
||||
|
||||
// Insstance returns a new action that calls handler with a specific WissKI instance
|
||||
func (sockets *Sockets) Instance(numParams int, handler func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
func (sockets *Sockets) Instance(scope component.Scope, scopeParam string, numParams int, handler func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
Scope: scope,
|
||||
ScopeParam: scopeParam,
|
||||
|
||||
NumParams: numParams + 1,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
instance, err := sockets.Dependencies.Instances.WissKI(ctx, params[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue