app/plugin: add product middleware and enable products register their routers (#20374)

Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-06-07 09:12:15 +03:00
коммит произвёл GitHub
родитель 2580722426
Коммит c1c084a596
4 изменённых файлов: 37 добавлений и 1 удалений

Просмотреть файл

@@ -51,6 +51,7 @@ type Channels struct {
cfgSvc configSvc cfgSvc configSvc
filestore filestore.FileBackend filestore filestore.FileBackend
licenseSvc licenseSvc licenseSvc licenseSvc
routerSvc *routerService
postActionCookieSecret []byte postActionCookieSecret []byte
@@ -209,6 +210,9 @@ func NewChannels(s *Server, services map[ServiceKey]interface{}) (*Channels, err
return nil, errors.Wrap(imgErr, "failed to create image encoder") return nil, errors.Wrap(imgErr, "failed to create image encoder")
} }
ch.routerSvc = newRouterService()
services[RouterKey] = ch.routerSvc
// Setup routes. // Setup routes.
pluginsRoute := ch.srv.Router.PathPrefix("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() pluginsRoute := ch.srv.Router.PathPrefix("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter()
pluginsRoute.HandleFunc("", ch.ServePluginRequest) pluginsRoute.HandleFunc("", ch.ServePluginRequest)
@@ -267,6 +271,7 @@ func (ch *Channels) Start() error {
if err := ch.ensurePostActionCookieSecret(); err != nil { if err := ch.ensurePostActionCookieSecret(); err != nil {
return errors.Wrapf(err, "unable to ensure PostAction cookie secret") return errors.Wrapf(err, "unable to ensure PostAction cookie secret")
} }
return nil return nil
} }

Просмотреть файл

@@ -17,6 +17,7 @@ import (
"sync" "sync"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/gorilla/mux"
svg "github.com/h2non/go-is-svg" svg "github.com/h2non/go-is-svg"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -37,6 +38,28 @@ type pluginSignaturePath struct {
signaturePath string signaturePath string
} }
type routerService struct {
mu sync.Mutex
routerMap map[string]*mux.Router
}
func newRouterService() *routerService {
return &routerService{
routerMap: make(map[string]*mux.Router),
}
}
func (rs *routerService) RegisterRouter(productID string, sub *mux.Router) {
rs.mu.Lock()
defer rs.mu.Unlock()
rs.routerMap[productID] = sub
}
func (rs *routerService) getHandler(productID string) (http.Handler, bool) {
handler, ok := rs.routerMap[productID]
return handler, ok
}
// GetPluginsEnvironment returns the plugin environment for use if plugins are enabled and // GetPluginsEnvironment returns the plugin environment for use if plugins are enabled and
// initialized. // initialized.
// //

Просмотреть файл

@@ -21,6 +21,14 @@ import (
) )
func (ch *Channels) ServePluginRequest(w http.ResponseWriter, r *http.Request) { func (ch *Channels) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
if handler, ok := ch.routerSvc.getHandler(params["plugin_id"]); ok {
ch.servePluginRequest(w, r, func(*plugin.Context, http.ResponseWriter, *http.Request) {
handler.ServeHTTP(w, r)
})
return
}
pluginsEnvironment := ch.GetPluginsEnvironment() pluginsEnvironment := ch.GetPluginsEnvironment()
if pluginsEnvironment == nil { if pluginsEnvironment == nil {
err := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented) err := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented)
@@ -31,7 +39,6 @@ func (ch *Channels) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
params := mux.Vars(r)
hooks, err := pluginsEnvironment.HooksForPlugin(params["plugin_id"]) hooks, err := pluginsEnvironment.HooksForPlugin(params["plugin_id"])
if err != nil { if err != nil {
mlog.Debug("Access to route for non-existent plugin", mlog.Debug("Access to route for non-existent plugin",

Просмотреть файл

@@ -95,6 +95,7 @@ const (
TeamKey ServiceKey = "team" TeamKey ServiceKey = "team"
UserKey ServiceKey = "user" UserKey ServiceKey = "user"
PermissionsKey ServiceKey = "permissions" PermissionsKey ServiceKey = "permissions"
RouterKey ServiceKey = "router"
) )
type Server struct { type Server struct {