mirror of https://github.com/jlelse/GoBlog
Regex path redirects
This commit is contained in:
parent
dff1da6f68
commit
a78883a679
24
config.go
24
config.go
|
@ -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 {
|
||||
|
|
|
@ -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"
|
5
http.go
5
http.go
|
@ -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
|
||||
}
|
||||
|
|
5
main.go
5
main.go
|
@ -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)
|
||||
|
|
|
@ -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, ®exRedirect{
|
||||
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)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue