Reconnect websockets to API & server
This commit is contained in:
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/cortezaproject/corteza-server/messaging/websocket"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -39,6 +40,7 @@ type (
|
||||
|
||||
// Servers
|
||||
HttpServer httpApiServer
|
||||
WsServer *websocket.Websocket
|
||||
GrpcServer grpcServer
|
||||
}
|
||||
)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
cmpEvent "github.com/cortezaproject/corteza-server/compose/service/event"
|
||||
msgService "github.com/cortezaproject/corteza-server/messaging/service"
|
||||
msgEvent "github.com/cortezaproject/corteza-server/messaging/service/event"
|
||||
"github.com/cortezaproject/corteza-server/messaging/websocket"
|
||||
"github.com/cortezaproject/corteza-server/pkg/actionlog"
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/corredor"
|
||||
@@ -227,6 +228,12 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) {
|
||||
corredor.Service().SetUserFinder(sysService.DefaultUser)
|
||||
corredor.Service().SetRoleFinder(sysService.DefaultRole)
|
||||
|
||||
app.WsServer = websocket.New(&websocket.Config{
|
||||
Timeout: app.Opt.Websocket.Timeout,
|
||||
PingTimeout: app.Opt.Websocket.PingTimeout,
|
||||
PingPeriod: app.Opt.Websocket.PingPeriod,
|
||||
})
|
||||
|
||||
app.lvl = bootLevelServicesInitialized
|
||||
return
|
||||
}
|
||||
@@ -311,6 +318,10 @@ func (app *CortezaApp) Activate(ctx context.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if app.WsServer != nil {
|
||||
websocket.Watch(ctx)
|
||||
}
|
||||
|
||||
// Initialize external authentication (from default settings)
|
||||
//
|
||||
// We're relying on current settings to be loaded at this point so
|
||||
|
||||
@@ -52,7 +52,10 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) {
|
||||
r.Route("/"+apiBaseUrl, func(r chi.Router) {
|
||||
r.Route("/system", systemRest.MountRoutes)
|
||||
r.Route("/compose", composeRest.MountRoutes)
|
||||
r.Route("/messaging", messagingRest.MountRoutes)
|
||||
r.Route("/messaging", func(r chi.Router) {
|
||||
messagingRest.MountRoutes(r)
|
||||
app.WsServer.ApiServerRoutes(r)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,13 @@ package websocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/repository"
|
||||
"github.com/cortezaproject/corteza-server/messaging/service"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sentry"
|
||||
"github.com/go-chi/chi"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func middlewareAllowedAccess(next http.Handler) http.Handler {
|
||||
@@ -46,17 +44,7 @@ func Watch(ctx context.Context) {
|
||||
func (ws Websocket) ApiServerRoutes(r chi.Router) {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Route("/websocket", func(r chi.Router) {
|
||||
r.Use(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// @todo make access control injectable
|
||||
if !service.DefaultAccessControl.CanAccess(r.Context()) {
|
||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
r.Use(middlewareAllowedAccess)
|
||||
r.Get("/", ws.Open)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user