From 1c90bafcf85054192a30ef97ce9868384073009b Mon Sep 17 00:00:00 2001 From: Lz Date: Mon, 23 Dec 2024 10:57:03 +0800 Subject: [PATCH] refactor(io): improved dir exists checking to reduce io --- viewengine_html.go | 64 +++++++++++++++++++------------------------- viewengine_static.go | 15 +++++------ 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/viewengine_html.go b/viewengine_html.go index c17b776..7dc3596 100644 --- a/viewengine_html.go +++ b/viewengine_html.go @@ -75,15 +75,7 @@ func (ve *HtmlViewEngine) FileChanged(fsys fs.FS, app *App, event fsnotify.Event } func (ve *HtmlViewEngine) loadComponents() error { - _, err := fs.Stat(ve.fsys, "components") - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err - } - - return fs.WalkDir(ve.fsys, "components", func(path string, d fs.DirEntry, err error) error { + err := fs.WalkDir(ve.fsys, "components", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -95,6 +87,12 @@ func (ve *HtmlViewEngine) loadComponents() error { return err }) + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil + } + + return err + } func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) { @@ -119,15 +117,7 @@ func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) { } func (ve *HtmlViewEngine) loadLayouts() error { - _, err := fs.Stat(ve.fsys, "layouts") - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err - } - - return fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, err error) error { + err := fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -142,18 +132,16 @@ func (ve *HtmlViewEngine) loadLayouts() error { return nil }) -} -func (ve *HtmlViewEngine) loadPages() error { - _, err := fs.Stat(ve.fsys, "pages") - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil } - return fs.WalkDir(ve.fsys, "pages", func(path string, d fs.DirEntry, err error) error { + return err +} + +func (ve *HtmlViewEngine) loadPages() error { + err := fs.WalkDir(ve.fsys, "pages", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -165,6 +153,12 @@ func (ve *HtmlViewEngine) loadPages() error { return ve.loadPage(path) }) + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil + } + + return err + } func (ve *HtmlViewEngine) loadPage(path string) error { @@ -189,15 +183,7 @@ func (ve *HtmlViewEngine) loadPage(path string) error { } func (ve *HtmlViewEngine) loadViews() error { - _, err := fs.Stat(ve.fsys, "views") - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err - } - - return fs.WalkDir(ve.fsys, "views", func(path string, d fs.DirEntry, err error) error { + err := fs.WalkDir(ve.fsys, "views", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -209,6 +195,12 @@ func (ve *HtmlViewEngine) loadViews() error { return ve.loadView(path) }) + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil + } + + return err + } func (ve *HtmlViewEngine) loadView(path string) error { diff --git a/viewengine_static.go b/viewengine_static.go index 760996c..c103ec3 100644 --- a/viewengine_static.go +++ b/viewengine_static.go @@ -12,15 +12,8 @@ type StaticViewEngine struct { } func (ve *StaticViewEngine) Load(fsys fs.FS, app *App) error { - _, err := fs.Stat(fsys, "public") - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err - } - return fs.WalkDir(fsys, "public", func(path string, d fs.DirEntry, err error) error { + err := fs.WalkDir(fsys, "public", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -31,6 +24,12 @@ func (ve *StaticViewEngine) Load(fsys fs.FS, app *App) error { return nil }) + + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil + } + + return err } func (ve *StaticViewEngine) FileChanged(fsys fs.FS, app *App, event fsnotify.Event) error {