Regex path redirects

This commit is contained in:
Jan-Lukas Else 2020-10-15 20:54:43 +02:00
parent dff1da6f68
commit a78883a679
5 changed files with 73 additions and 12 deletions

View File

@ -8,15 +8,16 @@ import (
)
type config struct {
Server *configServer `mapstructure:"server"`
Db *configDb `mapstructure:"database"`
Cache *configCache `mapstructure:"cache"`
DefaultBlog string `mapstructure:"defaultblog"`
Blogs map[string]*configBlog `mapstructure:"blogs"`
User *configUser `mapstructure:"user"`
Hooks *configHooks `mapstructure:"hooks"`
Hugo *configHugo `mapstructure:"hugo"`
Micropub *configMicropub `mapstructure:"micropub"`
Server *configServer `mapstructure:"server"`
Db *configDb `mapstructure:"database"`
Cache *configCache `mapstructure:"cache"`
DefaultBlog string `mapstructure:"defaultblog"`
Blogs map[string]*configBlog `mapstructure:"blogs"`
User *configUser `mapstructure:"user"`
Hooks *configHooks `mapstructure:"hooks"`
Hugo *configHugo `mapstructure:"hugo"`
Micropub *configMicropub `mapstructure:"micropub"`
PathRedirects []*configRegexRedirect `mapstructure:"pathRedirects"`
}
type configServer struct {
@ -135,6 +136,11 @@ type configMicropubMedia struct {
TinifyKey string `mapstructure:"tinifyKey"`
}
type configRegexRedirect struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
}
var appConfig = &config{}
func initConfig() error {

View File

@ -60,4 +60,11 @@ micropub:
bookmarkParam: link
audioParam: audio
photoParam: images
photoDescriptionParam: imagealts
photoDescriptionParam: imagealts
pathRedirects:
- from: "\\/index\\.xml"
to: ".rss"
- from: "\\/feed\\.json"
to: ".json"
- from: "\\/(feed|rss)\\/?$"
to: ".rss"

View File

@ -136,7 +136,7 @@ func buildHandler() (http.Handler, error) {
r.Get(path, serveAsset)
}
paginationPath := "/page/{page}"
paginationPath := "/page/{page:[0-9-]+}"
feedPath := ".{feed:rss|json|atom}"
for blog, blogConfig := range appConfig.Blogs {
@ -199,7 +199,8 @@ func buildHandler() (http.Handler, error) {
// Sitemap
r.With(cacheMiddleware).Get(sitemapPath, serveSitemap)
r.With(minifier.Middleware).NotFound(serve404)
// Check redirects, then serve 404
r.With(checkRegexRedirects, minifier.Middleware).NotFound(serve404)
return r, nil
}

View File

@ -45,6 +45,11 @@ func main() {
log.Fatal(err)
return
}
err = initRedirects()
if err != nil {
log.Fatal(err)
return
}
// Prepare graceful shutdown
quit := make(chan os.Signal, 1)

42
regexRedirects.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"net/http"
"regexp"
)
var regexRedirects []*regexRedirect
type regexRedirect struct {
From *regexp.Regexp
To string
}
func initRedirects() error {
for _, cr := range appConfig.PathRedirects {
re, err := regexp.Compile(cr.From)
if err != nil {
return err
}
regexRedirects = append(regexRedirects, &regexRedirect{
From: re,
To: cr.To,
})
}
return nil
}
func checkRegexRedirects(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
oldPath := r.URL.Path
for _, re := range regexRedirects {
newPath := re.From.ReplaceAllString(oldPath, re.To)
if oldPath != newPath {
r.URL.Path = newPath
http.Redirect(w, r, r.URL.String(), http.StatusFound)
return
}
}
next.ServeHTTP(w, r)
})
}