add watcher config settings

This commit is contained in:
Sky Johnson 2025-03-22 09:49:16 -05:00
parent 87aadc8574
commit 82a7c88d35

View File

@ -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,11 +55,12 @@ 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
if config.Routes {
luaRouterWatcher, err := watchers.WatchLuaRouter(luaRouter, routesDir, log)
if err != nil {
log.Warning("Failed to watch routes directory: %v", err)
@ -60,8 +68,10 @@ func setupWatchers(luaRouter *routers.LuaRouter, staticRouter *routers.StaticRou
cleanupFuncs = append(cleanupFuncs, luaRouterWatcher.Close)
log.Info("File watcher active for Lua routes")
}
}
// Set up watcher 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)
@ -69,9 +79,10 @@ func setupWatchers(luaRouter *routers.LuaRouter, staticRouter *routers.StaticRou
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,12 +170,34 @@ 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)
// 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
defer func() {