Documentation
¶
Overview ¶
Package sitemap allows dynamic generation of a sitemap. This package allows to:
- Register routes when binding handlers (embedding a github.com/gorilla/mux.Router),
- Create a sitemap on request (lazy-creation),
- Update the sitemap regularly and thread-safely.
The sitemap is stored (=cached) in XML format on disk, and served directly from there.
Example of use:
1. Create the router:
r := sitemap.NewRouter(mux.NewRouter(), "http://example.com", "local/path/to/sitemaps/cache")
2. Static route handler:
r.Register("/my/static/route").Handler(handler)
... or a secret route (i.e. not appearing in the sitemap):
r.HandleFunc("/my/secret/route", f)
3. Parameterized route:
documents := []struct { Category string Id string }{{ Category: "book", Id: "AAA", }, { Category: "book", Id: "BBB", }, { Category: "html", Id: "WWW", }} r.RegisterParam("/documents/{category}/{id:[A-Z]+}", func(cb func(...string) error) error { for _, doc := range documents { err := cb("category", doc.Category, "id", doc.Id) if err != nil { return err } } return nil }).Handler(h)
4. Handle sitemap requests:
r.HandleSitemaps() http.Handle("/", r)
So that an http GET on (r.Options.ServerPath + "sitemapindex.xml") returns:
<?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"> <sitemap> <loc>http://example.com/sitemap_1.xml</loc> </sitemap> </sitemapindex>
and (r.Options.ServerPath + "sitemap_1.xml") returns:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <url> <loc>http://example.com/my/static/route</loc> <priority>0.5</priority> </url> <url> <loc>http://example.com/documents/book/AAA</loc> <priority>0.5</priority> </url> <url> <loc>http://example.com/documents/book/BBB</loc> <priority>0.5</priority> </url> <url> <loc>http://example.com/documents/html/WWW</loc> <priority>0.5</priority> </url> </urlset>
Index ¶
Constants ¶
const ( Always ChangeFrequency = "always" Hourly = "hourly" Daily = "daily" Weekly = "weekly" Monthly = "monthly" Yearly = "yearly" Never = "never" )
Variables ¶
var DefaultOptions = &Options{
ServerPath: "/",
DefaultPriority: 0.5,
}
DefaultOptions is the default options used when calling NewRouter().
var SitemapIndexSchema = &Schema{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd",
}
SitemapIndexSchema is the XML schema used for sitemap indexes.
var SitemapSchema = &Schema{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
}
SitemapSchema is the XML schema used for sitemaps.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct { Locations []string // Relative path of serialized sitemaps. // contains filtered or unexported fields }
Buffer is a sitemap buffer.
When the current sitemap is full, it is offloaded to disk, and a new empty sitemap is created.
func NewBuffer ¶
NewBuffer creates a new buffer for sitemaps on the given domain. The path variable is the location for serialization on Flush().
type ChangeFrequency ¶
type ChangeFrequency string
ChangeFrequency is an optional attribute for sitemap entries.
type Entry ¶
type Entry struct { *FileReference ChangeFrequency ChangeFrequency `xml:"changefreq,omitempty"` // optional Priority *float64 `xml:"priority,omitempty"` // optional }
Entry is a sitemap entry (a url block in the XML file).
type FileReference ¶
type FileReference struct { Location string `xml:"loc"` LastModification *time.Time `xml:"lastmod,omitempty"` // optional }
FileReference is a reference to a file (given by full URL) and the last modification.
type Options ¶
type Options struct { CachePath string // path of a directory, to store sitemaps on disk ServerPath string // server path for sitemaps DefaultPriority float64 // default priority for sitemap entries Domain string // domain for entries in the sitemap (multiple domains are not supported) }
Options is used by Router.
type Router ¶
Router has an embedded github.com/gorilla/mux.Router. It retains all functionalities of the router (unchanged) and has a few extra methods to register the routes which should belong to the sitemap.
See Register(), RegisterParam() and HandleSitemaps().
func NewRouter ¶
NewRouter wraps router into a new Router, ready to register sitemap urls for the given domain.
localPath is the path where to store the sitemaps when created.
Change the routers options if you want more control on the sitemap creation:
r := NewRouter(router, "example.com", "cache/sitemaps") r.Options.DefaultPriority = 1 r.ServerPath = "/sitemaps/" // don't forget the trailing slash!
func (*Router) GenerateSitemaps ¶
GenerateSitemaps creates sitemapindex.xml and as many sitemaps as needed. Since there are size restrictions on a sitemap, there may be more than one. In this case they are named sitemap_1.xml, sitemap_2.xml, and so on.
All files created is returned (paths relative to r.Options.CachePath).
It is safe to call GenerateSitemaps() even when they are served due to a call to HandleSitemaps(). A read-write lock takes care of queueing requests until the sitemaps are generated.
func (*Router) HandleSitemaps ¶
HandleSitemaps register routes to serve the sitemap files on the router. The http handler is returned.
All routes registered are:
r.Options.ServerPath + "sitemapindex.xml" r.Options.ServerPath + "sitemap_%d.xml" // where %d is a replaced by a positive integer.
func (*Router) Register ¶
Register creates a static route (no variables in the path) and adds it to the sitemap.
func (*Router) RegisterParam ¶
func (r *Router) RegisterParam(pattern string, enum VariableEnumerator) *mux.Route
RegisterParam creates a route with parameters (=variables) in the path. Each time the sitemap is (re-)created, enum is called to get the list of allowed variable values.
See the package's main documentation for an example.
func (*Router) SitemapHandler ¶
SitemapHandler creates and returns a new http.Handler for sitemaps. It expects to serve r,Options.ServerPath + `{file:sitemap(index|_\d+)\.xml}`.
type Schema ¶
type Schema struct { Xmlns string `xml:"xmlns,attr"` XmlnsXsi string `xml:"xmlns:xsi,attr"` XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"` }
Schema represents an XML schema.
type Sitemap ¶
Sitemap is a sitemap, with xml-encoding attributes.
Call NewSitemap() to get a new sitemap with the correct XML schema, ready to get encoded.
func NewSitemap ¶
func NewSitemap() *Sitemap
NewSitemap creates an empty sitemap with the schema set as SitemapSchema.
func (*Sitemap) IsFull ¶
IsFull retruns true if the sitemap has reached the maximum number of entries allowed.
func (*Sitemap) WriteToFile ¶
WriteToFile encodes the sitemap in XML format into path.
type SitemapIndex ¶
type SitemapIndex struct { XMLName xml.Name `xml:"sitemapindex"` *Schema SitemapRefs []*FileReference `xml:"sitemap"` }
SitemapIndex is a sitemap index with xml-encoding attributes.
func NewSitemapIndex ¶
func NewSitemapIndex(sitemapUrls []string) *SitemapIndex
NewSitemapIndex creates a sitemap index with the default schema and all sitemap urls given.
func (*SitemapIndex) WriteToFile ¶
func (s *SitemapIndex) WriteToFile(path string) error
WriteToFile writes the sitemap index in XML into path.
type VariableEnumerator ¶
VariableEnumerator calls the callback as many times as there are routes allowed.
The arguments passed to the callback should be as needed by github.com/gorilla/mux.Route.URL().
See the package's main documentation for an example.