Added forward rules

This commit is contained in:
2026-03-31 12:33:03 +02:00
parent bedf8bc335
commit 55348057a0
3 changed files with 48 additions and 8 deletions
+7
View File
@@ -16,6 +16,13 @@ func NewStaticHandler(storagePath string, siteMap map[string]config.SiteConfig)
} }
urlPath := filepath.Clean(c.Path()) 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 == "." { if urlPath == "/" || urlPath == "." {
urlPath = "/index.html" urlPath = "/index.html"
} }
+7
View File
@@ -8,6 +8,13 @@ sites:
branch: gh-pages branch: gh-pages
domain: docs.my-lib.com domain: docs.my-lib.com
deploy_token: "${MY_LIB_DEPLOY_TOKEN}" 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 - repo: portfolio
owner: yourname owner: yourname
+34 -8
View File
@@ -6,15 +6,22 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
type ForwardRule struct {
Source string `yaml:"source"`
Destination string `yaml:"destination"`
StatusCode int `yaml:"status_code"`
}
type SiteConfig struct { type SiteConfig struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Repo string `yaml:"repo"` Repo string `yaml:"repo"`
Owner string `yaml:"owner"` Owner string `yaml:"owner"`
Branch string `yaml:"branch"` Branch string `yaml:"branch"`
Domain string `yaml:"domain"` Domain string `yaml:"domain"`
SPA bool `yaml:"spa"` SPA bool `yaml:"spa"`
NotFoundFile string `yaml:"not_found_file"` NotFoundFile string `yaml:"not_found_file"`
DeployToken string `yaml:"deploy_token"` DeployToken string `yaml:"deploy_token"`
ForwardRules []ForwardRule `yaml:"forward_rules"`
} }
type Config struct { type Config struct {
@@ -47,6 +54,20 @@ func validateConfig(config *Config) error {
if site.Domain == "" { if site.Domain == "" {
return &Error{Field: "sites[" + string(i) + "].domain", Message: "Domain is required"} 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 return nil
} }
@@ -56,6 +77,11 @@ func applyDefaults(config *Config) {
if config.Sites[i].NotFoundFile == "" { if config.Sites[i].NotFoundFile == "" {
config.Sites[i].NotFoundFile = "404.html" 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
}
}
} }
} }