From 82a7c88d3567728ca3e67a4feeb2cd58223cab8d Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sat, 22 Mar 2025 09:49:16 -0500 Subject: [PATCH] add watcher config settings --- moonshark.go | 69 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/moonshark.go b/moonshark.go index 9fc178a..e30ee0c 100644 --- a/moonshark.go +++ b/moonshark.go @@ -17,6 +17,13 @@ import ( "git.sharkk.net/Sky/Moonshark/core/watchers" ) +// WatcherConfig holds the configuration for which watchers to enable +type WatcherConfig struct { + Routes bool + Static bool + Modules bool +} + // initRouters sets up the Lua and static routers func initRouters(routesDir, staticDir string, log *logger.Logger) (*routers.LuaRouter, *routers.StaticRouter, error) { // Ensure directories exist @@ -48,30 +55,34 @@ func initRouters(routesDir, staticDir string, log *logger.Logger) (*routers.LuaR // setupWatchers initializes and starts all file watchers func setupWatchers(luaRouter *routers.LuaRouter, staticRouter *routers.StaticRouter, luaRunner *runner.LuaRunner, routesDir string, staticDir string, - libDirs []string, log *logger.Logger) ([]func() error, error) { + libDirs []string, log *logger.Logger, config WatcherConfig) ([]func() error, error) { var cleanupFuncs []func() error // Set up watcher for Lua routes - luaRouterWatcher, err := watchers.WatchLuaRouter(luaRouter, routesDir, log) - if err != nil { - log.Warning("Failed to watch routes directory: %v", err) - } else { - cleanupFuncs = append(cleanupFuncs, luaRouterWatcher.Close) - log.Info("File watcher active for Lua routes") + if config.Routes { + luaRouterWatcher, err := watchers.WatchLuaRouter(luaRouter, routesDir, log) + if err != nil { + log.Warning("Failed to watch routes directory: %v", err) + } else { + cleanupFuncs = append(cleanupFuncs, luaRouterWatcher.Close) + log.Info("File watcher active for Lua routes") + } } // Set up watcher for static files - staticWatcher, err := watchers.WatchStaticRouter(staticRouter, staticDir, log) - if err != nil { - log.Warning("Failed to watch static directory: %v", err) - } else { - cleanupFuncs = append(cleanupFuncs, staticWatcher.Close) - log.Info("File watcher active for static files") + if config.Static { + staticWatcher, err := watchers.WatchStaticRouter(staticRouter, staticDir, log) + if err != nil { + log.Warning("Failed to watch static directory: %v", err) + } else { + cleanupFuncs = append(cleanupFuncs, staticWatcher.Close) + log.Info("File watcher active for static files") + } } // Set up watchers for Lua modules libraries - if len(libDirs) > 0 { + if config.Modules && len(libDirs) > 0 { moduleWatchers, err := watchers.WatchLuaModules(luaRunner, libDirs, log) if err != nil { log.Warning("Failed to watch Lua module directories: %v", err) @@ -159,11 +170,33 @@ func main() { // Set up file watchers if enabled var cleanupFuncs []func() error - if cfg.GetBool("watchers", true) { - cleanupFuncs, err = setupWatchers(luaRouter, staticRouter, luaRunner, routesDir, staticDir, libDirs, log) - if err != nil { - log.Warning("Error setting up watchers: %v", err) + + // Get watcher configuration (default all disabled) + watcherConfig := WatcherConfig{ + Routes: false, + Static: false, + Modules: false, + } + + // Enable watchers based on map configuration + watchersMap := cfg.GetMap("watchers") + if watchersMap != nil { + if routes, ok := watchersMap["routes"].(bool); ok && routes { + watcherConfig.Routes = true } + if static, ok := watchersMap["static"].(bool); ok && static { + watcherConfig.Static = true + } + if modules, ok := watchersMap["modules"].(bool); ok && modules { + watcherConfig.Modules = true + } + } + + // Setup enabled watchers + cleanupFuncs, err = setupWatchers(luaRouter, staticRouter, luaRunner, + routesDir, staticDir, libDirs, log, watcherConfig) + if err != nil { + log.Warning("Error setting up watchers: %v", err) } // Register cleanup functions