add Route.ExcludeSitemap method to exclude a route from sitemap, also exclude the offline routes as requested

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-29 01:21:42 +03:00
parent 6f9a453160
commit 00684f9d2e
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
4 changed files with 22 additions and 2 deletions

View File

@ -32,6 +32,10 @@ func newApp() *iris.Application {
app.Get("/this/{myparam}/should/not/be/listed", handler)
app.Get("/this-should-not-be-listed-offline", handler).SetStatusOffline()
// These should be excluded as well
app.Get("/about", handler).ExcludeSitemap()
app.Get("/offline", handler).SetStatusOffline()
return app
}

View File

@ -439,6 +439,11 @@ func WithOtherValue(key string, val interface{}) Configurator {
// WithSitemap enables the sitemap generator.
// Use the Route's `SetLastMod`, `SetChangeFreq` and `SetPriority` to modify
// the sitemap's URL child element properties.
// Excluded routes:
// - dynamic
// - subdomain
// - offline
// - ExcludeSitemap method called
//
// It accepts a "startURL" input argument which
// is the prefix for the registered routes that will be included in the sitemap.
@ -462,7 +467,7 @@ func WithSitemap(startURL string) Configurator {
}
for _, r := range app.GetRoutes() {
if !r.IsStatic() || r.Subdomain != "" {
if !r.IsStatic() || r.Subdomain != "" || !r.IsOnline() || r.NoSitemap {
continue
}

View File

@ -68,6 +68,7 @@ type Route struct {
overlappedLink *Route
// Sitemap properties: https://www.sitemaps.org/protocol.html
NoSitemap bool // when this route should be hidden from sitemap.
LastMod time.Time `json:"lastMod,omitempty"`
ChangeFreq string `json:"changeFreq,omitempty"`
Priority float32 `json:"priority,omitempty"`
@ -238,6 +239,16 @@ func (r *Route) DeepEqual(other *Route) bool {
return r.Equal(other) && r.tmpl.Src == other.tmpl.Src
}
// ExcludeSitemap excludes this route page from sitemap generator.
// It sets the NoSitemap field to true.
//
// See `SetLastMod`, `SetChangeFreq`, `SetPriority` methods
// and `iris.WithSitemap`.
func (r *Route) ExcludeSitemap() *Route {
r.NoSitemap = true
return r
}
// SetLastMod sets the date of last modification of the file served by this static GET route.
func (r *Route) SetLastMod(t time.Time) *Route {
r.LastMod = t

View File

@ -43,7 +43,7 @@ func getSourceFileLine(ctrlType reflect.Type, m reflect.Method) (string, int) {
// }
// but BaseCtrl has not the method, *BaseCtrl does:
// (c *BaseCtrl) HandleHTTPError(...)
// so we are creating a new temporarly value ptr of that type
// so we are creating a new temporary value ptr of that type
// and searching inside it for the method instead.
typ = reflect.New(typ).Type()