Updated static pages to use db

This commit is contained in:
2026-04-03 11:21:27 +02:00
parent 60461e49af
commit 12678f8241
7 changed files with 90 additions and 31 deletions
+36 -23
View File
@@ -4,15 +4,21 @@ import (
"os"
"path"
"path/filepath"
"quay/internal/config"
"quay/app/repository"
"regexp"
"github.com/gofiber/fiber/v3"
)
func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig) fiber.Handler {
func NewStaticHandler(storagePath string, siteRepo repository.SiteRepository) fiber.Handler {
return func(c fiber.Ctx) error {
site, ok := siteMap[c.Hostname()]
if !ok {
domain := c.Hostname()
site, err := siteRepo.GetSiteByDomain(domain)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to resolve site")
}
if site == nil {
return c.Status(fiber.StatusNotFound).SendString("Site not found")
}
@@ -22,25 +28,15 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig)
urlPath := filepath.Clean(c.Path())
for _, header := range site.CustomHeaders {
var matched bool
if header.Regex && header.Compiled != nil {
matched = header.Compiled.MatchString(urlPath)
} else {
matched, _ = path.Match(header.Source, urlPath)
}
if matched {
for key, value := range header.Headers {
c.Set(key, value)
}
}
}
for _, rule := range site.ForwardRules {
if rule.Regex && rule.Compiled != nil {
match := rule.Compiled.FindStringSubmatchIndex(urlPath)
if rule.Regex {
re, err := regexp.Compile(rule.Source)
if err != nil {
continue
}
match := re.FindStringSubmatchIndex(urlPath)
if match != nil {
dest := rule.Compiled.ExpandString([]byte{}, rule.Destination, urlPath, match)
dest := re.ExpandString([]byte{}, rule.Destination, urlPath, match)
return c.Redirect().Status(rule.StatusCode).To(string(dest))
}
} else if rule.Source == urlPath {
@@ -48,11 +44,28 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig)
}
}
for _, customHeader := range site.CustomHeaders {
var matched bool
if customHeader.Regex {
re, err := regexp.Compile(customHeader.Source)
if err == nil {
matched = re.MatchString(urlPath)
}
} else {
matched, _ = path.Match(customHeader.Source, urlPath)
}
if matched {
for _, header := range customHeader.Headers {
c.Set(header.Key, header.Value)
}
}
}
if urlPath == "/" || urlPath == "." {
urlPath = "/index.html"
}
basePath := filepath.Join(storagePath, site.Name)
basePath := filepath.Join(storagePath, site.ID)
filePath := filepath.Join(basePath, urlPath)
info, err := os.Stat(filePath)
@@ -67,7 +80,7 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig)
}
}
if site.SPA {
if site.Spa {
indexPath := filepath.Join(basePath, "index.html")
if _, err := os.Stat(indexPath); err == nil {
return c.SendFile(indexPath)