sitemap

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 6 Imported by: 6

README

Sitemap (Go)

build status report card godocs

Sitemap Protocol implementation for Go. Automatically handles Sitemap index files "/sitemap.xml".

Getting started

The only requirement is the Go Programming Language.

$ go get github.com/kataras/sitemap
import "github.com/kataras/sitemap"
sitemaps := sitemap.New("http://localhost:8080").
    URL(sitemap.URL{Loc: "/home"}).
    URL(sitemap.URL{Loc: "/articles", LastMod: time.Now(), ChangeFreq: sitemap.Daily, Priority: 1}).
    Build()
import "net/http"
for _, s := range sitemaps {
    http.Handle(s.Path, s)
}

http.ListenAndServe(":8080", nil)

Visit http://localhost:8080/sitemap.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://localhost:8080/home</loc>
    </url>
    <url>
        <loc>http://localhost:8080/articles</loc>
        <lastmod>2019-12-05T08:17:35+02:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>1</priority>
    </url>
</urlset>

For a more detailed technical documentation you can head over to our godocs. And for executable code you can always visit the _examples repository's subdirectory.

License

kataras/sitemap is free and open-source software licensed under the MIT License.

Documentation

Overview

Package sitemap implements the Sitemap Protocol. Reference: https://www.sitemaps.org/protocol.html

Index

Constants

View Source
const (
	Always  = "always"
	Hourly  = "hourly"
	Daily   = "daily"
	Weekly  = "weekly"
	Monthly = "monthly"
	Yearly  = "yearly"
	Never   = "never"
)

URL.ChangeFreq valid values.

View Source
const DefaultLang = "en"

DefaultLang is the default "hreflang" attribute of a self-included Link child element of URL.

Variables

View Source
var MaxURLsPerSitemap = 50000

MaxURLsPerSitemap is the limit of each sitemap, if more than number of urls are registered then sitemaps are automatically splitted and a sitemap index will be used. Defaults to 50000 as Sitemap Protocol specifies.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is the sitemaps Builder.

func New

func New(startURL string) *Builder

New returns a new sitemaps Builder. Use its `Add` to add one or more urls and `Build` once.

func (*Builder) Build

func (b *Builder) Build() (handlers []*Handler)

Build builds the sitemaps based on previous `Builder#URL` calls. It returns a list of sitemap Handlers. Each `Handler` is compatible with `net/http#Handler` and it contains further like the `Path`, `Pos` and if it's a sitemaps index handler.

func (*Builder) DefaultLang added in v0.0.4

func (b *Builder) DefaultLang(langCode string) *Builder

DefaultLang sets the default "hreflang" attribute of a self-included URL Link.

func (*Builder) ErrorHandler

func (b *Builder) ErrorHandler(fn func(err error) (handled bool)) *Builder

ErrorHandler sets the error handler.

func (*Builder) URL

func (b *Builder) URL(sitemapURLs ...URL) *Builder

URL adds a location of a Sitemap file determines the set of URLs that can be included in that Sitemap.

type Handler

type Handler struct {
	// Content returns the raw xml data.
	Content []byte
	// Pos returns the position, starting from 0.
	Pos int
	// Path returns the request path that this handler should be listening on.
	Path string
	// IsSitemapIndex reports whether this handler serves a Sitemap Index File.
	IsSitemapIndex bool
}

Handler is a sitemap handler. The result of `Builder#Build`.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Link struct {
	Rel      string `xml:"rel,attr"`
	Hreflang string `xml:"hreflang,attr"`
	Href     string `xml:"href,attr"`
}

Link is the optional child element of a URL. It can be used to list every alternate version of the page.

Read more at: https://support.google.com/webmasters/answer/189077?hl=en.

type URL

type URL struct {
	// Loc is required. It defines the URL of the page.
	// This URL must begin with the protocol (such as http) and end with a trailing slash,
	// if your web server requires it. This value must be less than 2,048 characters.
	// Read more at: https://www.sitemaps.org/protocol.html#location
	Loc string `xml:"loc"`
	// LastMod is optional. It is the date of last modification of the file.
	LastMod time.Time `xml:"-"`
	// LastModStr do NOT set it directly,
	// other solution would be to use ptr or custom time marshaler but this will ruin the API's expressiveness.
	//
	// See internal `sitemap#Add`.
	LastModStr string `xml:"lastmod,omitempty"`
	// ChangeFreq is optional. Defines how frequently the page is likely to change.
	// This value provides general information to search engines and may not correlate exactly to how often they crawl the page.
	// Valid values are:
	// "always"
	// "hourly"
	// "daily"
	// "weekly"
	// "monthly"
	// "yearly"
	// "never"
	ChangeFreq string `xml:"changefreq,omitempty"`
	// Priority is optional. It defines the priority of this URL relative to other URLs on your site.
	// Valid values range from 0.0 to 1.0.
	//
	// The default priority of a page is 0.5.
	Priority float32 `xml:"priority,omitempty"`

	Links []Link `xml:"xhtml:link,omitempty"`
}

URL is the parent tag for each URL entry.

func (u *URL) AddLink(link Link)

AddLink adds a link to this URL.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL