Made static route more efficient
This commit is contained in:
+20
-16
@@ -16,35 +16,39 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlPath := filepath.Clean(c.Path())
|
urlPath := filepath.Clean(c.Path())
|
||||||
|
|
||||||
// Serve index.html for root
|
|
||||||
if urlPath == "/" || urlPath == "." {
|
if urlPath == "/" || urlPath == "." {
|
||||||
urlPath = "/index.html"
|
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
|
info, err := os.Stat(filePath)
|
||||||
if info, err := os.Stat(filePath); err == nil && info.IsDir() {
|
if err == nil {
|
||||||
filePath = filepath.Join(filePath, "index.html")
|
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 site.SPA {
|
||||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
indexPath := filepath.Join(basePath, "index.html")
|
||||||
filePath = filepath.Join(storagePath, site.Name, "index.html")
|
if _, err := os.Stat(indexPath); err == nil {
|
||||||
|
return c.SendFile(indexPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
if site.NotFoundFile != "" {
|
||||||
notFoundFilePath := filepath.Join(storagePath, site.Name, site.NotFoundFile)
|
notFoundPath := filepath.Join(basePath, site.NotFoundFile)
|
||||||
if _, err := os.Stat(notFoundFilePath); err == nil {
|
if _, err := os.Stat(notFoundPath); err == nil {
|
||||||
return c.SendFile(notFoundFilePath)
|
return c.SendFile(notFoundPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusNotFound)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.SendFile(filePath)
|
return c.SendStatus(fiber.StatusNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user