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
+19
View File
@@ -52,6 +52,25 @@ func (c *CachedSiteRepository) GetSite(id string) (*models.Site, error) {
return s, nil
}
func (c *CachedSiteRepository) GetSiteByDomain(domain string) (*models.Site, error) {
c.mu.RLock()
if s, ok := c.sites[domain]; ok {
c.mu.RUnlock()
return s, nil
}
c.mu.RUnlock()
s, err := c.inner.GetSiteByDomain(domain)
if err != nil {
return nil, err
}
c.mu.Lock()
c.sites[domain] = s
c.mu.Unlock()
return s, nil
}
func (c *CachedSiteRepository) ListSites() ([]models.Site, error) {
c.mu.RLock()
if c.siteListValid {
+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)
+1
View File
@@ -17,6 +17,7 @@ type Header struct {
type CustomHeaders struct {
ID string `json:"id"`
Source string `json:"source"`
Regex bool `json:"regex"`
Headers []Header `json:"headers"`
}
+1
View File
@@ -4,6 +4,7 @@ import "quay/app/models"
type SiteRepository interface {
GetSite(id string) (*models.Site, error)
GetSiteByDomain(domain string) (*models.Site, error)
ListSites() ([]models.Site, error)
CreateSite(s *models.Site) error
UpdateSite(s *models.Site) error
+1 -1
View File
@@ -66,5 +66,5 @@ func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig, d
siteMap[site.Domain] = site
}
app.Use(handlers.NewStaticHandler(storagePath, siteMap))
app.Use(handlers.NewStaticHandler(storagePath, siteRepository))
}