From 00684f9d2e8e5d6af952405f07f11093e7be29bd Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 29 Aug 2020 01:21:42 +0300 Subject: [PATCH] add Route.ExcludeSitemap method to exclude a route from sitemap, also exclude the offline routes as requested --- _examples/routing/sitemap/main.go | 4 ++++ configuration.go | 7 ++++++- core/router/route.go | 11 +++++++++++ mvc/reflect.go | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/_examples/routing/sitemap/main.go b/_examples/routing/sitemap/main.go index 31b2a9f7..fa7c9f7d 100644 --- a/_examples/routing/sitemap/main.go +++ b/_examples/routing/sitemap/main.go @@ -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 } diff --git a/configuration.go b/configuration.go index 1e6988f9..470fa825 100644 --- a/configuration.go +++ b/configuration.go @@ -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 } diff --git a/core/router/route.go b/core/router/route.go index d5817425..c0a01042 100644 --- a/core/router/route.go +++ b/core/router/route.go @@ -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 diff --git a/mvc/reflect.go b/mvc/reflect.go index 0d336d6c..1957cbaf 100644 --- a/mvc/reflect.go +++ b/mvc/reflect.go @@ -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()