sitemap

package module
v0.0.0-...-bf245c0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: BSD-3-Clause Imports: 14 Imported by: 1

README

sitemap GoDoc

Generates sitemaps and index files based on the sitemaps.org protocol.

sitemap.NewSitemapGroup(dir string,name string)

Creates a new group of sitemaps that used a common name.

group := sitemap.NewSitemapGroup("/var/www/blog/public/sitemaps/","blog")

If the sitemap exceed the limit of 50k urls, new sitemaps will have a numeric suffix to the name. Example:

  • blog_1.xml.gz
  • blog_2.xml.gz
group.Add(url sitemap.URL)

Add sitemap.URL to group

now := time.Now()
group.Add(sitemap.URL{
    Loc: "http://example.com/blog/1/",
    ChangeFreq: sitemap.Hourly,
    LastMod: &now,
    Priority: 0.9
    })
group.Close()

Handle the rest of the url that has not been added to any sitemap and add, in addition to clean variables and close the channel group

sitemap.CloseGroups(groups ...*SitemapGroup) (done <-chan bool)

if you use several groups of sitemap is safer use this function to close all groups for you before creating the index. Returns a channel with the done signal.

	//release after close all groups
	<-sitemap.CloseGroups(group, group2)

	//generate index - by last execution paths
	savedSitemaps := group.GetSavedSitemaps()
	sitemapsgroup2 := group.GetSavedSitemaps()
	savedSitemaps = append(savedSitemaps, sitemapsgroup2...)

Creating the index file

Currently we have 2 ways to create the index, searching for files in the directory or passing a slice of urls to sitemaps. To generate the slice of sitemaps generated in the last run we GetSavedSitemaps function.

group.GetSavedSitemaps() []string

Returns an array of urls in the sitemaps created script execution

savedSitemaps := group.GetSavedSitemaps()
sitemap.CreateIndexBySlice(savedSitemaps, path) sitemap.Index
index := sitemap.CreateIndexBySlice(savedSitemaps, "http://example.com.br/sitemaps/")

#####OR

sitemap.CreateIndexByScanDir(sitemaps_dir,index_path, path) sitemap.Index

Search all the xml.gz sitemaps_dir directory, uses the modified date of the file as lastModified

path_index is included for the function does not include the url of the index in your own content, if it is present in the same directory.

index := sitemap.CreateIndexByScanDir("/var/www/blog/public/sitemaps/", "/var/www/blog/public/index.xml.gz", "http://example.com.br/sitemaps/")

Warning: this release do not control old sitemaps, when using this method the index can be created with sitemaps that are no longer used. In case you need to delete manually.

sitemap.CreateSitemapIndex(path, sitemap.Index)

creates and gzip the xml index

sitemap.CreateSitemapIndex("/var/www/blog/public/index.xml.gz", index)
sitemap.PingSearchEngines(public_index_path)

Sends a ping to search engines indicating that the index has been updated.

Currently supports Google and Bing.

sitemap.PingSearchEngines("http://exemple.com/index.xml.gz")

Example

There is a very simple example of using the example folder.

Documentation

Overview

Generates sitemaps and index files based on the sitemaps.org protocol. facilitates the creation of sitemaps for large amounts of urls. For a full guide visit https://github.com/StudioSol/Sitemap

Index

Constants

View Source
const (
	XMLNS         = "http://www.sitemaps.org/schemas/sitemap/0.9"
	XMLNSMOBILE   = "http://www.google.com/schemas/sitemap-mobile/1.0"
	PREAMBLE      = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	MAXURLSETSIZE = 50000
	MAXFILESIZE   = 10 * 1024 * 1024 //10mb
)

Variables

View Source
var (
	ErrMaxUrlSetSize = errors.New("exceeded maximum number of URLs allowed in sitemap")
	ErrMaxFileSize   = errors.New("exceeded maximum file size of a sitemap (10mb)")
	ISMOBILE         = new(struct{})
)

Functions

func CloseIndexGroups

func CloseIndexGroups(groups ...*IndexGroup) (done <-chan bool)

Starts to run the given list of Sitemap Groups concurrently.

func CreateSitemapIndex

func CreateSitemapIndex(indexFilePath string, index Index) (err error)

Creates and gzip the xml index

func PingSearchEngines

func PingSearchEngines(indexFile string)

Sends a ping to search engines indicating that the index has been updated. Currently supports Google and Bing.

Types

type ChangeFreq

type ChangeFreq string
const (
	Always  ChangeFreq = "always"
	Hourly  ChangeFreq = "hourly"
	Daily   ChangeFreq = "daily"
	Weekly  ChangeFreq = "weekly"
	Monthly ChangeFreq = "monthly"
	Yearly  ChangeFreq = "yearly"
	Never   ChangeFreq = "never"
)

type File

type File struct {
	Name    string
	Content []byte
}

func (*File) Write

func (f *File) Write(w io.Writer) error

type HttpResponse

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

type Index

type Index struct {
	XMLName  xml.Name  `xml:"sitemapindex"`
	XMLNS    string    `xml:"xmlns,attr"`
	Sitemaps []Sitemap `xml:"sitemap"`
}

func CreateIndexByScanDir

func CreateIndexByScanDir(targetDir string, indexFileName string, public_url string) (index Index)

Search all the xml.gz sitemaps_dir directory, uses the modified date of the file as lastModified path_index is included for the function does not include the url of the index in your own content, if it is present in the same directory.

func CreateIndexBySlice

func CreateIndexBySlice(urls []string, public_url string) (index Index)

Returns an index sitemap starting from a slice of urls

type IndexGroup

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

func NewIndexGroup

func NewIndexGroup(folder string, name string) (*IndexGroup, error)

Creates a new group of sitemaps indice that used a common name. If the sitemap exceed the limit of 50k urls, new sitemaps will have a numeric suffix to the name. Example: - blog_1.xml.gz - blog_2.xml.gz

func (*IndexGroup) Add

func (s *IndexGroup) Add(entry Sitemap)

Add a sitemap.Sitemap to the group

func (*IndexGroup) Clear

func (s *IndexGroup) Clear()

Clean Urls not yet added to the group

func (*IndexGroup) Close

func (s *IndexGroup) Close() <-chan bool

Mandatory operation, handle the rest of the url that has not been added to any sitemap and add. Furthermore performs cleaning of variables and closes the channel group

func (*IndexGroup) Configure

func (s *IndexGroup) Configure(name string, folder string) error

Configure name and folder of group

func (*IndexGroup) Create

func (s *IndexGroup) Create(index Index)

Saves the sitemap from the sitemap.URLSet

func (*IndexGroup) Initialize

func (s *IndexGroup) Initialize()

Initialize channel

type Sitemap

type Sitemap struct {
	Loc     string     `xml:"loc"`
	LastMod *time.Time `xml:"lastmod,omitempty"`
}

type SitemapGroup

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

func NewSitemapGroup

func NewSitemapGroup(name string, isMobile bool) *SitemapGroup

Creates a new group of sitemaps that used a common name. If the sitemap exceed the limit of 50k urls, new sitemaps will have a numeric suffix to the name. Example: - blog_1.xml.gz - blog_2.xml.gz

func (*SitemapGroup) Add

func (s *SitemapGroup) Add(url URL)

Add a sitemap.URL to the group

func (*SitemapGroup) Clear

func (s *SitemapGroup) Clear()

Clean Urls not yet added to the group

func (*SitemapGroup) ClearSavedSitemaps

func (s *SitemapGroup) ClearSavedSitemaps()

clean array of already generated sitemaps (not delete files)

func (*SitemapGroup) Configure

func (s *SitemapGroup) Configure(name string, isMobile bool)

Configure name and folder of group

func (*SitemapGroup) Create

func (s *SitemapGroup) Create(url_set URLSet) ([]File, error)

Saves the sitemap from the sitemap.URLSet

func (*SitemapGroup) Files

func (s *SitemapGroup) Files() chan File

func (*SitemapGroup) URLs

func (s *SitemapGroup) URLs() []string

returns the url of already generated sitemaps

type URL

type URL struct {
	Loc        string     `xml:"loc"`
	LastMod    *time.Time `xml:"lastmod,omitempty"`
	ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
	Priority   float64    `xml:"priority,omitempty"`
	Mobile     *struct{}  `xml:"mobile:mobile,omitempty"`
}

type URLSet

type URLSet struct {
	XMLName     xml.Name `xml:"urlset"`
	XMLNS       string   `xml:"xmlns,attr"`
	XMLNSMOBILE string   `xml:"xmlns:mobile,attr,omitempty"`
	URLs        []URL    `xml:"url"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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