Added frontend

This commit is contained in:
2026-04-03 11:51:36 +02:00
parent 12678f8241
commit 29ee01afba
30 changed files with 1 additions and 337 deletions
+99
View File
@@ -0,0 +1,99 @@
package handlers
import (
"os"
"path"
"path/filepath"
"quay/app/repository"
"regexp"
"github.com/gofiber/fiber/v3"
)
func NewStaticHandler(storagePath string, siteRepo repository.SiteRepository) fiber.Handler {
return func(c fiber.Ctx) error {
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")
}
if !site.Enabled {
return c.Status(fiber.StatusServiceUnavailable).SendString("Site is currently unavailable")
}
urlPath := filepath.Clean(c.Path())
for _, rule := range site.ForwardRules {
if rule.Regex {
re, err := regexp.Compile(rule.Source)
if err != nil {
continue
}
match := re.FindStringSubmatchIndex(urlPath)
if match != nil {
dest := re.ExpandString([]byte{}, rule.Destination, urlPath, match)
return c.Redirect().Status(rule.StatusCode).To(string(dest))
}
} else if rule.Source == urlPath {
return c.Redirect().Status(rule.StatusCode).To(rule.Destination)
}
}
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.ID)
filePath := filepath.Join(basePath, urlPath)
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)
}
}
if site.Spa {
indexPath := filepath.Join(basePath, "index.html")
if _, err := os.Stat(indexPath); err == nil {
return c.SendFile(indexPath)
}
}
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)
}
}