Added forward rules
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,6 +6,12 @@ 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"`
|
||||
@@ -15,6 +21,7 @@ type SiteConfig struct {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user