Skip to content

Commit

Permalink
Add UIConfig To Static Assets Handler Config
Browse files Browse the repository at this point in the history
Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
  • Loading branch information
mahadzaryab1 committed Sep 23, 2024
1 parent b46ecbc commit f6fa90b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
21 changes: 11 additions & 10 deletions cmd/query/app/static_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ var (
// RegisterStaticHandler adds handler for static assets to the router.
func RegisterStaticHandler(r *mux.Router, logger *zap.Logger, qOpts *QueryOptions, qCapabilities querysvc.StorageCapabilities) io.Closer {
staticHandler, err := NewStaticAssetsHandler(qOpts.UIConfig.AssetsPath, StaticAssetsHandlerOptions{
UIConfig: UIConfig{
ConfigFile: qOpts.UIConfig.ConfigFile,
LogAccess: qOpts.UIConfig.LogAccess,
},
BasePath: qOpts.BasePath,
UIConfigPath: qOpts.UIConfig.ConfigFile,
StorageCapabilities: qCapabilities,
Logger: logger,
LogAccess: qOpts.UIConfig.LogAccess,
})
if err != nil {
logger.Panic("Could not create static assets handler", zap.Error(err))
Expand All @@ -62,9 +64,8 @@ type StaticAssetsHandler struct {

// StaticAssetsHandlerOptions defines options for NewStaticAssetsHandler
type StaticAssetsHandlerOptions struct {
UIConfig UIConfig
BasePath string
UIConfigPath string
LogAccess bool
StorageCapabilities querysvc.StorageCapabilities
Logger *zap.Logger
}
Expand All @@ -91,8 +92,8 @@ func NewStaticAssetsHandler(staticAssetsRoot string, options StaticAssetsHandler
return nil, err
}

options.Logger.Info("Using UI configuration", zap.String("path", options.UIConfigPath))
watcher, err := fswatcher.New([]string{options.UIConfigPath}, h.reloadUIConfig, h.options.Logger)
options.Logger.Info("Using UI configuration", zap.String("path", options.UIConfig.ConfigFile))
watcher, err := fswatcher.New([]string{options.UIConfig.ConfigFile}, h.reloadUIConfig, h.options.Logger)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +110,7 @@ func (sH *StaticAssetsHandler) loadAndEnrichIndexHTML(open func(string) (http.Fi
return nil, fmt.Errorf("cannot load index.html: %w", err)
}
// replace UI config
if configObject, err := loadUIConfig(sH.options.UIConfigPath); err != nil {
if configObject, err := loadUIConfig(sH.options.UIConfig.ConfigFile); err != nil {
return nil, err
} else if configObject != nil {
indexBytes = configObject.regexp.ReplaceAll(indexBytes, configObject.config)
Expand Down Expand Up @@ -137,13 +138,13 @@ func (sH *StaticAssetsHandler) loadAndEnrichIndexHTML(open func(string) (http.Fi
}

func (sH *StaticAssetsHandler) reloadUIConfig() {
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfigPath))
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfig.ConfigFile))
content, err := sH.loadAndEnrichIndexHTML(sH.assetsFS.Open)
if err != nil {
sH.options.Logger.Error("error while reloading the UI config", zap.Error(err))
}
sH.indexHTML.Store(content)
sH.options.Logger.Info("reloaded UI config", zap.String("filename", sH.options.UIConfigPath))
sH.options.Logger.Info("reloaded UI config", zap.String("filename", sH.options.UIConfig.ConfigFile))
}

func loadIndexHTML(open func(string) (http.File, error)) ([]byte, error) {
Expand Down Expand Up @@ -200,7 +201,7 @@ func loadUIConfig(uiConfig string) (*loadedConfig, error) {
}

func (sH *StaticAssetsHandler) loggingHandler(handler http.Handler) http.Handler {
if !sH.options.LogAccess {
if !sH.options.UIConfig.LogAccess {
return handler
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 13 additions & 7 deletions cmd/query/app/static_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,20 @@ func TestRegisterStaticHandler(t *testing.T) {

func TestNewStaticAssetsHandlerErrors(t *testing.T) {
_, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{
UIConfigPath: "fixture/invalid-config",
Logger: zap.NewNop(),
UIConfig: UIConfig{
ConfigFile: "fixture/invalid-config",
},
Logger: zap.NewNop(),
})
require.Error(t, err)

for _, base := range []string{"x", "x/", "/x/"} {
_, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{
UIConfigPath: "fixture/ui-config.json",
BasePath: base,
Logger: zap.NewNop(),
UIConfig: UIConfig{
ConfigFile: "fixture/ui-config.json",
},
BasePath: base,
Logger: zap.NewNop(),
})
require.Errorf(t, err, "basePath=%s", base)
assert.Contains(t, err.Error(), "invalid base path")
Expand Down Expand Up @@ -195,8 +199,10 @@ func TestHotReloadUIConfig(t *testing.T) {
zcore, logObserver := observer.New(zapcore.InfoLevel)
logger := zap.New(zcore)
h, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{
UIConfigPath: cfgFileName,
Logger: logger,
UIConfig: UIConfig{
ConfigFile: cfgFileName,
},
Logger: logger,
})
require.NoError(t, err)
defer h.Close()
Expand Down

0 comments on commit f6fa90b

Please sign in to comment.