Skip to content

kosto1992/go-sitemap-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-sitemap-generator is the easiest way to generate Sitemaps in Go.

GoDoc Build Status

package main

import (
	"github.com/ikeikeikeike/go-sitemap-generator/stm"
)


func main() {
	sm := stm.NewSitemap(1)

	// Create method must be that calls first this method in that before
	// call to Add method on this struct.
	sm.Create()

	sm.Add(stm.URL{{"loc", "home"}, {"changefreq", "always"}, {"mobile", true}})
	sm.Add(stm.URL{{"loc", "readme"}})
	sm.Add(stm.URL{{"loc", "aboutme"}, {"priority", 0.1}})

	sm.Finalize().PingSearchEngines()
}

Sitemap provides interface for create sitemap xml file and that has convenient interface. And also needs to use first Sitemap struct if it wants to use this package.

Installing

$ go get github.com/ikeikeikeike/go-sitemap-generator/stm

Features

Current Features or To-Do

Getting Started

Setting concurrency

To disable concurrency, set number of CPUs to 1.

sm := stm.NewSitemap(1)

If you want to set max CPUs that are available, set number of CPUs <= 0.

sm := stm.NewSitemap(0)

Preventing Output

To disable all non-essential output you can give false to sm.SetVerbose. To disable output in-code use the following:

sm := stm.NewSitemap(1)
sm.SetVerbose(false)

Pinging Search Engines

PingSearchEngines requests some ping server.

sm.Finalize().PingSearchEngines()

If you want to add new search engine, you can set that to method's arguments. like this.

sm.Finalize().PingSearchEngines("http://newengine.com/ping?url=%s")

Options

// Your website's host name
sm.SetDefaultHost("http://www.example.com")

// The remote host where your sitemaps will be hosted
sm.SetSitemapsHost("http://s3.amazonaws.com/sitemap-generator/")

// The directory to write sitemaps to locally
sm.SetPublicPath("tmp/")

// Set this to a directory/path if you don't want to upload to the root of your `SitemapsHost`
sm.SetSitemapsPath("sitemaps/")

// Struct of `S3Adapter`
sm.SetAdapter(&stm.S3Adapter{Region: "ap-northeast-1", Bucket: "your-bucket", ACL: "public-read"})

// It changes to output filename
sm.SetFilename("new_filename")

Upload sitemap to S3

Recently I disabled this module here.

package main

import (
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/ikeikeikeike/go-sitemap-generator/stm"
)

func main() {
	sm := stm.NewSitemap(1)
	sm.SetDefaultHost("http://example.com")
	sm.SetSitemapsPath("sitemap-generator") // default: public
	sm.SetSitemapsHost("http://s3.amazonaws.com/sitemap-generator/")
	sm.SetAdapter(&stm.S3Adapter{
		Region: "ap-northeast-1",
		Bucket: "your-bucket",
		ACL:    "public-read",
		Creds:  credentials.NewEnvCredentials(),
	})

	sm.Create()

	sm.Add(stm.URL{{"loc", "home"}, {"changefreq", "always"}, {"mobile", true}})
	sm.Add(stm.URL{{"loc", "readme"}})
	sm.Add(stm.URL{{"loc", "aboutme"}, {"priority", 0.1}})

	sm.Finalize().PingSearchEngines()
}

News sitemaps

sm.Add(stm.URL{
	{"loc", "/news"}, 
	{"news", stm.URL{
	{"publication", stm.URL{
		{"name",     "Example"},
		{"language", "en"},
	},
	},
	{"title",            "My Article"},
	{"keywords",         "my article, articles about myself"},
	{"stock_tickers",    "SAO:PETR3"},
	{"publication_date", "2011-08-22"},
	{"access",           "Subscription"},
	{"genres",           "PressRelease"},
},},})

Look at Creating a Google News Sitemap as required.

Video sitemaps

sm.Add(stm.URL{
	{"loc", "/videos"}, 
	{"video", stm.URL{
	{"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"},
	{"title",         "Title"},
	{"description",   "Description"},
	{"content_loc",   "http://www.example.com/cool_video.mpg"},
	{"category",      "Category"},
	{"tag",           []string{"one", "two", "three"}},
    {"player_loc",    stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}},},
},
},
})

Look at Video sitemaps as required.

Image sitemaps

sm.Add(stm.URL{
	{"loc", "/images"}, 
	{"image", []stm.URL{
	{{"loc", "http://www.example.com/image.png"}, {"title", "Image"}},
	{{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}},
},},
})

Look at Image sitemaps as required.

Geo sitemaps

sm.Add(stm.URL{
	{"loc", "/geos"}, 
	{"geo", stm.URL{
	{"format", "kml"},
},},
})

Couldn't find Geo sitemaps example. Although its like a below.

<url>
	<loc>/geos</loc>
	<geo:geo>
		<geo:format>kml</geo:format>
	</geo:geo>
</url>

Mobile sitemaps

sm.Add(stm.URL{{"loc", "mobiles"}, {"mobile", true}})

Look at Feature phone sitemaps as required.

Full example

package main

import (
	"github.com/ikeikeikeike/go-sitemap-generator/stm"
)

func main() {
	sm := stm.NewSitemap(0)
	sm.SetDefaultHost("http://yourhost.com")
	sm.SetSitemapsHost("http://s3.amazonaws.com/sitemaps/")
	sm.SetSitemapsPath("sitemaps/")
	sm.SetFilename("anothername")
	sm.SetCompress(true)
	sm.SetVerbose(true)
	sm.SetAdapter(&stm.S3Adapter{Region: "ap-northeast-1", Bucket: "your-bucket"})

	sm.Create()

	sm.Add(stm.URL{{"loc", "/home"}, {"changefreq", "daily"}})

	sm.Add(stm.URL{{"loc", "/abouts"}, {"mobile", true}})

	sm.Add(stm.URL{{"loc", "/news"}, 
	{"news", stm.URL{
		{"publication", stm.URL{
			{"name",     "Example"},
			{"language", "en"},
		},
		},
		{"title",            "My Article"},
		{"keywords",         "my article, articles about myself"},
		{"stock_tickers",    "SAO:PETR3"},
		{"publication_date", "2011-08-22"},
		{"access",           "Subscription"},
		{"genres",           "PressRelease"},
	},},
	})

	sm.Add(stm.URL{{"loc", "/images"}, 
	{"image", []stm.URL{
		{{"loc", "http://www.example.com/image.png"}, {"title", "Image"}},
		{{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}},
	},},
	})

	sm.Add(stm.URL{{"loc", "/videos"}, 
	{"video", stm.URL{
		{"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"},
		{"title",         "Title"},
		{"description",   "Description"},
		{"content_loc",   "http://www.example.com/cool_video.mpg"},
		{"category",      "Category"},
		{"tag",           []string{"one", "two", "three"}},
	    {"player_loc",    stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}}},
	},},
	})

	sm.Add(stm.URL{{"loc", "/geos"}, 
	{"geo", stm.URL{
		{"format", "kml"},
	},},
	})

	sm.Finalize().PingSearchEngines("http://newengine.com/ping?url=%s")
}

Webserver example

package main

import (
	"log"
	"net/http"

	"github.com/ikeikeikeike/go-sitemap-generator/stm"
)

func buildSitemap() *stm.Sitemap {
	sm := stm.NewSitemap(1)
	sm.SetDefaultHost("http://example.com")

	sm.Create()
	sm.Add(stm.URL{{"loc", "/"}, {"changefreq", "daily"}})

	// Note: Do not call `sm.Finalize()` because it flushes
	// the underlying datastructure from memory to disk.

	return sm
}

func main() {
	sm := buildSitemap()

	mux := http.NewServeMux()
	mux.HandleFunc("/sitemap.xml", func(w http.ResponseWriter, r *http.Request) {
		// Go's webserver automatically sets the correct `Content-Type` header.
		w.Write(sm.XMLContent())
		return
	})

	log.Fatal(http.ListenAndServe(":8080", mux))
}

Documentation

How to testing

Prepare testing

$ go get github.com/clbanning/mxj

Do testing

$ go test -v -cover ./...

Inspired by sitemap_generator

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%