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())
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user