added golangci-linting, refactor, simplified error msgs and fixed some error handling

This commit is contained in:
yusing
2024-10-10 11:52:09 +08:00
parent d91b66ae87
commit da04a0dff4
63 changed files with 690 additions and 410 deletions

View File

@@ -7,10 +7,12 @@ import (
"github.com/yusing/go-proxy/internal/common"
)
var configDirWatcher *dirWatcher
var configDirWatcherMu sync.Mutex
var (
configDirWatcher *DirWatcher
configDirWatcherMu sync.Mutex
)
// create a new file watcher for file under ConfigBasePath
// create a new file watcher for file under ConfigBasePath.
func NewConfigFileWatcher(filename string) Watcher {
configDirWatcherMu.Lock()
defer configDirWatcherMu.Unlock()

View File

@@ -13,7 +13,7 @@ import (
"github.com/yusing/go-proxy/internal/watcher/events"
)
type dirWatcher struct {
type DirWatcher struct {
dir string
w *fsnotify.Watcher
@@ -26,7 +26,7 @@ type dirWatcher struct {
ctx context.Context
}
func NewDirectoryWatcher(ctx context.Context, dirPath string) *dirWatcher {
func NewDirectoryWatcher(ctx context.Context, dirPath string) *DirWatcher {
//! subdirectories are not watched
w, err := fsnotify.NewWatcher()
if err != nil {
@@ -35,7 +35,7 @@ func NewDirectoryWatcher(ctx context.Context, dirPath string) *dirWatcher {
if err = w.Add(dirPath); err != nil {
logrus.Panicf("unable to create fs watcher: %s", err)
}
helper := &dirWatcher{
helper := &DirWatcher{
dir: dirPath,
w: w,
fwMap: F.NewMapOf[string, *fileWatcher](),
@@ -47,11 +47,11 @@ func NewDirectoryWatcher(ctx context.Context, dirPath string) *dirWatcher {
return helper
}
func (h *dirWatcher) Events(_ context.Context) (<-chan Event, <-chan E.NestedError) {
func (h *DirWatcher) Events(_ context.Context) (<-chan Event, <-chan E.NestedError) {
return h.eventCh, h.errCh
}
func (h *dirWatcher) Add(relPath string) *fileWatcher {
func (h *DirWatcher) Add(relPath string) Watcher {
h.mu.Lock()
defer h.mu.Unlock()
@@ -85,7 +85,7 @@ func (h *dirWatcher) Add(relPath string) *fileWatcher {
return s
}
func (h *dirWatcher) start() {
func (h *DirWatcher) start() {
defer close(h.eventCh)
defer h.w.Close()