Add "On this Day" redirect

This commit is contained in:
Jan-Lukas Else 2022-01-26 21:08:46 +01:00
parent 9eb9b50c0a
commit bf812d9650
4 changed files with 41 additions and 1 deletions

View File

@ -85,6 +85,7 @@ type configBlog struct {
Telegram *configTelegram `mapstructure:"telegram"`
PostAsHome bool `mapstructure:"postAsHome"`
RandomPost *configRandomPost `mapstructure:"randomPost"`
OnThisDay *configOnThisDay `mapstructure:"onThisDay"`
Comments *configComments `mapstructure:"comments"`
Map *configGeoMap `mapstructure:"map"`
Contact *configContact `mapstructure:"contact"`
@ -160,6 +161,11 @@ type configRandomPost struct {
Path string `mapstructure:"path"`
}
type configOnThisDay struct {
Enabled bool `mapstructure:"enabled"`
Path string `mapstructure:"path"`
}
type configComments struct {
Enabled bool `mapstructure:"enabled"`
}

View File

@ -235,6 +235,10 @@ blogs:
randomPost:
enabled: true # Enable
path: /random # Path
# Redirect to archive of the current day in previous years
onThisDay:
enabled: true # Enable
path: /onthisday # Path
# Send notifications about new posts to Telegram channel
telegram:
enabled: true # Enable

View File

@ -134,6 +134,9 @@ func (a *goBlog) blogRouter(blog string, conf *configBlog) func(r chi.Router) {
// Random post
r.Group(a.blogRandomRouter(conf))
// On this day
r.Group(a.blogOnThisDayRouter(conf))
// Editor
r.Route(conf.getRelativePath(editorPath), a.blogEditorRouter(conf))
@ -319,7 +322,16 @@ func (a *goBlog) blogCustomPagesRouter(conf *configBlog) func(r chi.Router) {
func (a *goBlog) blogRandomRouter(conf *configBlog) func(r chi.Router) {
return func(r chi.Router) {
if rp := conf.RandomPost; rp != nil && rp.Enabled {
r.With(a.privateModeHandler).Get(conf.getRelativePath(defaultIfEmpty(rp.Path, "/random")), a.redirectToRandomPost)
r.With(a.privateModeHandler).Get(conf.getRelativePath(defaultIfEmpty(rp.Path, defaultRandomPath)), a.redirectToRandomPost)
}
}
}
// Blog - On this day
func (a *goBlog) blogOnThisDayRouter(conf *configBlog) func(r chi.Router) {
return func(r chi.Router) {
if otd := conf.OnThisDay; otd != nil && otd.Enabled {
r.With(a.privateModeHandler).Get(conf.getRelativePath(defaultIfEmpty(otd.Path, defaultOnThisDayPath)), a.redirectToOnThisDay)
}
}
}

View File

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
"github.com/vcraescu/go-paginator"
@ -89,6 +90,8 @@ func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
})
}
const defaultRandomPath = "/random"
func (a *goBlog) redirectToRandomPost(rw http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
randomPath, err := a.getRandomPostPath(blog)
@ -99,6 +102,21 @@ func (a *goBlog) redirectToRandomPost(rw http.ResponseWriter, r *http.Request) {
http.Redirect(rw, r, randomPath, http.StatusFound)
}
const defaultOnThisDayPath = "/onthisday"
func (a *goBlog) redirectToOnThisDay(w http.ResponseWriter, r *http.Request) {
_, bc := a.getBlog(r)
// Get current local month and day
now := time.Now()
month := now.Month()
day := now.Day()
// Build the path
targetPath := fmt.Sprintf("/x/%02d/%02d", month, day)
targetPath = bc.getRelativePath(targetPath)
// Redirect
http.Redirect(w, r, targetPath, http.StatusFound)
}
type postPaginationAdapter struct {
config *postsRequestConfig
nums int64