satellite/admin/back-office: Use PathPrefix value

PathPrefix is the full path of the subrouter passed to the back office
server when it hooks into another server, in this case, the satellite
admin server.

PathPrefix allows to serve the static assets from the root of the
sub-router when the prefix is stripped before accessing them.

There was a bug where the PathPrefix weren't used and a hard-coded path
was used.

Test passed because the back-office server is hooked into the satellite
admin server with a subrouter with path `/back-office/` which matched
the hard-code value, however, it wouldn't work if that path changed or
it is hooked into another server with a different subrouter path,
despite it was set to PathPrefix.

This commit fixes that bug.

Change-Id: Id4a0d86329eb563b008b3fc6f8eb7b51cbfd2e6f
This commit is contained in:
Ivan Fraixedes 2023-11-14 12:21:40 +01:00 committed by Storj Robot
parent fe890ff535
commit e39f395cf1

View File

@ -46,6 +46,7 @@ type Server struct {
// ParentRouter is mux.Router with its full path prefix.
type ParentRouter struct {
Router *mux.Router
// PathPrefix is the full path prefix of Router.
PathPrefix string
}
@ -81,13 +82,13 @@ func NewServer(log *zap.Logger, listener net.Listener, parentRouter *ParentRoute
var staticHandler http.Handler
if config.StaticDir == "" {
if parentRouter.PathPrefix != "" {
staticHandler = http.StripPrefix("/back-office/", http.FileServer(http.FS(ui.Assets)))
staticHandler = http.StripPrefix(parentRouter.PathPrefix, http.FileServer(http.FS(ui.Assets)))
} else {
staticHandler = http.FileServer(http.FS(ui.Assets))
}
} else {
if parentRouter.PathPrefix != "" {
staticHandler = http.StripPrefix("/back-office/", http.FileServer(http.Dir(config.StaticDir)))
staticHandler = http.StripPrefix(parentRouter.PathPrefix, http.FileServer(http.Dir(config.StaticDir)))
} else {
staticHandler = http.FileServer(http.Dir(config.StaticDir))
}