Added static serving
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"quay/internal/config"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig) fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
site, ok := siteMap[c.Hostname()]
|
||||
if !ok {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
urlPath := filepath.Clean(c.Path())
|
||||
|
||||
// Serve index.html for root
|
||||
if urlPath == "/" || urlPath == "." {
|
||||
urlPath = "/index.html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join(storagePath, site.Name, 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")
|
||||
}
|
||||
|
||||
// SPA fallback
|
||||
if site.SPA {
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
filePath = filepath.Join(storagePath, site.Name, "index.html")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
//log.Printf("File not found: %s (requested by host: %s, path: %s)", filePath, c.Hostname(), c.Path())
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
//log.Printf("Serving file: %s (requested by host: %s, path: %s)", filePath, c.Hostname(), c.Path())
|
||||
|
||||
return c.SendFile(filePath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user