From 55348057a0c6ade8af11b2a27dba777c9e7c6afb Mon Sep 17 00:00:00 2001 From: KartoffelChips <104089082+KartoffelChipss@users.noreply.github.com> Date: Tue, 31 Mar 2026 12:33:03 +0200 Subject: [PATCH] Added forward rules --- app/handlers/static.go | 7 +++++++ config.example.yaml | 7 +++++++ internal/config/config.go | 42 +++++++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/app/handlers/static.go b/app/handlers/static.go index 1dddb27..1496dd2 100644 --- a/app/handlers/static.go +++ b/app/handlers/static.go @@ -16,6 +16,13 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig) } urlPath := filepath.Clean(c.Path()) + + for _, rule := range site.ForwardRules { + if rule.Source == urlPath { + return c.Redirect().Status(rule.StatusCode).To(rule.Destination) + } + } + if urlPath == "/" || urlPath == "." { urlPath = "/index.html" } diff --git a/config.example.yaml b/config.example.yaml index aea2f43..4189a63 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -8,6 +8,13 @@ sites: branch: gh-pages domain: docs.my-lib.com deploy_token: "${MY_LIB_DEPLOY_TOKEN}" + forward_rules: + - source: /npm + destination: https://www.npmjs.com/package/my-lib + status_code: 302 + - source: /github + destination: https://github.com/yourname/my-lib + status_code: 302 - repo: portfolio owner: yourname diff --git a/internal/config/config.go b/internal/config/config.go index 2c7025d..60d4253 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,15 +6,22 @@ import ( "gopkg.in/yaml.v3" ) +type ForwardRule struct { + Source string `yaml:"source"` + Destination string `yaml:"destination"` + StatusCode int `yaml:"status_code"` +} + type SiteConfig struct { - Name string `yaml:"name"` - Repo string `yaml:"repo"` - Owner string `yaml:"owner"` - Branch string `yaml:"branch"` - Domain string `yaml:"domain"` - SPA bool `yaml:"spa"` - NotFoundFile string `yaml:"not_found_file"` - DeployToken string `yaml:"deploy_token"` + Name string `yaml:"name"` + Repo string `yaml:"repo"` + Owner string `yaml:"owner"` + Branch string `yaml:"branch"` + Domain string `yaml:"domain"` + SPA bool `yaml:"spa"` + NotFoundFile string `yaml:"not_found_file"` + DeployToken string `yaml:"deploy_token"` + ForwardRules []ForwardRule `yaml:"forward_rules"` } type Config struct { @@ -47,6 +54,20 @@ func validateConfig(config *Config) error { if site.Domain == "" { return &Error{Field: "sites[" + string(i) + "].domain", Message: "Domain is required"} } + for j, rule := range site.ForwardRules { + if rule.Source == "" { + return &Error{Field: "sites[" + string(i) + "].forward_rules[" + string(j) + "].source", Message: "Source is required"} + } + if rule.Destination == "" { + return &Error{Field: "sites[" + string(i) + "].forward_rules[" + string(j) + "].destination", Message: "Destination is required"} + } + if rule.StatusCode == 0 { + return &Error{Field: "sites[" + string(i) + "].forward_rules[" + string(j) + "].status_code", Message: "Status code is required"} + } + if rule.StatusCode < 300 || rule.StatusCode >= 400 { + return &Error{Field: "sites[" + string(i) + "].forward_rules[" + string(j) + "].status_code", Message: "Status code must be a valid redirect code (300-399)"} + } + } } return nil } @@ -56,6 +77,11 @@ func applyDefaults(config *Config) { if config.Sites[i].NotFoundFile == "" { config.Sites[i].NotFoundFile = "404.html" } + for j := range config.Sites[i].ForwardRules { + if config.Sites[i].ForwardRules[j].StatusCode == 0 { + config.Sites[i].ForwardRules[j].StatusCode = 302 + } + } } }