diff --git a/core/Moonshark.go b/core/Moonshark.go index 39af91c..752c238 100644 --- a/core/Moonshark.go +++ b/core/Moonshark.go @@ -6,13 +6,13 @@ import ( "fmt" "time" - "Moonshark/core/config" "Moonshark/core/http" - "Moonshark/core/logger" "Moonshark/core/routers" "Moonshark/core/runner" "Moonshark/core/sessions" "Moonshark/core/utils" + "Moonshark/core/utils/config" + "Moonshark/core/utils/logger" "Moonshark/core/watchers" ) diff --git a/core/http/Csrf.go b/core/http/Csrf.go index 5b23cfc..1830b78 100644 --- a/core/http/Csrf.go +++ b/core/http/Csrf.go @@ -1,8 +1,8 @@ package http import ( - "Moonshark/core/logger" "Moonshark/core/utils" + "Moonshark/core/utils/logger" "github.com/valyala/fasthttp" ) diff --git a/core/http/HttpLogger.go b/core/http/HttpLogger.go index 7a96470..8705f6f 100644 --- a/core/http/HttpLogger.go +++ b/core/http/HttpLogger.go @@ -3,7 +3,7 @@ package http import ( "time" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" ) // StatusColors for different status code ranges diff --git a/core/http/Server.go b/core/http/Server.go index bf10aae..2024b71 100644 --- a/core/http/Server.go +++ b/core/http/Server.go @@ -6,12 +6,12 @@ import ( "fmt" "time" - "Moonshark/core/config" - "Moonshark/core/logger" "Moonshark/core/metadata" "Moonshark/core/routers" "Moonshark/core/runner" "Moonshark/core/utils" + "Moonshark/core/utils/config" + "Moonshark/core/utils/logger" "github.com/valyala/fasthttp" ) diff --git a/core/routers/StaticRouter.go b/core/routers/StaticRouter.go index 86c10c5..029fcb2 100644 --- a/core/routers/StaticRouter.go +++ b/core/routers/StaticRouter.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" "github.com/valyala/fasthttp" ) diff --git a/core/runner/CoreModules.go b/core/runner/CoreModules.go index 9356ce7..bf25778 100644 --- a/core/runner/CoreModules.go +++ b/core/runner/CoreModules.go @@ -1,12 +1,11 @@ package runner import ( + "Moonshark/core/utils/logger" "fmt" "strings" "sync" - "Moonshark/core/logger" - luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) @@ -269,11 +268,11 @@ func init() { GlobalRegistry.Register("util", UtilModuleInitFunc()) GlobalRegistry.Register("http", HTTPModuleInitFunc()) GlobalRegistry.RegisterWithDependencies("cookie", CookieModuleInitFunc(), []string{"http"}) - GlobalRegistry.RegisterWithDependencies("csrf", CSRFModuleInitFunc(), []string{"go"}) + GlobalRegistry.RegisterWithDependencies("csrf", CSRFModuleInitFunc(), []string{"util"}) // Set explicit initialization order GlobalRegistry.SetInitOrder([]string{ - "go", // First: core utilities + "util", // First: core utilities "http", // Second: HTTP functionality "cookie", // Third: Cookie functionality (uses HTTP) "csrf", // Fourth: CSRF protection (uses go and possibly session) diff --git a/core/runner/CsrfModule.go b/core/runner/CsrfModule.go index f6f882a..683f35f 100644 --- a/core/runner/CsrfModule.go +++ b/core/runner/CsrfModule.go @@ -3,7 +3,7 @@ package runner import ( "crypto/subtle" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/GoModules.go b/core/runner/GoModules.go index 56681cc..daa1df7 100644 --- a/core/runner/GoModules.go +++ b/core/runner/GoModules.go @@ -1,7 +1,7 @@ package runner import ( - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/HttpModule.go b/core/runner/HttpModule.go index 501b5ce..6f69b75 100644 --- a/core/runner/HttpModule.go +++ b/core/runner/HttpModule.go @@ -14,7 +14,7 @@ import ( "github.com/valyala/bytebufferpool" "github.com/valyala/fasthttp" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/Runner.go b/core/runner/Runner.go index a8405d0..6455a4b 100644 --- a/core/runner/Runner.go +++ b/core/runner/Runner.go @@ -12,7 +12,7 @@ import ( "github.com/panjf2000/ants/v2" "github.com/valyala/bytebufferpool" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/Sandbox.go b/core/runner/Sandbox.go index ce903e2..3f01b47 100644 --- a/core/runner/Sandbox.go +++ b/core/runner/Sandbox.go @@ -7,7 +7,7 @@ import ( "github.com/goccy/go-json" "github.com/valyala/bytebufferpool" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/SessionHandler.go b/core/runner/SessionHandler.go index 5cf915b..92d1a5c 100644 --- a/core/runner/SessionHandler.go +++ b/core/runner/SessionHandler.go @@ -3,8 +3,8 @@ package runner import ( "net/http" - "Moonshark/core/logger" "Moonshark/core/sessions" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/SessionModule.go b/core/runner/SessionModule.go index 6b1a0b9..bdea3a7 100644 --- a/core/runner/SessionModule.go +++ b/core/runner/SessionModule.go @@ -1,7 +1,7 @@ package runner import ( - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/runner/UtilModule.go b/core/runner/UtilModule.go index 55e2e57..9141af5 100644 --- a/core/runner/UtilModule.go +++ b/core/runner/UtilModule.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "encoding/base64" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/core/utils/Debug.go b/core/utils/Debug.go index 8c871ac..0aa458e 100644 --- a/core/utils/Debug.go +++ b/core/utils/Debug.go @@ -7,8 +7,8 @@ import ( "strings" "time" - "Moonshark/core/config" "Moonshark/core/metadata" + "Moonshark/core/utils/config" ) // ComponentStats holds stats from various system components @@ -26,7 +26,7 @@ type SystemStats struct { Memory runtime.MemStats Components ComponentStats Version string - Config *config.Config // Configuration information + Config *config.Config } // CollectSystemStats gathers basic system statistics diff --git a/core/utils/Dirs.go b/core/utils/Dirs.go index c4def88..d01f665 100644 --- a/core/utils/Dirs.go +++ b/core/utils/Dirs.go @@ -8,25 +8,20 @@ import ( // EnsureDir checks if a directory exists and creates it if it doesn't. // Returns any error encountered during directory creation. func EnsureDir(path string) error { - // Clean the path to handle any malformed input path = filepath.Clean(path) - // Check if the directory exists info, err := os.Stat(path) - // If no error, check if it's a directory if err == nil { if info.IsDir() { - return nil // Directory already exists + return nil } - return os.ErrExist // Path exists but is not a directory + return os.ErrExist } - // If the error is not that the path doesn't exist, return it if !os.IsNotExist(err) { return err } - // Create the directory with default permissions (0755) return os.MkdirAll(path, 0755) } diff --git a/core/config/Config.go b/core/utils/config/Config.go similarity index 98% rename from core/config/Config.go rename to core/utils/config/Config.go index 67e69dd..3924cd3 100644 --- a/core/config/Config.go +++ b/core/utils/config/Config.go @@ -39,7 +39,7 @@ type Config struct { values map[string]any } -// New creates a new configuration with default values +// NewConfig creates a new configuration with default values func New() *Config { config := &Config{ // Initialize values map diff --git a/core/logger/Logger.go b/core/utils/logger/Logger.go similarity index 100% rename from core/logger/Logger.go rename to core/utils/logger/Logger.go diff --git a/core/watchers/Api.go b/core/watchers/Api.go index dc1f0dc..8c7a821 100644 --- a/core/watchers/Api.go +++ b/core/watchers/Api.go @@ -4,9 +4,9 @@ import ( "fmt" "sync" - "Moonshark/core/logger" "Moonshark/core/routers" "Moonshark/core/runner" + "Moonshark/core/utils/logger" ) // Global watcher manager instance @@ -18,7 +18,7 @@ var ( // GetWatcherManager returns the global watcher manager, creating it if needed func GetWatcherManager(adaptive bool) *WatcherManager { globalManagerOnce.Do(func() { - globalManager = NewWatcherManager(adaptive) + globalManager = NewWatcherManager() }) return globalManager } diff --git a/core/watchers/Dir.go b/core/watchers/Dir.go index f4ed4fa..1d0528e 100644 --- a/core/watchers/Dir.go +++ b/core/watchers/Dir.go @@ -7,7 +7,7 @@ import ( "sync" "time" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" ) // Default debounce time between detected change and callback @@ -42,17 +42,10 @@ type DirectoryWatcher struct { // DirectoryWatcherConfig contains configuration for a directory watcher type DirectoryWatcherConfig struct { - // Directory to watch - Dir string - - // Callback function to call when changes are detected - Callback func() error - - // Debounce time (0 means use default) - DebounceTime time.Duration - - // Recursive watching (watch subdirectories) - Recursive bool + Dir string // Directory to watch + Callback func() error // Callback function to call when changes are detected + DebounceTime time.Duration // Debounce time (0 means use default) + Recursive bool // Recursive watching (watch subdirectories) } // NewDirectoryWatcher creates a new directory watcher @@ -83,13 +76,15 @@ func (w *DirectoryWatcher) scanDirectory() error { w.filesMu.Lock() defer w.filesMu.Unlock() + // Clear existing files map + w.files = make(map[string]FileInfo) + return filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error { if err != nil { - w.logWarning("Error accessing path %s: %v", path, err) - return nil // Continue with other files + return nil // Skip files with errors } - // Skip if not recursive and this is a subdirectory + // Skip subdirectories if not recursive if !w.recursive && info.IsDir() && path != w.dir { return filepath.SkipDir } @@ -106,25 +101,46 @@ func (w *DirectoryWatcher) scanDirectory() error { // checkForChanges detects if any files have been added, modified, or deleted func (w *DirectoryWatcher) checkForChanges() (bool, error) { - // Get current state - currentFiles := make(map[string]FileInfo) + // Lock for reading previous state + w.filesMu.RLock() + prevFileCount := len(w.files) + w.filesMu.RUnlock() + // Track new state and whether changes were detected + newFiles := make(map[string]FileInfo) + changed := false + + // Walk the directory to check for changes err := filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error { + // Skip errors (file might have been deleted) if err != nil { - // File might have been deleted between directory read and stat return nil } - // Skip if not recursive and this is a subdirectory + // Skip subdirectories if not recursive if !w.recursive && info.IsDir() && path != w.dir { return filepath.SkipDir } - currentFiles[path] = FileInfo{ + // Store current file info + currentInfo := FileInfo{ ModTime: info.ModTime(), Size: info.Size(), IsDir: info.IsDir(), } + newFiles[path] = currentInfo + + // Check if file is new or modified (only if we haven't already detected a change) + if !changed { + w.filesMu.RLock() + prevInfo, exists := w.files[path] + w.filesMu.RUnlock() + + if !exists || currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size { + changed = true + w.logDebug("File changed: %s", path) + } + } return nil }) @@ -133,55 +149,27 @@ func (w *DirectoryWatcher) checkForChanges() (bool, error) { return false, err } - // Compare with previous state - w.filesMu.RLock() - previousFiles := w.files - w.filesMu.RUnlock() - - // Check for different file count (quick check) - if len(currentFiles) != len(previousFiles) { - w.logDebug("File count changed: %d -> %d", len(previousFiles), len(currentFiles)) - w.updateFiles(currentFiles) - return true, nil + // Check for deleted files (only if we haven't already detected a change) + if !changed && len(newFiles) != prevFileCount { + w.filesMu.RLock() + for path := range w.files { + if _, exists := newFiles[path]; !exists { + changed = true + w.logDebug("File deleted: %s", path) + break + } + } + w.filesMu.RUnlock() } - // Check for modified, added, or removed files - for path, currentInfo := range currentFiles { - prevInfo, exists := previousFiles[path] - if !exists { - // New file - w.logDebug("New file detected: %s", path) - w.updateFiles(currentFiles) - return true, nil - } - - if currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size { - // File modified - w.logDebug("File modified: %s", path) - w.updateFiles(currentFiles) - return true, nil - } + // Update files map if changed + if changed { + w.filesMu.Lock() + w.files = newFiles + w.filesMu.Unlock() } - // Check for deleted files - for path := range previousFiles { - if _, exists := currentFiles[path]; !exists { - // File deleted - w.logDebug("File deleted: %s", path) - w.updateFiles(currentFiles) - return true, nil - } - } - - // No changes detected - return false, nil -} - -// updateFiles updates the internal file list -func (w *DirectoryWatcher) updateFiles(newFiles map[string]FileInfo) { - w.filesMu.Lock() - w.files = newFiles - w.filesMu.Unlock() + return changed, nil } // notifyChange triggers the callback with debouncing @@ -189,8 +177,8 @@ func (w *DirectoryWatcher) notifyChange() { w.debounceMu.Lock() defer w.debounceMu.Unlock() + // Reset timer if already debouncing if w.debouncing { - // Reset timer if already debouncing if w.debounceTimer != nil { w.debounceTimer.Stop() } diff --git a/core/watchers/Manager.go b/core/watchers/Manager.go index 412c1f2..b995785 100644 --- a/core/watchers/Manager.go +++ b/core/watchers/Manager.go @@ -4,47 +4,33 @@ import ( "sync" "time" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" ) -// Default polling intervals +// Default polling interval const ( - defaultPollInterval = 1 * time.Second // Initial polling interval - extendedPollInterval = 5 * time.Second // Extended polling interval after inactivity - inactivityThreshold = 10 * time.Minute // Time before extending polling interval + defaultPollInterval = 1 * time.Second ) // WatcherManager coordinates file watching across multiple directories type WatcherManager struct { - // Registry of directories and their watchers watchers map[string]*DirectoryWatcher mu sync.RWMutex - // Shared polling state - pollInterval time.Duration - adaptive bool - lastActivity time.Time - - // Control channels done chan struct{} ticker *time.Ticker - // Wait group for shutdown coordination wg sync.WaitGroup } // NewWatcherManager creates a new watcher manager -func NewWatcherManager(adaptive bool) *WatcherManager { +func NewWatcherManager() *WatcherManager { manager := &WatcherManager{ - watchers: make(map[string]*DirectoryWatcher), - pollInterval: defaultPollInterval, - adaptive: adaptive, - lastActivity: time.Now(), - done: make(chan struct{}), + watchers: make(map[string]*DirectoryWatcher), + done: make(chan struct{}), } - // Start the polling loop - manager.ticker = time.NewTicker(manager.pollInterval) + manager.ticker = time.NewTicker(defaultPollInterval) manager.wg.Add(1) go manager.pollLoop() @@ -86,30 +72,7 @@ func (m *WatcherManager) pollLoop() { for { select { case <-m.ticker.C: - anyActivity := m.checkAllDirectories() - - // Update polling interval based on activity - if m.adaptive { - if anyActivity { - // Activity detected, reset to fast polling - m.lastActivity = time.Now() - if m.pollInterval > defaultPollInterval { - m.pollInterval = defaultPollInterval - m.ticker.Reset(m.pollInterval) - logger.Debug("[WatcherManager] Reset to base polling interval: %v", m.pollInterval) - } - } else { - // No activity, consider slowing down polling - inactiveDuration := time.Since(m.lastActivity) - if m.pollInterval == defaultPollInterval && inactiveDuration > inactivityThreshold { - m.pollInterval = extendedPollInterval - m.ticker.Reset(m.pollInterval) - logger.Debug("[WatcherManager] Extended polling interval to: %v after %v of inactivity", - m.pollInterval, inactiveDuration.Round(time.Minute)) - } - } - } - + m.checkAllDirectories() case <-m.done: return } @@ -117,11 +80,10 @@ func (m *WatcherManager) pollLoop() { } // checkAllDirectories polls all registered directories for changes -func (m *WatcherManager) checkAllDirectories() bool { +func (m *WatcherManager) checkAllDirectories() { m.mu.RLock() defer m.mu.RUnlock() - anyActivity := false for _, watcher := range m.watchers { changed, err := watcher.checkForChanges() if err != nil { @@ -130,10 +92,7 @@ func (m *WatcherManager) checkAllDirectories() bool { } if changed { - anyActivity = true watcher.notifyChange() } } - - return anyActivity } diff --git a/main.go b/main.go index 99d9325..dbe2f3a 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "syscall" "Moonshark/core" - "Moonshark/core/logger" + "Moonshark/core/utils/logger" ) func main() {