xuxin 316723d774 reorganize project structure 1 year ago
..
README.md 316723d774 reorganize project structure 1 year ago
http.go 316723d774 reorganize project structure 1 year ago
ping.go 316723d774 reorganize project structure 1 year ago
robots.go 316723d774 reorganize project structure 1 year ago
robots_test.go 316723d774 reorganize project structure 1 year ago
sitemap.go 316723d774 reorganize project structure 1 year ago
sitemap_test.go 316723d774 reorganize project structure 1 year ago
xml.go 316723d774 reorganize project structure 1 year ago

README.md

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()