sitemap

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 6 Imported by: 2

README

Sitemap

Usage

  • Create a sitemap
sitemap := SiteMap() // will use the default file name and path as '/sitemap.xml'
sitemap := SiteMap("product") //  /product.xml
  • Register URLs

    • Register a raw string path

      sitemap.RegisterRawString("/product1") // path mode
      sitemap.RegisterRawString("https://qor5.dev.com/product1") //url mode
      
    • Register a regularURL

      sitemap.RegisterURL(URL{Loc: "/product1"}, URL{Loc: "https://qor5.dev.com/product1"})
      
    • Register a contextFunc

      sitemap.RegisterContextFunc(func(context.Context) []URL {
          // fetch and generate the urls by the context
      }
      )
      
    • Register a model

      type product struct{
          ...
      }
      
      // model need to implement this method
      func (p product) Sitemap(ctx context.Context) []URL {
          // fetch urls from db
      }
      
      sitemap.RegisterModel(&product{}) // path mode
      
  • Mount to HTTP ServeMux, will automatically fetch the host according to the request and put it in context.

    serveMux := http.NewServeMux()
    site.MountTo(serveMux)
    
  • Generate xml string data directly according to the host in the context

    sitemap.EncodeToXml(WithHost("https://qor5.dev.com"))
    
  • Ping the search engine when the new sitemap is generated

    PingBing(sitemap,WithHost("https://qor5.dev.com"))
    PingGoogle(sitemap,WithHost("https://qor5.dev.com"))
    PingAll(sitemap,WithHost("https://qor5.dev.com"))
    

Sitemap Index

index := SiteMapIndex().RegisterSiteMap(SiteMap(), SiteMap("product"), SiteMap("post")) // Register multiple sitemaps

index.EncodeToXml(WithHost("https://qor5.dev.com")) // Generate xml string data directly
index.MountTo(serveMux) // MountTo Mux

Robots

  • Create a robots
robots := Robots()
  • Register a agent
robot.Agent(GoogleAgent).Allow("/product1", "/product2") // Allow

robot.Agent(GoogleAgent).Disallow("/product1", "/product2") // Disallow

robot.Agent(GoogleAgent).AddSitemapUrl(sitemao.ToUrl(WithHost("https://qor5.dev.com")))
 // Add a sitemap

  • Mount to HTTP ServeMux,
	serveMux := http.NewServeMux()
	robot.MountTo(serveMux)
  • Generate plain txt
	robot.ToTxt()

Documentation

Index

Constants

View Source
const (
	// https://www.keycdn.com/blog/web-crawlers
	AllAgents     = "*"
	GoogleAgent   = "Googlebot"
	BingAgent     = "Bingbot"
	YahooAgent    = "Slurp"
	DuckDuckAgent = "DuckDuckBot"
	BaiduAgent    = "Baiduspider"
	YandexAgent   = "YandexBot"
	SogouAgent    = "Sogou web spider/4.0"
	ExaleadAgent  = "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Exabot-Thumbnails)"
	FacebookAgent = "facebot"
	AlexaAgent    = "ia_archiver"
)
View Source
const (
	FreqNever   = "never"
	FreqYearly  = "yearly"
	FreqMonthly = "monthly"
	FreqWeekly  = "weekly"
	FreqDaily   = "daily"
	FreqHourly  = "hourly"
	FreqAlways  = "always"
)

Variables

This section is empty.

Functions

func EncodeToXmlByRequest

func EncodeToXmlByRequest(r *http.Request, encoder EncodeToXmlInterface) string

func PingAll

func PingAll(site ToUrlInterface, ctx context.Context) (err error)

func PingBing

func PingBing(site ToUrlInterface, ctx context.Context) (err error)

func PingGoogle

func PingGoogle(site ToUrlInterface, ctx context.Context) (err error)

func WithHost

func WithHost(host string, ctxs ...context.Context) context.Context

Types

type ContextFunc

type ContextFunc func(context.Context) []URL

type EncodeToXmlInterface

type EncodeToXmlInterface interface {
	EncodeToXml(ctx context.Context) string
}

type ModelInferface

type ModelInferface interface {
	Sitemap(context.Context) []URL
}

type RobotsBuilder

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

func Robots

func Robots() *RobotsBuilder

func (*RobotsBuilder) Agent

func (r *RobotsBuilder) Agent(name string) *userAgentBuilder

func (*RobotsBuilder) MountTo

func (robot *RobotsBuilder) MountTo(mux *http.ServeMux)

func (*RobotsBuilder) ServeHTTP

func (robot *RobotsBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*RobotsBuilder) ToTxt

func (r *RobotsBuilder) ToTxt() string

type SiteMapBuilder

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

func SiteMap

func SiteMap(names ...string) (s *SiteMapBuilder)

func (SiteMapBuilder) EncodeToXml

func (s SiteMapBuilder) EncodeToXml(ctx context.Context) string

func (*SiteMapBuilder) MountTo

func (site *SiteMapBuilder) MountTo(mux *http.ServeMux)

func (*SiteMapBuilder) RegisterContextFunc

func (site *SiteMapBuilder) RegisterContextFunc(funcs ...ContextFunc) (s *SiteMapBuilder)

func (*SiteMapBuilder) RegisterModel

func (site *SiteMapBuilder) RegisterModel(models ...ModelInferface) (s *SiteMapBuilder)

func (*SiteMapBuilder) RegisterRawString

func (site *SiteMapBuilder) RegisterRawString(rs ...string) (s *SiteMapBuilder)

func (*SiteMapBuilder) RegisterURL

func (site *SiteMapBuilder) RegisterURL(urls ...URL) (s *SiteMapBuilder)

func (*SiteMapBuilder) ServeHTTP

func (site *SiteMapBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*SiteMapBuilder) ToUrl

func (site *SiteMapBuilder) ToUrl(ctx context.Context) string

type SiteMapIndexBuilder

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

func SiteMapIndex

func SiteMapIndex(names ...string) (s *SiteMapIndexBuilder)

func (SiteMapIndexBuilder) EncodeToXml

func (s SiteMapIndexBuilder) EncodeToXml(ctx context.Context) string

func (*SiteMapIndexBuilder) MountTo

func (index *SiteMapIndexBuilder) MountTo(mux *http.ServeMux)

func (*SiteMapIndexBuilder) RegisterSiteMap

func (index *SiteMapIndexBuilder) RegisterSiteMap(sites ...*SiteMapBuilder) (s *SiteMapIndexBuilder)

func (*SiteMapIndexBuilder) ServeHTTP

func (index *SiteMapIndexBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*SiteMapIndexBuilder) ToUrl

func (index *SiteMapIndexBuilder) ToUrl(ctx context.Context) string

type ToUrlInterface

type ToUrlInterface interface {
	ToUrl(context.Context) string
}

type URL

type URL struct {
	Loc        string
	LastMod    string
	Changefreq string
	Priority   float32
}

Jump to

Keyboard shortcuts

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