diff --git a/config.go b/config.go index cee26a9..5cc8cd1 100644 --- a/config.go +++ b/config.go @@ -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"` } diff --git a/example-config.yml b/example-config.yml index 02af752..b4bda79 100644 --- a/example-config.yml +++ b/example-config.yml @@ -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 diff --git a/httpRouters.go b/httpRouters.go index c1b28f1..6775101 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -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) } } } diff --git a/posts.go b/posts.go index 20c4854..77e4e6d 100644 --- a/posts.go +++ b/posts.go @@ -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