From b1079efe5874ec96d657bf57a0d0c65f819393c8 Mon Sep 17 00:00:00 2001 From: KartoffelChips <104089082+KartoffelChipss@users.noreply.github.com> Date: Mon, 30 Mar 2026 21:46:08 +0200 Subject: [PATCH] Made static route more efficient --- app/handlers/static.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/app/handlers/static.go b/app/handlers/static.go index b3c16cc..c19db80 100644 --- a/app/handlers/static.go +++ b/app/handlers/static.go @@ -16,35 +16,39 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig) } urlPath := filepath.Clean(c.Path()) - - // Serve index.html for root if urlPath == "/" || urlPath == "." { urlPath = "/index.html" } - filePath := filepath.Join(storagePath, site.Name, urlPath) + basePath := filepath.Join(storagePath, site.Name) + filePath := filepath.Join(basePath, urlPath) - // If it's a directory, try index.html inside it - if info, err := os.Stat(filePath); err == nil && info.IsDir() { - filePath = filepath.Join(filePath, "index.html") + info, err := os.Stat(filePath) + if err == nil { + if info.IsDir() { + indexPath := filepath.Join(filePath, "index.html") + if _, err := os.Stat(indexPath); err == nil { + return c.SendFile(indexPath) + } + } else { + return c.SendFile(filePath) + } } - // SPA fallback if site.SPA { - if _, err := os.Stat(filePath); os.IsNotExist(err) { - filePath = filepath.Join(storagePath, site.Name, "index.html") + indexPath := filepath.Join(basePath, "index.html") + if _, err := os.Stat(indexPath); err == nil { + return c.SendFile(indexPath) } } - if _, err := os.Stat(filePath); os.IsNotExist(err) { - notFoundFilePath := filepath.Join(storagePath, site.Name, site.NotFoundFile) - if _, err := os.Stat(notFoundFilePath); err == nil { - return c.SendFile(notFoundFilePath) + if site.NotFoundFile != "" { + notFoundPath := filepath.Join(basePath, site.NotFoundFile) + if _, err := os.Stat(notFoundPath); err == nil { + return c.SendFile(notFoundPath) } - - return c.SendStatus(fiber.StatusNotFound) } - return c.SendFile(filePath) + return c.SendStatus(fiber.StatusNotFound) } }