mirror of https://github.com/jlelse/GoBlog
Add "On this Day" redirect
This commit is contained in:
parent
9eb9b50c0a
commit
bf812d9650
|
@ -85,6 +85,7 @@ type configBlog struct {
|
||||||
Telegram *configTelegram `mapstructure:"telegram"`
|
Telegram *configTelegram `mapstructure:"telegram"`
|
||||||
PostAsHome bool `mapstructure:"postAsHome"`
|
PostAsHome bool `mapstructure:"postAsHome"`
|
||||||
RandomPost *configRandomPost `mapstructure:"randomPost"`
|
RandomPost *configRandomPost `mapstructure:"randomPost"`
|
||||||
|
OnThisDay *configOnThisDay `mapstructure:"onThisDay"`
|
||||||
Comments *configComments `mapstructure:"comments"`
|
Comments *configComments `mapstructure:"comments"`
|
||||||
Map *configGeoMap `mapstructure:"map"`
|
Map *configGeoMap `mapstructure:"map"`
|
||||||
Contact *configContact `mapstructure:"contact"`
|
Contact *configContact `mapstructure:"contact"`
|
||||||
|
@ -160,6 +161,11 @@ type configRandomPost struct {
|
||||||
Path string `mapstructure:"path"`
|
Path string `mapstructure:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type configOnThisDay struct {
|
||||||
|
Enabled bool `mapstructure:"enabled"`
|
||||||
|
Path string `mapstructure:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
type configComments struct {
|
type configComments struct {
|
||||||
Enabled bool `mapstructure:"enabled"`
|
Enabled bool `mapstructure:"enabled"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -235,6 +235,10 @@ blogs:
|
||||||
randomPost:
|
randomPost:
|
||||||
enabled: true # Enable
|
enabled: true # Enable
|
||||||
path: /random # Path
|
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
|
# Send notifications about new posts to Telegram channel
|
||||||
telegram:
|
telegram:
|
||||||
enabled: true # Enable
|
enabled: true # Enable
|
||||||
|
|
|
@ -134,6 +134,9 @@ func (a *goBlog) blogRouter(blog string, conf *configBlog) func(r chi.Router) {
|
||||||
// Random post
|
// Random post
|
||||||
r.Group(a.blogRandomRouter(conf))
|
r.Group(a.blogRandomRouter(conf))
|
||||||
|
|
||||||
|
// On this day
|
||||||
|
r.Group(a.blogOnThisDayRouter(conf))
|
||||||
|
|
||||||
// Editor
|
// Editor
|
||||||
r.Route(conf.getRelativePath(editorPath), a.blogEditorRouter(conf))
|
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) {
|
func (a *goBlog) blogRandomRouter(conf *configBlog) func(r chi.Router) {
|
||||||
return func(r chi.Router) {
|
return func(r chi.Router) {
|
||||||
if rp := conf.RandomPost; rp != nil && rp.Enabled {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
posts.go
18
posts.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/vcraescu/go-paginator"
|
"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) {
|
func (a *goBlog) redirectToRandomPost(rw http.ResponseWriter, r *http.Request) {
|
||||||
blog, _ := a.getBlog(r)
|
blog, _ := a.getBlog(r)
|
||||||
randomPath, err := a.getRandomPostPath(blog)
|
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)
|
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 {
|
type postPaginationAdapter struct {
|
||||||
config *postsRequestConfig
|
config *postsRequestConfig
|
||||||
nums int64
|
nums int64
|
||||||
|
|
Loading…
Reference in New Issue